c語言實現隊列的基本操作


話不多說,直接代碼

  1 #include"stdio.h"
  2 #include"stdlib.h"
  3 typedef struct QNode{
  4     int date;
  5     struct QNode *next;
  6 }QNode,*QueuePtr;
  7 typedef struct{
  8     QueuePtr front;
  9     QueuePtr rear;
 10 }LinkQueue;
 11 //初始化
 12 int InitStack(LinkQueue &S){
 13     S.front=(QueuePtr)malloc(sizeof(QNode));
 14     //S.front=NULL;
 15     S.rear=S.front;
 16     if(!S.front)
 17         return 0;
 18     S.front->next=NULL;
 19     return 1;
 20 }
 21 //
 22 int EnQueue(LinkQueue &S,int e){
 23     QueuePtr p=NULL;
 24     p=(QueuePtr)malloc(sizeof(QNode));
 25     if(!p)exit(1);
 26     p->date=e;
 27     p->next=0;
 28     S.rear->next=p;
 29     S.rear=p;
 30     return 1;
 31 }
 32 //置空
 33 void ClearQueue(LinkQueue &S){
 34     QueuePtr p=NULL;
 35     p=S.front;
 36     while(S.front!=S.rear){
 37         p=S.front->next;
 38         free(S.front);
 39         S.front=p;
 40     }    
 41 }
 42 //判空
 43 void QueueEmpty(LinkQueue &S){
 44     if(S.front==S.rear)
 45         printf("判空:是\n");
 46     else 
 47         printf("判空:否\n");
 48     
 49 }
 50 //長度
 51 int   QueueLen(LinkQueue &S){
 52     QueuePtr p=NULL;
 53     int len=0;
 54     p=S.front;
 55     if(S.front==S.rear)
 56         return len;
 57     else{
 58         while(p!=S.rear){
 59             len++;
 60             p=p->next;
 61         }
 62         return len;
 63     }
 64 }
 65 void len(LinkQueue &S){
 66 printf("%d\n",S.front);
 67 printf("%d\n",S.front->next);
 68 printf("%d\n",S.rear);
 69 printf("大小%d\n",sizeof(QNode));
 70 printf("%d\n",S.rear-S.front->next);
 71 printf("%d\n",S.rear-S.front);
 72 printf("%d\n",(S.rear-S.front->next)/sizeof(QNode));
 73 
 74 }
 75 //
 76 int pop(LinkQueue &S){
 77     int tem=0;
 78     QueuePtr p=NULL;
 79     if(S.front->next==NULL)
 80         return 0;
 81     else{
 82         tem=S.front->next->date;
 83         p=S.front->next;
 84         free(S.front);
 85         S.front=p;
 86         return tem;
 87     }
 88 }
 89 //輸出
 90 void QueueTraverse(LinkQueue &S){
 91     printf("輸出:");
 92     QueuePtr p=S.front;
 93     while(p!=S.rear){
 94         printf("%d   ",p->next->date);
 95         p=p->next;
 96     }
 97     
 98 }
 99 void main(){
100     LinkQueue S;
101     printf("初始化:");
102     printf("%d\n",InitStack(S));
103     printf("%d\n",EnQueue(S,1));
104     printf("%d\n",EnQueue(S,2));
105     printf("%d\n",EnQueue(S,3));
106     printf("%d\n",EnQueue(S,4));
107     printf("%d\n",EnQueue(S,5));
108     printf("長度:%d\n",QueueLen(S));
109     len(S);
110     QueueTraverse(S);
111     printf("輸出:%d\n",pop(S));
112     QueueTraverse(S);
113     EnQueue(S,9);
114     QueueTraverse(S);
115     QueueEmpty(S);
116     ClearQueue(S);
117     QueueEmpty(S);
118 }

在使用構造體的時候注意應該有兩個構造體,分別對應整個鏈表和鏈表的一個結點。

在獲取隊列長度的時候不能像棧一樣隊首隊尾直接相減,具體我也搞不懂為什么,代碼中的len函數就是對該方法的測試。

另外,一定在。要注意S.front並不是第一個元素的位置,S.front->next才是,見圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM