c語言編程之循環隊列

        利用鏈表實現的循環隊列,完成了隊列的入隊和出隊,對於隊空和隊滿用了一個flag進行標記。入隊flag++,出隊flag--node

  1 #include"stdio.h"
  2 
  3 typedef int element;
  4 
  5 typedef struct Node{
  6         struct Node *next;
  7         element data;
  8 }*pNode;
  9 
 10 typedef struct QNode{
 11         pNode front,rear;
 12         element flag;
 13 }*Linknode;
 14 
 15 //init a empty queue
 16 element Init_queue(Linknode *pLinknode)
 17         {
 18            Linknode P;
 19            P=*pLinknode;
 20            P->front=P->rear=(pNode)malloc(sizeof(struct Node));
 21            P->front->next=NULL;
 22            P->rear->next=P->front;     // make loop queue font linked rear,in usuall queue this is P->rear->next=NULL;make a change become 
//Loop queue;
23 P->flag=0; 24 } 25 26 //add a data to queue rear 27 element Add_queue(Linknode *pLinknode,int num) 28 { 29 Linknode P; 30 P=*pLinknode; 31 pNode s=(pNode)malloc(sizeof(struct Node)); 32 s->data=num; 33 s->next=P->front; //in this position we can see the new add memory s is point to front,in usuall queue we can see s->next=NULL;
 34           P->rear->next=s;
 35           P->rear=s;                   
 36           ++P->flag;
 37           printf("add data:%d add flag:%d\n",P->rear->data,P->flag);
 38         }
 42         {
 43           if((*pLinknode)->flag==0)
 44                 {
 45                   printf("queue is empty\n");
 46                   return 0;
 47                 }
 48           //if(((*pLinknode)->front)==((*pLinknode)->rear))
 49           //    {
 50           //            printf("queue is empty!!");
 51           //            return 0;
 52           //      }
 53           Linknode P;
 54           pNode k;
 55           P=*pLinknode;
 56           k=P->front->next;
 57           P->front->next=k->next;
 58           printf("delet data:%d\n",k->data);
 59           //--P->flag;
 60           free(k);
 61           printf("delete flag:%d\n",P->flag);
 62           --P->flag;
 63           return 0;
 64         }
 65 
 66 element Print_queue(Linknode pLinknode)
 67         {
 68           pNode p;
 69           p=pLinknode->front;
 71           while(p!=pLinknode->rear&&pLinknode->flag)    //it is importand,because in loop queue fornt==rear can means empty or full,it is a                                                  //loop,we can't determin this loop queue is empty by using front==rear,must add num as flag;
 72 { 73                   p=p->next; 74                   printf("data:%d\n",p->data); 75 } 76           return 0; 77 } 78 element main() 79 { 80 Linknode pQnode; 81           pQnode=(Linknode)malloc(sizeof(struct QNode)); 82           Init_queue(&pQnode); 83           Add_queue(&pQnode,99); 84           Add_queue(&pQnode,11); 85           Add_queue(&pQnode,22); 86           Add_queue(&pQnode,33); 87           Add_queue(&pQnode,44); 88           Add_queue(&pQnode,55); 89 Print_queue(pQnode); 90           Delet_queue(&pQnode); 91           Delet_queue(&pQnode); 92           Delet_queue(&pQnode); 93           Delet_queue(&pQnode); 94           Delet_queue(&pQnode); 95           Delet_queue(&pQnode); 96           Delet_queue(&pQnode); 97 Print_queue(pQnode); 98           return 0; 99 } 100 
相關文章
相關標籤/搜索