数据结构之C++实现栈与队列的建立及插入删除操作
栈的顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 50
class SqStack
{
public:
SqStack();
void Push(int x);
void Pop(void);
void show(void);
private:
int data[MAXSIZE];
int top;
};
SqStack::SqStack()
{
top=0;
}
void SqStack::Push(int x)
{
if(top == MAXSIZE-1)
{
cout <<"SqStack is full"<<endl;
exit(1);
}
data[top]=x;
top++;
}
void SqStack::Pop(void)
{
if(top == 0)
{
cout<<"SqStack is empty"<<endl;
exit(1);
}
top--;
}
void SqStack::show(void)
{
cout<<"stack is "<<endl;
for(int i=0;i<top;i++) cout<<data[i]<<" ";
cout<<endl<<"length is "<<top<<endl;
}
int main()
{
SqStack stack;
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.show();
return 0;
}
栈的链式存储结构
#include <iostream>
using namespace std;
struct StackNode
{
int data;
StackNode* next;
};
class LinkStack
{
public:
LinkStack();
void Push(int x);
void show(void);
void Pop(void);
private:
StackNode* top;
int count;
};
LinkStack::LinkStack()
{
top = new StackNode;
top->next = NULL;
count=0;
}
void LinkStack::Push(int x)
{
StackNode* s=new StackNode;
s->data=x;
s->next=top->next;
top->next = s;
count++;
}
void LinkStack::show(void)
{
StackNode* p=top->next;
while(p!=NULL)
{
cout << p->data << " ";
p = p->next;
}
cout<<endl<<"length is "<<count<<endl;
}
void LinkStack::Pop(void)
{
if(top->next == NULL)
{
cout<<"LinkStack is empty!!!"<<endl;
exit(1);
}
StackNode* p=top->next;
top->next=p->next;
delete(p);
count--;
}
int main()
{
LinkStack* stack=new LinkStack;
for(int i=0;i<10;i++) stack->Push(i);
stack->show();
for(int i=0;i<5;i++) stack->Pop();
stack->show();
return 0;
}
循环队列的顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 5
class SqQueue
{
public:
SqQueue();
int QueueLength(void);
bool isqueuefull(void);
void Insert(int x);
void Delete(void);
int data[MAXSIZE];
int front;
int rear;
};
SqQueue::SqQueue()
{
front=0;
rear=0;
}
int SqQueue::QueueLength(void)
{
return (rear-front+MAXSIZE)%MAXSIZE;
}
bool SqQueue::isqueuefull(void)
{
if((rear+1)%MAXSIZE == front) return true;
else return false;
}
void SqQueue::Insert(int x)
{
if(isqueuefull())
{
cout<<"queue is full!!!"<<endl;
exit(1);
}
data[rear]=x;
rear=(rear+1)%MAXSIZE;
}
void SqQueue::Delete(void)
{
if(front==rear)
{
cout<<"queue is empty!!!"<<endl;
exit(1);
}
front=(front+1)%MAXSIZE;
}
int main()
{
SqQueue* Queue = new SqQueue;
Queue->Insert(26);
Queue->Insert(26);
Queue->Insert(26);
Queue->Insert(26);
cout<<"front is "<<Queue->front<<". rear is "<<Queue->rear<<endl;
Queue->Delete();
cout<<"front is "<<Queue->front<<". rear is "<<Queue->rear<<endl;
if(Queue->isqueuefull()) cout<<"queue is full"<<endl;
else cout<<"queue is not full"<<endl;
return 0;
}
队列的链式存储结构
#include <iostream>
using namespace std;
struct Qnode
{
int data;
Qnode* next;
};
class LinkQueue
{
public:
LinkQueue();
void Insert(int x);
void show(void);
void Delete(void);
private:
Qnode* front;
Qnode* rear;
Qnode* first;
int length;
};
LinkQueue::LinkQueue()
{
first = new Qnode;
first->data=0;
first->next=NULL;
front = rear = first;
length=0;
}
void LinkQueue::Insert(int x)
{
Qnode* s= new Qnode;
s->data = x;
s->next = NULL;
rear->next=s;
rear=s;
length++;
}
void LinkQueue::show(void)
{
if(length == 0)
{
cout<<"LinkQueue is empty!!!"<<endl;
exit(1);
}
cout<<"LinkQueue's length is "<<length<<endl;
Qnode* p=front->next;
cout<<"LinkQueue is "<<endl;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void LinkQueue::Delete(void)
{
if(front==rear)
{
cout<<"LinkQueue is empty!!!"<<endl;
exit(1);
}
Qnode* p=front->next;
front->next=p->next;
if(rear==p) rear=front;
delete(p);
length--;
}
int main()
{
LinkQueue* ptr=new LinkQueue;
ptr->Insert(1);
ptr->Insert(23);ptr->Insert(25);ptr->Insert(26);ptr->Insert(28);
ptr->show();
ptr->Delete();
ptr->show();
return 0;
}