入队的实现:rear->next=e; rear=e; //修改尾指针位置
出队的实现:t=front; front=front->next; if(front==NULL); rear=NULL;delete t;
#include <bits/stdc++.h>
using namespace std;
struct Queue{ //队列结点
int data;
struct Queue *next;
};
Queue *front = NULL; //队头
Queue *rear = NULL; //队尾
void add(int value){ //入队
Queue *e = new Queue;
e->data = value;
e->next = NULL;
if(front == NULL){ //队列第一个元素
front = e;
}else{
rear->next = e;
}
rear = e;
}
void del(){ //出队
Queue *t;
//如果队列有元素
if(front != NULL){
cout<<front->data<<"出队"<<endl;
t = front;
front = front->next;
if(front == NULL) rear = NULL;
delete t;
}else{
cout<<"队列空"<<endl;
}
}
void display(){ //显示队
Queue *p = front;
while(p != NULL){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main(){
int order,x;
cout<<"输入指令:"<<endl;
while(1 == 1){
cout<<"1:入队,2:出队,3:显示队!"<<endl;
cin>>order;
if(order == 1){
cin>>x;
add(x);
display();
}else if(order == 2){
del();
display();
}else if(order == 3){
display();
}
}
return 0;
}