数据结构 链表队列

 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 }

猜你喜欢

转载自www.cnblogs.com/sucker/p/10817270.html