C++ 用数组实现stack,queue
2011-08-20 08:40
387 查看
/*intstack.h*/
#include<iostream>
using namespace std;
class Stack
{
private:
int index;
int capability;
int *arr;
public:
Stack(int cap);
~Stack();
int size();
void pop( );
void push(int a);
bool isfull();
bool isempty();
void printStack();
};
/*intstack.cpp*/
#include"intstack.h"
#include<iostream>
//#include<vector>
using namespace std;
Stack::Stack(int cap)
{
capability=cap;
arr=new int[capability];
index=0;
}
Stack::~Stack()
{
delete arr;
}
int Stack::size()
{
return index;
}
void Stack::pop()
{
if(size()==0)
{cout<<"there is no data";}
else
{
cout<<"the data to pop out is: "<<arr[index-1]<<" "<<endl;
index=index-1;
}
cout<<endl;
}
void Stack::push(int a)
{
if(index<=(capability-1))
{
arr[index]=a;
index++;
}
if(isfull())
cout<<"you can not push in anymore"<<endl;
}
bool Stack::isfull()
{
if(index==capability)
return true;
else
return false;
}
void Stack::printStack()
{
cout<<"the stack now has elements: "<<endl;
for(int i=0;i<index;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
/*main.cpp*/
#include<iostream>
using namespace std;
#include"intstack.h"
void main()
{
Stack* s=new Stack(4);
s->printStack();
s->push(5);
s->printStack();
s->push(2);
s->printStack();
s->push(4);
s->printStack();
s->push(1);
s->printStack();
s->isfull();
s->pop();
s->printStack();
s->pop();
s->printStack();
system("pause");
}
/*queue.h*/
#ifndef QUEUE_H_
#define QUEUE_H_
class Queue
{
private:
int *a;
int front;
int rear;
int len;
int index;
public:
Queue(int l=10);
~Queue();
void enqueue(int d);
void dequeue();
bool isfull();
bool isempty();
void show();
};
#endif QUEUE_H_
/*queue.cpp*/
#include"queue.h"
#include<iostream>
using namespace std;
Queue::Queue(int l)
{
if(l<1)
l=10;
a=new int[l];
front=0;
rear=0;
len=l;
index=0;
}
Queue::~Queue()
{
delete[]a;
front=0;
rear=0;
len=0;
index=0;
}
bool Queue::isempty()
{
if(index==0)
return true;
else
return false;
}
bool Queue::isfull()
{
if(index==len)
return true;
else
return false;
}
void Queue::enqueue(int d)
{
if(isfull()==true)
{
cout<<"there is no space"<<endl;
return;
}
index=index+1;
a[rear]=d;
rear=index;
}
void Queue::dequeue()
{
if(isempty()==true)
{
cout<<"it is a empty queue"<<endl;
return;
}
int data;
data=a[front];
index=index-1;
rear=index;
for(int i=front;i<=index;i++)
a[i]=a[i+1];
cout<<data<<"has been dequeue"<<endl;
}
void Queue::show()
{
for(int i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl;
}
/*mail.cpp*/
#include"queue.h"
void main()
{
Queue *q=new Queue(6);
q->enqueue(3);
q->enqueue(5);
q->enqueue(12);
q->enqueue(1);
q->enqueue(9);
q->enqueue(13);
q->dequeue();
q->dequeue();
q->dequeue();
q->enqueue(6);
q->enqueue(8);
q->enqueue(0);
q->show();
}
/*因为我用的是固定长度的数组,所以出队几个元素也要入队几个元素,初学者,实现的比较繁琐*/
#include<iostream>
using namespace std;
class Stack
{
private:
int index;
int capability;
int *arr;
public:
Stack(int cap);
~Stack();
int size();
void pop( );
void push(int a);
bool isfull();
bool isempty();
void printStack();
};
/*intstack.cpp*/
#include"intstack.h"
#include<iostream>
//#include<vector>
using namespace std;
Stack::Stack(int cap)
{
capability=cap;
arr=new int[capability];
index=0;
}
Stack::~Stack()
{
delete arr;
}
int Stack::size()
{
return index;
}
void Stack::pop()
{
if(size()==0)
{cout<<"there is no data";}
else
{
cout<<"the data to pop out is: "<<arr[index-1]<<" "<<endl;
index=index-1;
}
cout<<endl;
}
void Stack::push(int a)
{
if(index<=(capability-1))
{
arr[index]=a;
index++;
}
if(isfull())
cout<<"you can not push in anymore"<<endl;
}
bool Stack::isfull()
{
if(index==capability)
return true;
else
return false;
}
void Stack::printStack()
{
cout<<"the stack now has elements: "<<endl;
for(int i=0;i<index;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
/*main.cpp*/
#include<iostream>
using namespace std;
#include"intstack.h"
void main()
{
Stack* s=new Stack(4);
s->printStack();
s->push(5);
s->printStack();
s->push(2);
s->printStack();
s->push(4);
s->printStack();
s->push(1);
s->printStack();
s->isfull();
s->pop();
s->printStack();
s->pop();
s->printStack();
system("pause");
}
/*queue.h*/
#ifndef QUEUE_H_
#define QUEUE_H_
class Queue
{
private:
int *a;
int front;
int rear;
int len;
int index;
public:
Queue(int l=10);
~Queue();
void enqueue(int d);
void dequeue();
bool isfull();
bool isempty();
void show();
};
#endif QUEUE_H_
/*queue.cpp*/
#include"queue.h"
#include<iostream>
using namespace std;
Queue::Queue(int l)
{
if(l<1)
l=10;
a=new int[l];
front=0;
rear=0;
len=l;
index=0;
}
Queue::~Queue()
{
delete[]a;
front=0;
rear=0;
len=0;
index=0;
}
bool Queue::isempty()
{
if(index==0)
return true;
else
return false;
}
bool Queue::isfull()
{
if(index==len)
return true;
else
return false;
}
void Queue::enqueue(int d)
{
if(isfull()==true)
{
cout<<"there is no space"<<endl;
return;
}
index=index+1;
a[rear]=d;
rear=index;
}
void Queue::dequeue()
{
if(isempty()==true)
{
cout<<"it is a empty queue"<<endl;
return;
}
int data;
data=a[front];
index=index-1;
rear=index;
for(int i=front;i<=index;i++)
a[i]=a[i+1];
cout<<data<<"has been dequeue"<<endl;
}
void Queue::show()
{
for(int i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl;
}
/*mail.cpp*/
#include"queue.h"
void main()
{
Queue *q=new Queue(6);
q->enqueue(3);
q->enqueue(5);
q->enqueue(12);
q->enqueue(1);
q->enqueue(9);
q->enqueue(13);
q->dequeue();
q->dequeue();
q->dequeue();
q->enqueue(6);
q->enqueue(8);
q->enqueue(0);
q->show();
}
/*因为我用的是固定长度的数组,所以出队几个元素也要入队几个元素,初学者,实现的比较繁琐*/
相关文章推荐
- C++实现一个数组类及其stack适配器
- C++ 队列(queue)堆栈(stack)实现基础
- Stack_Queue 一个数组实现三个栈 @CareerCup
- 从零开始学C++之模板(二):类模板、Stack的类模板实现(自定义链栈方式,自定义数组方式)
- Stack与Queue的实现(c++模板实现)
- 从零开始学C++之模板(二):类模板、Stack的类模板实现(自定义链栈方式,自定义数组方式)
- 数据结构之stack,queue的数组与链表实现
- 队列(Queue)-- 数组实现(C++ )
- C++用数组和链表分别实现Queue
- C++用数组和链表分别实现Stack
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- C++用数组和链表分别实现Queue
- C++用数组和链表分别实现Stack
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 数据结构学习笔记——stack实现(数组篇)
- 用数组实现Stack
- 基于Vector实现C++中的适配器stack(顺序栈)
- C++模拟实现堆heap和优先级队列poriorty_queue
- js用数组实现Stack
- C++实现字符串数组作为函数的参数的反序输出