数据结构(十) 链队列的基本操作 ----入队列,出队列,判断队列是否为空

来源:互联网 发布:锁机软件下载 编辑:程序博客网 时间:2024/06/04 17:41
//链队列的基本操作 #include <iostream>using namespace std;//节点的结构体struct Node{int data;struct Node *next;};//队列的结构体struct queue{struct Node * front;struct Node *rear;}; //队列的初始化操作void initQueue(struct queue &Q){Q.front = new Node();Q.rear = Q.front;Q.front->next = NULL;}//队列的入队操作void enQueue(struct queue &Q){struct Node *p;int data;cout<<"请输入入队列的数据:\n";cin>>data;p = new Node();p->data = data;p->next = NULL;Q.rear->next= p;Q.rear=p;} //队列的出队操作void DeQueue(struct queue &Q){struct Node *p;p = Q.front->next;cout<<"队列的数据为:\n";cout<<p->data;if(p==Q.rear){Q.front=Q.rear;}else{Q.front->next = p->next;}delete(p);} //判断队列是否为空void isEmpty(struct queue Q){if(Q.front==Q.rear){cout<<"对列为空\n";exit(1);}} int main(){struct queue Q;initQueue(Q);enQueue(Q);DeQueue(Q);isEmpty(Q);return 0;}

0 0