实验三 栈和队列的基本操作实现及其应用

来源:互联网 发布:淘宝客公司怎么样 编辑:程序博客网 时间:2024/06/05 12:46

一、实验目的

1、 熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。

2、学会使用栈和队列解决实际问题。

二、实验内容

1、自己确定结点的具体数据类型和问题规模:

分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。

分别建立一个顺序队列和链队列,实现队列的入队和出队操作。

2、设计算法并写出代码,实现一个十将二进制转换成2进制数。

3.设计一个模拟饭堂排队打饭管理软件,实现“先来先打饭”的排号叫号管理。

三、编码

1.顺序栈

#includeusing namespace std;const int MAX_SIZE=10;class SeqStack{private:float data[MAX_SIZE];int top;public:SeqStack();void push(float x);float pop();float getTop();int isEmpty();};SeqStack::SeqStack(){top=-1;}void SeqStack::push(float x){  //top上移,把x存到top指向的位置if(top==MAX_SIZE-1) throw"上溢!\n";data[++top]=x;}float SeqStack::pop(){  //取出栈顶元素,把top下移if(top==-1) throw"栈空!\n";return data[top--];}float SeqStack::getTop(){  //返回top指向位置的元素if(top==-1) throw"栈空!\n";return data[top];}int SeqStack::isEmpty(){  //topif(top==-1) return 1;else return 0;}int main(){try{SeqStack s;try{cout<<"将元素7.1入栈;\n";s.push(7.1);}catch(char *p){cout<
运行结果:

2.链栈
#includeusing namespace std;const int MAX_SIZE=10;struct Node{float data;Node *next;};class LinkedStack{private:Node *top;public:LinkedStack();~LinkedStack();void push(float x);float pop();float getTop();bool isEmpty();};LinkedStack::LinkedStack(){top=NULL;}LinkedStack::~LinkedStack(){  //若top指针不为空,其地址赋给p,delete pNode *p=top;while(top!=NULL){top=top->next;delete p;p=top;}}void LinkedStack::push(float x){  //新建一个结点,使其data域存放入栈元素x,top地址赋给其指针域,top上移指向新建结点Node *s=new Node;s->data=x;s->next=top;top=s;}float LinkedStack::pop(){  //把栈顶结点的地址赋给p,top下移,删除结点pif(top==NULL) throw"栈空!\n";Node *p=top;float x=top->data;top=top->next;delete p;return x;}float LinkedStack::getTop(){if(top==NULL) throw"栈空!\n";return top->data;}bool LinkedStack::isEmpty(){if(top==NULL) return true;else return false;}int main(){try{LinkedStack l;try{cout<<"将元素8.2入栈;\n";l.push(8.2);}catch(char *p){cout<
运行结果:

3.循环队列
#includeusing namespace std;const int MAX_SIZE=100;class CirQueue{private:int front,rear;float data[MAX_SIZE];public:CirQueue();void enQueue(float x);float deQueue();float getQueue();bool isEmpty();};CirQueue::CirQueue(){rear=front=MAX_SIZE-1;}void CirQueue::enQueue(float x){  //尾指针rear前移,把入队元素x赋给rear指向的位置if((rear+1)%MAX_SIZE==front) throw"上溢!\n";rear=(rear+1)%MAX_SIZE;data[rear]=x;}float CirQueue::deQueue(){  //头指针front前移,把front指向位置的元素值返回if(rear==front) throw"队空!\n";front=(front+1)%MAX_SIZE;return data[front];}float CirQueue::getQueue(){if(rear==front) throw"队空!\n";return data[(front+1)%MAX_SIZE];}bool CirQueue::isEmpty(){if(rear==front) return true;else return false;}int main(){try{CirQueue c;try{cout<<"将元素6.6入队;\n";c.enQueue(6.6);}catch(char *p){cout<
运行结果:

