数据结构 链表队列

 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 }

 

转载于:https://www.cnblogs.com/sucker/p/10817270.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值