C语言版实现链队列

时间:2022-05-08 06:23:57

本文实例为大家分享了C语言实现链队列的具体代码,供大家参考,具体内容如下

源文件部分:  指针没学好的同学很难看懂^_^,有点乱,希望对大家有点帮助。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef int Elemtype;
#include"LQueue.h"
int main()
{
 Deque head;
 instruction(head);
 return 0;
}
头文件部分:
typedef struct Queue
{
 Elemtype data;
 struct Queue *next;
}LQnode,*LQueue;
 
typedef struct
{
 LQnode *front;
 LQnode *rear;
}Deque;
 
void Init_queue(Deque *head)  //初始化+清空操作==其实这里的清空是指将头节点后的节点给丢弃掉
{
 LQnode *p=NULL;
 p=(LQueue)malloc(sizeof(LQnode));
 head->front=p;
 head->rear=p;
 p->next=NULL;
}
 
int Empty_queue(Deque *head)      //判空
{
 if(head->front->next==head->rear->next)
 return 1;
 return 0;
}
 
int Lenght_queue(Deque arrow)
{
 LQnode *p=NULL;
 int len=0;
 p=arrow.front->next;
 while(p)
 {
 len++;
 p=p->next;
 }
 return len;
}
 
void Enqueue(Deque *arrow,Elemtype e)    //入队操作
{
 LQueue p=NULL;
 p=(LQueue)malloc(sizeof(LQnode));
 if(!p)
 {
 printf("已无更多的内存单元得到分配!\n");
 return ;
 }
 p->data=e;
 p->next=NULL;         //插入时,队首指针是不需要动的
 arrow->rear->next=p;
 arrow->rear=p;
 return ;
}
 
void Dequeue(Deque *arrow,Elemtype *e)    //出队操作
{
 LQnode *p=NULL;
 if(Empty_queue(arrow))
 {
 printf("当前链队列为空,无法完成出队操作!!!\n");
 return ;
 }
 p=arrow->front->next;
 (*e)=p->data;
 arrow->front->next=p->next;
 printf("元素%d已退出队列!!!\n",*e);
 if(Lenght_queue(*arrow)==0)
 return ;            //当最后一个元素出列以后,arrow->rear不知道指向了哪里  
 free(p);
 return ;
}
 
int Queue_top(Deque *arrow)  //返回队首元素
{
 if(Empty_queue(arrow))
 {
 printf("当前链队列为空,队首元素不存在!!!\n");
 return 0;
 }
 printf("当前队首元素是:%d\n",arrow->front->next->data);
}
 
void Destroy_queue(Deque *arrow)  //链队列的销毁
{
 LQnode *p=NULL;
 if(Empty_queue(arrow))
 {
 printf("当前链队列为空,无须完成销毁操作!!!\n");
 return ;
 }
 while(arrow->front->next)
 {
 p=arrow->front->next;
 arrow->front->next=p->next;
 if(Lenght_queue(*arrow)==0)
  break;  
 free(p);
 }
 printf("销毁成功!\n");
 return ;
}
 
void Print_queue(Deque arrow)
{
 LQnode *p=NULL;
 p=arrow.front->next;
 while(p)
 {
 printf("%d ",p->data);
 p=p->next;
 }
 printf("\n");
}
 
void Modify_queue(Deque *arrow,Elemtype index,Elemtype e)  //修改函数
{
 int i=0;
 LQnode *p=NULL;
 p=arrow->front->next;
 while(i<index-1)
 {
 p=p->next;
 }
 p->data=e;
 printf("已完成修改操作!\n");
}
 
int Insearch_queue(Deque arrow,Elemtype e)      //查找函数
{
 LQnode *p=NULL;
 int i=1;
 if(Empty_queue(&arrow))
 {
 printf("当前链队列为空,没有元素可查找!!!\n");
 return 0;
 }
 p=arrow.front->next;
 while(p!=NULL)
 {
 if(e==p->data)
 {
  return i;
  break;
 }
 i++;
 p=p->next;
 }
 if(p==NULL)
 printf("查找失败,队列内无该元素存在!\n");
 return 0;
}
 
void instruction(Deque head)
{
 int n,m,t,a,b,len1,index;
 printf("\t\t1、队列初始化 \n");
 printf("\t\t2、新增队列元素\n");
 printf("\t\t3、返回队首元素\n");
 printf("\t\t4、元素出队列 \n");
 printf("\t\t5、查找队列元素\n");
 printf("\t\t6、修改队列元素\n");
 printf("\t\t7、销毁队列  \n");
 printf("\t\t8、队列的长度 \n");
 printf("\t\t9、打印队列元素\n");
 printf("\t\t10、退出程序  \n");
 printf("请输入你所需要完成的指令:\n");
 do{
 scanf("%d",&n);
 if(n<1||n>10)
  printf("对不起,你输入的指令编号是无效的,请重新输入!!!\n");
 }while(n<1||n>10);
 switch(n)
 {
 case 1:
  Init_queue(&head);
  printf("已完成链队列初始化,请输入你要添加的元素个数!\n");
  scanf("%d",&n);
  while(n--)
  {
  int x;
  scanf("%d",&x);
  Enqueue(&head,x);
  }
  printf("完成建队操作!\n");
  break;
 case 2:
  printf("请输入你要添加的元素个数!\n");
  scanf("%d",&n);
  while(n--)
  {
  int x;
  scanf("%d",&x);
  Enqueue(&head,x);
  }
  printf("增添成功!\n");
  break;
 case 3:
  Queue_top(&head);
  break;
 case 4:
  Dequeue(&head,&t);
  break;
 case 5:
  printf("请输入你所要查找的元素:\n");
  scanf("%d",&m);
  index=Insearch_queue(head,m);
  if(index)
  printf("你所要查找的元素位于队列的第%d个位置上!!!\n",index);
  break;
 case 6:
  printf("请输入你更改的元素队列位置:\n");
  do{
  scanf("%d",&a);
  if(a<1||a>Lenght_queue(head))
   printf("对不起,你所输入的元素位置不在区域内,请重新输入!!!\n");
  }while(a<1||a>Lenght_queue(head));
  printf("请输入修改后的值:\n");
  scanf("%d",&b);
  Modify_queue(&head,a,b);
  break;
 case 7:
  Destroy_queue(&head);
  break;
 case 8:
  len1=Lenght_queue(head);
  printf("当前链队列的长度为:%d\n",len1);
  break;
 case 9:
  Print_queue(head);
  break;
 case 10:
  return;
 default:
  instruction(head);
  break;
 }
 instruction(head);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/huangqiang1363/article/details/40865045