4.链队列
#includeusing namespace std;struct Node{float data;Node *next;};class LinkedQueue{private:Node *front,*rear;public:LinkedQueue();~LinkedQueue();void enQueue(float x);float deQueue();float getQueue();bool isEmpty();};LinkedQueue::LinkedQueue(){front=new Node;front->next=NULL;rear=front;}LinkedQueue::~LinkedQueue(){Node *p;while(front!=NULL){p=front;front=p->next;delete p;}}void LinkedQueue::enQueue(float x){  //新建一个结点,其数据域存放入队元素x,指针域置空,把其地址赋给rear,rear下移Node *s=new Node;s->data=x;s->next=NULL;rear->next=s;rear=s;}float LinkedQueue::deQueue(){  //把队头结点地址赋给p,头指针front下移,返回p结点的数据元素,delete pif(front==rear) throw"队空!\n";Node *p=front->next;front->next=p->next;float x=p->data;if(p->next==NULL) rear=front;delete p;return x;}float LinkedQueue::getQueue(){if(front==rear) throw"队空!\n";return front->next->data;}bool LinkedQueue::isEmpty(){if(front==rear) return true;else return false;}int main(){try{LinkedQueue l;try{cout<<"将元素2.2入队;\n";l.enQueue(2.2);}catch(char *p){cout<
运行结果:

5.将十进制数转换成二进制数
#includeusing namespace std;struct Node{int data;Node *next;};class TransStack{private:Node *top;public:TransStack(){top=NULL;}~TransStack(){}void push(int a){Node *s=new Node;s->data=a;s->next=top;top=s;}int pop(){if(top==NULL) throw"\n\t\t下溢!\n\t\t";Node *p=top;int x=p->data;top=p->next;delete p;return x;}};int main(){int n,a,b;cout<<"\n\t\t请输入一个十进制的整数:";cin>>n;a=n;TransStack t;for(;;){b=a%2;  //取余数a=a/2;  //取商t.push(b);  //余数依次入栈if(!a) break;}try{cout<<"\n\t\t"<
运行结果:
 
6.排号叫号管理
#includeusing namespace std;struct Node{int data;Node *next;};class QueueOfCanteen{private:Node *front,*rear;int total;   //计算当前排队人数,并作为入队单号的参考public:QueueOfCanteen();~QueueOfCanteen();int getTotal();void enQueue(int x);int deQueue();int getQueue();};QueueOfCanteen::QueueOfCanteen(){total=0;front=new Node;front->next=NULL;rear=front;}QueueOfCanteen::~QueueOfCanteen(){Node *p;rear=NULL;while(front!=NULL){p=front;front=p->next;delete p;}}int QueueOfCanteen::getTotal(){return total;}void QueueOfCanteen::enQueue(int x){Node *s=new Node;s->data=x;s->next=NULL;rear->next=s;rear=s;total++;}int QueueOfCanteen::deQueue(){if(front==rear) throw"无\n";Node *p=front->next;front->next=p->next;int x=p->data;if(p->next==NULL) rear=front;delete p;total--;return x;}int QueueOfCanteen::getQueue(){if(front==rear) throw"无\n";return front->next->data;}int queue(QueueOfCanteen q){int queue(QueueOfCanteen q);cout<<"\n\t\t===============饭堂排号叫号管理================\n\n";cout<<"\t\t\t当前队列人数:"<>s;if(q.getTotal()!=0&&(s=='y'||s=='Y')){  //队列人数不为0,下一个则队头出队try{q.deQueue();}catch(...){}system("cls");queue(q);}if(s=='n'||s=='N') return 0;return 0;}int menu(QueueOfCanteen q){int a;system("cls");cout<<"\n\t\t===============饭堂排号叫号管理================\n\n";cout<<"\t\t\t1.入队打印单号\n";cout<<"\t\t\t2.叫号\n";cout<<"\t\t\t3.退出\n";cout<<"\n\t\t\t请选择:";cin>>a;system("cls");switch(a){case 1:q.enQueue(q.getTotal()+1);   //取得单号total+1入队cout<<"\n\t\t\t单号:"<>c;if(c=='y'||c=='Y'){menu(q);}if(c=='n'||c=='N') return 0;return 0;}int main(){try{QueueOfCanteen q;menu(q);}catch(char *p){cout<
运行结果:




退出系统时出现以下异常(找不出原因):

四、实验心得
  基本掌握了栈和队列的基本操作,但是实践时依然很多bug。

阅读全文
1 0
原创粉丝点击