数据结构——队列(C语言实现)
生活随笔
收集整理的這篇文章主要介紹了
数据结构——队列(C语言实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼
?
1 #include <stdio.h>2 #include <stdlib.h>
3
4 typedef struct Node
5 {
6 int data;
7 struct Node* next;
8 }QueueNode;
9
10 typedef struct LinkList
11 {
12 QueueNode* head;
13 QueueNode* rear;
14 }LinkList;
15
16 ?void initLinkQueue(LinkList* Q)
17 {
18 Q->head=Q->rear=NULL;
19 }
20
21 ?int isEmpty(LinkList* Q)
22 {
23 return Q->head==NULL&&Q->rear==NULL;
24 }
25
26 ?void inQueue(LinkList* Q,int item)
27 {
28 QueueNode* pi=(QueueNode*)malloc(sizeof(QueueNode));
29 if(pi==NULL)
30 {
31 printf("分配內存失敗!\n");
32 exit(1);
33 }
34 pi->data=item;
35 pi->next=NULL;
36 if(isEmpty(Q))
37 {
38 Q->head=pi;
39 Q->rear=pi;
40 }
41 else
42 {
43 Q->rear->next=pi;
44 Q->rear=pi;
45 }
46 }
47
48
49 int outQueue(LinkList* Q)
50 {
51 QueueNode* pdel;
52 int temp;
53 if(isEmpty(Q))
54 {
55 printf("There is no element!\n");
56 exit(1);
57 }
58 temp=Q->head->data;
59 pdel=Q->head;
60 Q->head=Q->head->next;
61 if(Q->head==NULL)
62 Q->rear=NULL;
63 free(pdel);
64 return temp;
65 }
66
67 int peepQueue(LinkList* Q)
68 {
69 if(Q->head==NULL)
70 {
71 printf("There is no element!\n");
72 exit(1);
73 }
74 return Q->head->data;
75 }
76
77
78 /* 6.清除鏈隊中的所有元素 */
79 void clearQueue(LinkList* Q)
80 {
81 QueueNode* temp=Q->head;
82 while(temp)
83 {
84 Q->head=Q->head->next;
85 free(temp);
86 temp=Q->head;
87 }
88 Q->rear=NULL;
89 }
90
91
?
1 #include "LinkQueue.h"2
3 int main()
4 {
5 int temp;
6 LinkList* Q=(LinkList*)malloc(sizeof(LinkList));
7 initLinkQueue(Q);
8 inQueue(Q,10);
9 temp=peepQueue(Q);
10 printf("%d\n",temp);
11 temp=isEmpty(Q);
12 printf("isEmpty: %d\n",temp);
13 temp=outQueue(Q);
14 printf("%d\n",temp);
15 temp=isEmpty(Q);
16 printf("isEmpty: %d\n",temp);
17 inQueue(Q,20);
18 inQueue(Q,11);
19 inQueue(Q,19);
20 temp=peepQueue(Q);
21 printf("%d\n",temp);
22 clearQueue(Q);
23 temp=isEmpty(Q);
24 printf("isEmpty: %d\n",temp);
25
26 }
?
轉載于:https://www.cnblogs.com/hstcghost/archive/2010/11/09/1873170.html
總結
以上是生活随笔為你收集整理的数据结构——队列(C语言实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩玩Android
- 下一篇: head first html with