1. 程式人生 > >數據結構 鏈表隊列

數據結構 鏈表隊列

spa style 等於 存儲 ext != 元素 include ont

 1 #include"stdio.h"
 2 #include"stdlib.h"
 3 typedef int DataType;
 4 typedef struct Seq
 5 {
 6     DataType data;//數據存儲 
 7     struct Seq *next;//指向下一個節點的指針 
 8 }SeqList;
 9 typedef struct
10 {
11     SeqList *front,*rear;//定義頭尾指針 
12 }Link;
13 void initQueue(Link *s)
14 {
15     s->front = s->rear = NULL;
16 } 17 //進隊 18 void inQueue(Link *s, DataType x) 19 { 20 printf("222"); 21 SeqList *p = (SeqList*)malloc(sizeof(SeqList));//創建一個新節點 22 p->data = x; 23 p->next = NULL; 24 if(s->front == NULL) s->front = p;//判斷該隊列中是否有元素 25 else s->rear->next = p; 26
s->rear = p; 27 } 28 //出隊 29 void outQueue(Link *s, DataType *x) 30 { 31 if(s->front == NULL) 32 { 33 printf("該隊列為空"); 34 } 35 else 36 { 37 SeqList *p = s->front; 38 *x = p->data; 39 s->front = s->front->next;//將隊頭移動到隊頭的下一個
40 if(s->front ==NULL) s->rear=NULL;//如果隊頭等於空的話那隊尾也就等於空 41 free(p); 42 } 43 } 44 void printQueue(Link *s) 45 { 46 SeqList *p = s->front; 47 if(s->front == NULL) printf("該隊列為空"); 48 else 49 { 50 while(p!=NULL) 51 { 52 printf("%d\n",p->data); 53 p = p->next; 54 } 55 } 56 } 57 main() 58 { 59 Link *s; 60 printf("ddd"); 61 s->front = NULL; 62 s->rear = NULL; 63 inQueue(s,1); 64 inQueue(s,2); 65 inQueue(s,3); 66 printQueue(s); 67 68 }

數據結構 鏈表隊列