/*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();
}
/*因为我用的是固定长度的数组,所以出队几个元素也要入队几个元素,初学者,实现的比较繁琐*/