入队/出队操作
#include<iostream> using namespace std; struct node { int data; node *next; }; struct queue { node *head; node *rear; }; //入队 queue *push_queue(queue *team,int x) { node *s=new node; s->data=x; s->next=NULL; if(team->rear==NULL) { team->head=s; team->rear=s; } else { team->rear->next=s; team->rear=s; } return team; } //出队 queue *pop_queue(queue *team) { int x; node *s=new node; if(team->head==NULL) { cout<<"empty"<<endl; } else { x=team->head->data; s=team->head; if(team->head==team->rear) { team->head=NULL; team->rear=NULL; } else { team->head=team->head->next; delete s; } cout<<x<<endl; return team; } } int main() { queue *H=new queue; H->rear=NULL; H->head=NULL;//建一个空队 cout<<"Input the data pushed to queue"<<endl; int x;char c; queue *p; while(cin>>x) { p=push_queue(H,x); cin.get(c); if(c==‘\n‘)break; } while(p->rear!=NULL) { p=pop_queue(p); } return 0; }
时间: 2024-10-12 16:31:02