实验三 栈和队列的基本操作实现及其应用
来源:互联网 发布:ps软件培训班 编辑:程序博客网 时间:2024/06/05 23:42
一、实验目的
1、 熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。
2、 学会使用栈和队列解决实际问题。
二、实验内容
1、自己确定结点的具体数据类型和问题规模:
分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。
分别建立一个顺序队列和链队列,实现队列的入队和出队操作。
2、设计算法并写出代码,实现将一个十进制数转换成二进制数。
三、代码
顺序栈
#include<iostream>using namespace std;const int StackSize=10;class SeqStack{public:SeqStack();~SeqStack(){}void Push(int x);int Pop();int getTop();int Empty();private:int data[StackSize];int top;};SeqStack::SeqStack(){top=-1;}void SeqStack::Push(int x){if(top==StackSize-1) throw"上溢";top++;data[top]=x;}int SeqStack::Pop(){int x;if(top==-1) throw"下溢";x=data[top--];return x;}int SeqStack::getTop(){if(top!=-1)return data[top];}int SeqStack::Empty(){if(top==-1) return 1;else return 0;}void main(){SeqStack s;if(s.Empty())cout<<"栈为空"<<endl;elsecout<<"栈非空"<<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 namespace std;struct node{int data;node *next;};class SeqStack{public:SeqStack(){top=NULL;}~SeqStack(){}void Push(int x);int Pop();int getTop(){if(top!=NULL)return top->data;}int Empty();private:node *top;};void SeqStack::Push(int x){node *s;s=new node;s->data=x;s->next=top;top=s;}int SeqStack::Pop(){int x;node *p;if(top==NULL) throw"下溢";x=top->data;p=top;top=top->next;delete p;return x;}int SeqStack::Empty(){if(top==NULL) return 1;else return 0;}void main(){SeqStack S;if(S.Empty())cout<<"栈为空"<<endl;elsecout<<"栈非空"<<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 namespace std;const int QueueSize=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(int x){if((rear+1)%QueueSize==front) throw"上溢";rear=(rear+1)%QueueSize;data[rear]=x;}int CirQueue::DeQueue(){if(rear==front) throw"下溢";front=(front+1)%QueueSize;return data[front];}int CirQueue::GetQueue(){int i;if(rear==front) throw"下溢";i=(front+1)%QueueSize;return data[i];}int CirQueue::Empty(){if(front==rear)return 1;elsereturn 0;}void main(){CirQueue c;if(c.Empty())cout<<"队列为空"<<endl;elsecout<<"队列非空"<<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 namespace std;struct node{int data;node *next;};class LinkQueue{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;}}void LinkQueue::EnQueue(int x){node *s=NULL;s=new node;s->data=x;s->next=NULL;rear->next=s;rear=s;}int LinkQueue::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;}int LinkQueue::GetQueue(){ if(front!=rear)return front->next->data;}int LinkQueue::Empty(){if(front==rear)return 1;elsereturn 0;}void main(){LinkQueue l;if(l.Empty())cout<<"队列为空"<<endl;elsecout<<"队列非空"<<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>using namespace std;struct node{int data;node *next;};class SeqStack{public:SeqStack(){top=NULL;}~SeqStack(){}void Push(int x);int Pop();private:node *top;};void SeqStack::Push(int x){node *s;s=new node;s->data=x;s->next=top;top=s;}int SeqStack::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;}
这次实验难度不大,相互之间都有相似性,只是内容有点多。
阅读全文
1 0
- 实验三:栈和队列的基本操作实现及其应用——顺序栈
- 实验三 栈和队列的基本操作实现及其应用
- 实验三 栈和队列的基本操作实现及其应用
- 实验三 栈和队列的基本操作实现及其应用 (1)
- 实验4:栈和队列的基本操作实现及其应用——循环队列
- 实验4:栈和队列的基本操作实现及其应用——链队列
- 实验4:栈和队列的基本操作实现及其应用之《循环队列》
- 实验4:栈和队列的基本操作实现及其应用之《链队列》
- 实验4:栈和队列的基本操作实现及其应用之《顺序队列》
- 实验4:栈和队列的基本操作实现及其应用之《链队列》
- 实验4:栈和队列的基本操作实现及其应用之《顺序队列》
- 实验4:栈和队列的基本操作实现及其应用之《链队列》
- 实验三 顺序栈的基本操作实现及其应用
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 实验4:栈和队列的基本操作实现及其应用之《顺序栈》
- 实验4:栈和队列的基本操作实现及其应用之《顺序栈》
- 实验4:栈和队列的基本操作实现及其应用之《顺序栈》
- 实验4:栈和队列的基本操作实现及其应用——链栈
- liunx基本命令(十一)系统日志管理
- hdu5977 Garden of Eden
- 在屏幕上打印杨辉三角
- binary-tree-preorder-traversal java code
- 算法导论第4章习题解析
- 实验三 栈和队列的基本操作实现及其应用
- DockerHub基于Github自动化构建
- 链表学习记录
- LeetCode_697_Degree of an Array
- 循环队列的实现
- Linux下Vue开发环境搭建一篇全搞定
- 数据结构实验之栈与队列四:括号匹配
- CS231N-12-Visualizing & Understanding CNN
- JS(11)