JBTALKS.CC

标题: C program Circular Queue [打印本页]

作者: 狂天使    时间: 2010-12-26 11:56 PM
标题: C program Circular Queue
各位大大幫忙一下,
我要寫一個 Circular Queue 我的問題是: 一開始 insert, 然後 insert 到 full 為之是沒問題,過後我delete 再 insert 就有問題了。

先謝謝各位。。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 5

  4. struct element
  5. {
  6.        int key ;
  7. };

  8. struct MyCircularQueue{
  9.         
  10.     struct element items[MAX] ;               
  11.     int rear ;                                                
  12.     int front ;
  13.     int count;
  14.     bool IsFullQ( )        ;                                       
  15.     bool IsEmptyQ( ) ;                                       
  16.     void AddQ( struct element ) ;               
  17.     struct element DeleteQ( ) ;                        
  18.     void ShowQ( ) ;                                       
  19. } ;

  20. bool MyCircularQueue::IsFullQ()
  21. {
  22.          return rear-front==MAX;
  23. }

  24. bool MyCircularQueue::IsEmptyQ()
  25. {
  26.    
  27.         return front==rear;
  28. }

  29. void MyCircularQueue::AddQ(struct element elements)
  30. {
  31.    
  32.      rear=(rear+1) % MAX;
  33.      items[rear]=elements;
  34. }

  35. struct element MyCircularQueue::DeleteQ()
  36. {
  37.      front=(front+1) % MAX;
  38.      return items[front];

  39. }

  40. void MyCircularQueue::ShowQ()
  41. {
  42.     int flag=front;
  43.     for(;flag<rear;)
  44.     printf("Queue elements: %d\n",items[++flag]);
  45. }
  46. int main()
  47. {
  48.     int choice,stop=0;
  49.     struct MyCircularQueue my_queue;
  50.     struct element elements;
  51.     my_queue.front=-1;
  52.     my_queue.rear=-1;
  53.     while(stop!=1)
  54.     {
  55.         printf("1)Check Queue is it Full\n");
  56.         printf("2)Check Queue is it Empty\n");
  57.         printf("3)Insert element into Queue\n");
  58.         printf("4)Delete element from Queue\n");
  59.         printf("5)Print out all element of Queue\n");
  60.         printf("6)Exit\n");
  61.         printf("Enter your choice:");
  62.         scanf("%d",&choice);
  63.         
  64.         switch(choice)
  65.         {
  66.            case 1:
  67.                 if(my_queue.IsFullQ())
  68.                   printf("\nQueue is full\n\n");
  69.                 else
  70.                   printf("\nQueue not full still can input %d elements\n\n",MAX-1-my_queue.rear);
  71.                 break;
  72.            case 2:
  73.                  if(my_queue.IsEmptyQ())
  74.                   printf("\nQueue is Empty\n\n");
  75.                 else
  76.                   printf("\nGot %d elements in Queue\n\n",my_queue.rear-my_queue.front);
  77.                 break;
  78.            case 3:
  79.                 if(!my_queue.IsFullQ()){
  80.                   printf("\nInsert the elements:");
  81.                   scanf("%d",&(elements.key));
  82.                   my_queue.AddQ(elements);
  83.                 }else
  84.                   printf("\ncannot add any more...\n\n");
  85.                 break;
  86.            case 4:
  87.                 if(!my_queue.IsEmptyQ()){
  88.                   my_queue.DeleteQ();
  89.                 }else
  90.                   printf("\nqueue already empty\n\n");
  91.                 break;
  92.            case 5:
  93.                 my_queue.ShowQ();
  94.                 break;
  95.            case 6:
  96.                 stop++;
  97.                 system("cls");
  98.                 break;
  99.         }
  100.     }
  101. }
复制代码

作者: Super-Tomato    时间: 2010-12-27 12:30 AM
因為你的 Array 没有在 delete 的時候重新排好記憶體分配的位置,你可以在 delete 之後执行 showQ 並列出你的 flag 看看就會明白




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5