数据结构C语言实现—队列操作

  1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef int elemType;
5 /************************************************************************/
6 /* 以下是关于队列链接存储操作的6种算法 */
7 /************************************************************************/
8
9 struct sNode{
10 elemType data; /* 值域 */
11 struct sNode *next; /* 链接指针 */
12 };
13
14 struct queueLK{
15 struct sNode *front; /* 队首指针 */
16 struct sNode *rear; /* 队尾指针 */
17 };
18
19 /* 1.初始化链队 */
20 void initQueue(struct queueLK *hq)
21 {
22 hq->front = hq->rear = NULL; /* 把队首和队尾指针置空 */
23 return;
24 }
25
26 /* 2.向链队中插入一个元素x */
27 void enQueue(struct queueLK *hq, elemType x)
28 {
29 /* 得到一个由newP指针所指向的新结点 */
30 struct sNode *newP;
31 newP = malloc(sizeof(struct sNode));
32 if(newP == NULL){
33 printf("内存空间分配失败! ");
34 exit( 1 );
35 }
36
37 /* 把x的值赋给新结点的值域,把新结点的指针域置空 */
38 newP->data = x;
39 newP->next = NULL;
40
41 /* 若链队为空,则新结点即是队首结点又是队尾结点 */
42 if(hq->rear == NULL){
43 hq->front = hq->rear = newP;
44 }else {
45 /* 若链队非空,则依次修改队尾结点的指针域和队尾指针,使之指向新的队尾结点 */
46 hq->rear = hq->rear->next = newP; /* 注意赋值顺序哦 */
47 }
48 return;
49 }
50
51 /* 3.从队列中删除一个元素 */
52 elemType outQueue(struct queueLK *hq)
53 {
54 struct sNode *p;
55 elemType temp;
56
57 /* 若链队为空则停止运行 */
58 if(hq->front == NULL){
59 printf("队列为空,无法删除! ");
60 exit(1);
61 }
62 temp = hq->front->data; /* 暂存队尾元素以便返回 */
63 p = hq->front; /* 暂存队尾指针以便回收队尾结点 */
64 hq->front = p->next; /* 使队首指针指向下一个结点 */
65
66 /* 若删除后链队为空,则需同时使队尾指针为空 */
67 if (hq->front == NULL){
68 hq->rear = NULL;
69 }
70
71 free(p); /* 回收原队首结点 */
72 return temp; /* 返回被删除的队首元素值 */
73 }
74
75 /* 4.读取队首元素 */
76 elemType peekQueue(struct queueLK *hq)
77 {
78 /* 若链队为空则停止运行 */
79 if(hq->front == NULL){
80 printf("队列为空,无法删除! ");
81 exit(1);
82 }
83
84 return hq->front->data; /* 返回队首元素 */
85 }
86
87 /* 5.检查链队是否为空,若为空则返回1, 否则返回0 */
88 int emptyQueue(struct queueLK *hq)
89 {
90 /* 判断队首或队尾任一个指针是否为空即可 */
91 if(hq->front == NULL){
92 return 1;
93 }else{
94 return 0;
95 }
96 }
97
98 /* 6.清除链队中的任何元素 */
99 void clearQueue(struct queueLK *hq)
100 {
101 struct sNode *p = hq->front; /* 队首指针赋给p */
102 /* 依次删除队列中的每一个结点,最后使队首指针为空 */
103 while(p != NULL){
104 hq->front = hq->front->next;
105 free(p);
106 p = hq->front;
107 } /* 循环结束后队首指针已为空 */
108
109 hq->rear = NULL; /* 置队尾指针为空 */
110 return;
111 }
112
113 /************************************************************************/
114
115 int main(int argc, char* argv[])
116 {
117 struct queueLK q;
118 int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
119 int i;
120
121 initQueue(&q);
122 for(i = 0; i < 8; i ){
123 enQueue(&q, a[i]);
124 }
125 printf("%d ", outQueue(&q));
126 printf("%d ", outQueue(&q));
127
128 enQueue(&q, 68);
129 printf("%d ", peekQueue(&q));
130 printf("%d ", outQueue(&q));
131
132 while(!emptyQueue(&q)){
133 printf("%d ", outQueue(&q));
134 }
135
136 printf("");
137 clearQueue( &q);
138 system("pause");
139
140 return 0;
141 }

  


转载于:https://www.cnblogs.com/smalltigerlee/archive/2011/10/26/2224944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值