实验2:栈和队列的基本操作实现及其应用
一、实验目的
1、 熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。
2、 学会使用栈和队列解决实际问题。
二、实验内容
1、自己确定结点的具体数据类型和问题规模:
分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。
分别建立一个顺序队列和链队列,实现队列的入队和出队操作。
顺序栈
#include<iostream>
using namespacestd;
const intStackSize=10;
class SeqStack
{
public:
SeqStack(){top=-1;}
~SeqStack(){}
void Push(int x);
int Pop();
int GetTop();
int Empty();
private:
int data[StackSize];
int top;
};
voidSeqStack::Push(int x)
{
if(top==StackSize-1) throw"上溢";
data[++top]=x;
}
intSeqStack::Pop()
{
int x;
if(top==-1) throw"下溢";
x=data[top--];
return x;
}
intSeqStack::GetTop()
{
if(top!=-1)
return data[top];
}
intSeqStack::Empty()
{
if(top==-1) return 1;
else return 0;
}
void main()
{
SeqStack s;
if(s.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈为非空"<<endl;
cout<<"进行1,2入栈操作"<<endl;
s.Push(1);
s.Push(2);
cout<<"栈顶元素:"<<endl;
cout<<s.GetTop()<<endl;
cout<<"出栈:"<<endl;
s.Pop();
cout<<"栈顶元素:"<<endl;
cout<<s.GetTop()<<endl;
}
#include<iostream.h>
struct node
{
int data;
node *next;
};
classLinkStack
{
public:
LinkStack(){top=NULL;}
~LinkStack(){}
void Push(int x);
int Pop();
int getTop(){if(top!=NULL)returntop->data;}
int Empty();
private:
node *top;
};
voidLinkStack::Push(int x)
{
node *s;
s=new node;s->data=x;
s->next=top;top=s;
}
intLinkStack::Pop()
{
int x;
node *p;
if(top==NULL) throw"下溢";
x=top->data;p=top;
top=top->next;
delete p;
return x;
}
intLinkStack::Empty()
{
if(top==NULL) return 1;
else return 0;
}
void main()
{
LinkStack S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对1和2执行入栈操作"<<endl;
S.Push(1);
S.Push(2);
cout<<"栈顶元素为:"<<endl;
cout<<S.getTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.getTop()<<endl;
}
#include<iostream>
using namespacestd;
const intQueueSize=100;
class CirQueue
{
public:
CirQueue(){front=rear=QueueSize-1;}
~CirQueue(){}
void Enqueue(int x);
int DeQueue();
int GetQueue();
int Empty();
private:
int data[QueueSize];
int front,rear;
};
void CirQueue::Enqueue(intx)
{
if((rear+1)%QueueSize==front) throw"上溢";
rear=(rear+1)%QueueSize;
data[rear]=x;
}
intCirQueue::DeQueue()
{
if(rear==front) throw"下溢";
front=(front+1)%QueueSize;
return data[front];
}
intCirQueue::GetQueue()
{
int i;
if(rear==front) throw"下溢";
i=(front+1)%QueueSize;
return data[i];
}
intCirQueue::Empty()
{
if(front==rear)
return 1;
else
return 0;
}
void main()
{
CirQueue c;
if(c.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列为非空"<<endl;
cout<<"对1和2执行入队操作"<<endl;
c.Enqueue(1);
c.Enqueue(2);
cout<<"队头元素:"<<endl;
cout<<c.GetQueue()<<endl;
cout<<"执行一次出队操作"<<endl;
c.DeQueue();
cout<<"队头元素:"<<endl;
cout<<c.GetQueue()<<endl;
}
#include<iostream>
using namespacestd;
struct node
{
int data;
node *next;
};
classLinkQueue
{
public:
LinkQueue();
~LinkQueue();
void EnQueue(int x);
int DeQueue();
int GetQueue();
int Empty();
private:
node *front,*rear;
};
LinkQueue::LinkQueue()
{
node *s=NULL;
s=new node;
s->next=NULL;
front=rear=s;
}
LinkQueue::~LinkQueue()
{
node *p=NULL;
while(front!=NULL)
{
p=front->next;
delete front;
front=p;
}
}
voidLinkQueue::EnQueue(int x)
{
node *s=NULL;
s=new node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
intLinkQueue::DeQueue()
{
node *p=NULL;
int x;
if(rear==front)throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next==NULL) rear=front;
delete p;
return x;
}
intLinkQueue::GetQueue()
{
if(front!=rear)
return front->next->data;
}
intLinkQueue::Empty()
{
if(front==rear)
return 1;
else
return 0;
}
void main()
{
LinkQueue l;
if(l.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
cout<<"对1和2执行入队操作"<<endl;
try
{
l.EnQueue(1);
l.EnQueue(2);
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"队头元素为:"<<endl;
cout<<l.GetQueue()<<endl;
cout<<"执行一次出队操作"<<endl;
try
{
l.DeQueue();
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"队头元素为:"<<endl;
cout<<l.GetQueue()<<endl;
}
#include<iostream.h>
const intStackSize=100;
class SeqStack
{
public:
SeqStack(){top=-1;}
~SeqStack(){}
int Empty();
int data[StackSize];
int top;
};
int main()
{
int x;
SeqStack s;
s.top=-1;
cout<<"输入一个十进制数字:"<<endl;
cin>>x;
do{
s.top++;
s.data[s.top]=x%2;
x=x/2;
}while(x!=0);
cout<<"该数字的二进制为:"<<endl;
do
{
cout<<s.data[s.top];
s.top--;
}while (s.top!=-1);
cout<<endl;
return 0;
}
2、设计算法并写出代码,实现一个十将二进制转换成2进制数。
3、选做题(*)
设计一个模拟饭堂排队打饭管理软件,实现“先来先打饭”的排号叫号管理。
三、实验步骤
1、依据实验内容分别说明实验程序中用到的数据类型的定义;
2、相关操作的算法表达;
3、完整程序;
4、总结、运行结果和分析。
5、总体收获和不足,疑问等。
四、实验要求
1、 按照数据结构实验任务书,提前做好实验预习与准备工作。
2、 加“*”为选做题。做好可加分。
3、 严格按照数据结构实验报告模板和规范,及时完成实验报告。
4、 在个人主页上发文章提交作业。
5、 实验课会抽查3-5人,希望你可以被查到!
#include<iostream.h>
struct node
{
int data;
node *next;
};
classSeqStack
{
public:
SeqStack(){top=NULL;}
~SeqStack(){}
void Push(int x);
int Pop();
private:
node *top;
};
voidSeqStack::Push(int x)
{
node *s;
s=new node;s->data=x;
s->next=top;top=s;
}
intSeqStack::Pop()
{
int x;
node *p;
if(top==NULL) throw"下溢";
x=top->data;p=top;
top=top->next;
delete p;
return x;
}
int main()
{
int n,a,b,length=0;
cout<<"请输入一个十进制整数:"<<endl;
cin>>n;
a=n;
SeqStack s1;
while(a!=0)
{
b=a%2;
a=a/2;
s1.Push(b);
length++;
}
try{
cout<<"其二进制为:"<<endl;
while(length!=0){cout<<s1.Pop();length--;}
cout<<endl;
}
catch(char *wrong)
{cout<<wrong<<endl;}
return 0;
}