数据结构 链表队列

 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; 
12     SeqList *rear;//定义头尾指针 
13 }Link;
14 void initQueue(Link *s)
15 {
16 
17     s->front = s->rear = (SeqList*)malloc(sizeof(SeqList));
18 }
19 //进队 
20 void inQueue(Link *s, DataType x)
21 {
22     SeqList *p = (SeqList*)malloc(sizeof(SeqList));//创建一个新节点 
23     p->data = x;
24     p->next = NULL;
25     if(s->front == NULL) s->front = p;//判断该队列中是否有元素 
26     else s->rear->next = p;    
27     s->rear = p;
28 } 
29 //出队
30 void outQueue(Link *s, DataType *x)
31 {
32     if(s->front == NULL) 
33     {
34         printf("该队列为空");
35     }
36     else
37     {
38         SeqList *p = s->front;
39         *x = p->data;
40         s->front = s->front->next;//将队头移动到队头的下一个 
41         if(s->front ==NULL) s->rear=NULL;//如果队头等于空的话那队尾也就等于空 
42         free(p);
43     }
44 } 
45 void printQueue(Link *s)
46 {
47     SeqList *p = s->front;
48     if(s->front == NULL) printf("该队列为空");
49     else
50     {
51         while(p!=NULL)
52         {
53             printf("%d\n",p->data);
54             p = p->next;
55         }
56     }
57 }
58 main()
59 {
60     Link s;
61     initQueue(&s);
62     s.front = NULL;
63     s.rear = NULL;
64     inQueue(&s,1);
65     inQueue(&s,2);
66     inQueue(&s,3);
67     printQueue(&s);
68     
69 }

 

posted @ 2019-05-05 23:57  oops_w  阅读(119)  评论(0编辑  收藏  举报