链队列的建立、判空、入队、出队、求长、访头、清空和销毁

来源:互联网 发布:内外网络切换软件 编辑:程序博客网 时间:2024/04/29 08:16
队列是从屁股进去 从头出来!
一:是不是Q.front只是记录队列的头结点的地址,里面没有data,Q.rear的是记录队列的末节点地址,里面有data? 二:
 Qnode *p; p=(QNode*)malloc(sizeof(QNode)); if(p==NULL) exit(OVERFLOW); p->next=NULL p->data=e; Q.rear->next=p; Q.rear=p;
同学你好:我看了你的问题,你问的两个问题的解析在下面,希望能采纳一 :你说的第一个问题是对的:是不是Q.front只是记录队列的头结点的地址,里面没有data,Q.rear的是记录队列的末节点地址,里面有data? 解析:是这样的,不管是数组形式的循环队列还是链式的队列,front和rear只是存放对头和队未的地址。数组时头尾的下表,链式的队列是地址。总之都是地址。 二:这段程序中Q.rear->next=p是不是多余的?去掉的话会有什么影响?解析:你的说不对。首先,要将一个新的节点入队,就要Q.rear->next = p;这是入队操作。简单点说就是将p挂在rear后面,然后让原来的rear指向新的p;也就是接下来的一步:Q.rear = p;如果没有这步就没有实现将新节点p入队。所以这步是必须的,不是多余的。
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. typedef struct node  
  5. {  
  6.     int data;  
  7.     struct node *next;  
  8. }Node;  
  9.   
  10. //头指针和尾指针  
  11. typedef struct  
  12. {  
  13.     Node *front;  
  14.     Node *rear;  
  15. }Queue;  
  16.   
  17. void initQueue(Queue &Q)  
  18. {  
  19.     Q.front = Q.rear = new Node;  
  20.     Q.front->next = NULL;  
  21. }  
  22.   
  23. bool isEmptyQueue(Queue &Q)  
  24. {  
  25.     if(NULL == Q.front) //此时链队列被销毁  
  26.     {  
  27.         cout << "链队列不存在." << endl;  
  28.         exit(1);  
  29.     }  
  30.   
  31.     if(Q.rear == Q.front)//此时链队列为空  
  32.         return true;  
  33.   
  34.     return false;  
  35. }  
  36.   
  37. void enterQueue(Queue &Q, int element)  
  38. {  
  39.     Node *p = new Node;  
  40.     p->next = NULL;  
  41.     p->data = element;  
  42.     Q.rear->next = p;  
  43.     Q.rear = p;  
  44. }  
  45.   
  46. int deQueue(Queue &Q)  
  47. {  
  48.     int outElement;  
  49.     if(isEmptyQueue(Q))  
  50.         exit(1);  
  51.   
  52.     Node *p = Q.front->next;  
  53.     outElement = p->data;  
  54.   
  55.     Q.front->next = p->next;  
  56.     if(Q.rear == p)  //需要特殊处理  
  57.         Q.rear = Q.front;  
  58.     delete p;  
  59.   
  60.     return outElement;  
  61. }  
  62.   
  63. int getLength(Queue &Q)  
  64. {  
  65.     Node *p1 = Q.front;  
  66.     Node *p2 = Q.rear;  
  67.     int length = 0;  
  68.     while(p1 != p2)  
  69.     {  
  70.         p1 = p1->next;  
  71.         length++;  
  72.     }  
  73.   
  74.     return length;  
  75. }  
  76.   
  77. int getHead(Queue &Q)  
  78. {  
  79.     if(isEmptyQueue(Q))  
  80.         exit(1);  
  81.   
  82.     return Q.front->next->data;  
  83. }  
  84.   
  85. void clearQueue(Queue &Q)  
  86. {  
  87.     while(!isEmptyQueue(Q))  
  88.         deQueue(Q);  
  89. }  
  90.   
  91. void destroyQueue(Queue &Q)  
  92. {  
  93.     clearQueue(Q);  
  94.     delete Q.front;  
  95.     Q.front = Q.rear = NULL;  
  96. }  
  97.   
  98. int main()  
  99. {  
  100.     Queue Q;  
  101.     initQueue(Q);  
  102.     cout << getLength(Q) << endl;  
  103.       
  104.     int i;  
  105.     for(i = 1; i <= 10; i++)  
  106.         enterQueue(Q, i);  
  107.     cout << getHead(Q) << endl;  
  108.     cout << getLength(Q) << endl;  
  109.   
  110.     while(!isEmptyQueue(Q))  
  111.         cout << deQueue(Q) << " ";  
  112.     cout << endl;  
  113.   
  114.     if(isEmptyQueue(Q))  
  115.         cout << "yes" << endl;  
  116.     else   
  117.         cout << "no" << endl;  
  118.       
  119.     destroyQueue(Q);  
  120.     if(isEmptyQueue(Q))  
  121.         cout << "yes" << endl;  
  122.     else   
  123.         cout << "no" << endl;  
  124.   
  125.     return 0;  
  126. }  
                                             
0 0
原创粉丝点击