1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 typedef int QElemType;
6
7 typedef struct QNode{
8 QElemType data;
9 struct QNode *next;
10 }QNode, *QueuePtr;
11
12 typedef struct{
13 QueuePtr front; //头指针
14 QueuePtr rear; //尾指针
15 }LinkQueue;
16
17 int InitQueue(LinkQueue *Q);
18 int DestroyQueue(LinkQueue *Q);
19 int ClearQueue(LinkQueue *Q);
20 int QueueEmpty(LinkQueue *Q);
21 int QueueLength(LinkQueue Q);
22 int GetHead(LinkQueue Q, QElemType *e);
23 int InsertQueue(LinkQueue *Q, QElemType e);
24 int DelQueue(LinkQueue *Q, QElemType *e);
25 int PrintQueue(LinkQueue Q);
26
27 //初始化队列
28 int InitQueue(LinkQueue *Q){
29 Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
30 if(!Q->front){
31 perror("malloc error ");
32 return -1;
33 }
34 Q->front->next = NULL;
35 Q->front->data = 0;
36 return 0;
37 }
38
39 //销毁队列
40 int DestroyQueue(LinkQueue *Q){
41 while(Q->front){
42 Q->rear = Q->front->next;
43 free(Q->front);
44 Q->front = Q->rear;
45 }
46 Q = NULL;
47 return 0;
48 }
49
50 //清空队列
51 int ClearQueue(LinkQueue *Q){
52 return 0;
53 }
54
55 //判断队列是否为空
56 int QueueEmpty(LinkQueue Q){
57 if(Q.front == Q.rear){
58 return 1;
59 }
60 else{
61 return 0;
62 }
63 }
64
65 //球队列长度
66 int QueueLength(LinkQueue Q){
67 return Q.front->data;
68 }
69
70 //球队列的头指针
71 int GetHead(LinkQueue Q, QElemType *e){
72 if(Q.front->next == NULL){
73 perror("Queue is empty! ");
74 *e = -1;
75 return -1;
76 }
77 *e = Q.front->next->data;
78 return 0;
79 }
80
81 //将元素e插入队列
82 int InsertQueue(LinkQueue *Q, QElemType e){
83 QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
84 if(p == NULL){
85 perror("malloc error! ");
86 return -1;
87 }
88 p->data = e;
89 p->next = NULL;
90 (Q->rear)->next = p;
91 Q->rear = p;
92 Q->front->data++;
93 return 0;
94 }
95
96 //删除队列中的元素e
97 int DelQueue(LinkQueue *Q,QElemType *e){
98 if(Q->front == Q->rear){
99 perror("The queue is empty! ");
100 return -1;
101 }
102 QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
103 p = Q->front->next;
104 *e = p->data;
105 Q->front->next = p->next;
106 if(Q->rear == p){
107 Q->rear = Q->front;
108 }
109 free(p);
110 Q->front->data--;
111 return 0;
112 }
113
114 //输出队列
115 int PrintQueue(LinkQueue Q){
116 Q.front = Q.front->next;
117 while(Q.front != NULL){
118 printf("%d-----", Q.front->data);
119 Q.front = Q.front->next;
120 }
121 return 0;
122 }
123
124 int main(){
125 return 0;
126 }
2 #include <string.h>
3 #include <stdlib.h>
4
5 typedef int QElemType;
6
7 typedef struct QNode{
8 QElemType data;
9 struct QNode *next;
10 }QNode, *QueuePtr;
11
12 typedef struct{
13 QueuePtr front; //头指针
14 QueuePtr rear; //尾指针
15 }LinkQueue;
16
17 int InitQueue(LinkQueue *Q);
18 int DestroyQueue(LinkQueue *Q);
19 int ClearQueue(LinkQueue *Q);
20 int QueueEmpty(LinkQueue *Q);
21 int QueueLength(LinkQueue Q);
22 int GetHead(LinkQueue Q, QElemType *e);
23 int InsertQueue(LinkQueue *Q, QElemType e);
24 int DelQueue(LinkQueue *Q, QElemType *e);
25 int PrintQueue(LinkQueue Q);
26
27 //初始化队列
28 int InitQueue(LinkQueue *Q){
29 Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
30 if(!Q->front){
31 perror("malloc error ");
32 return -1;
33 }
34 Q->front->next = NULL;
35 Q->front->data = 0;
36 return 0;
37 }
38
39 //销毁队列
40 int DestroyQueue(LinkQueue *Q){
41 while(Q->front){
42 Q->rear = Q->front->next;
43 free(Q->front);
44 Q->front = Q->rear;
45 }
46 Q = NULL;
47 return 0;
48 }
49
50 //清空队列
51 int ClearQueue(LinkQueue *Q){
52 return 0;
53 }
54
55 //判断队列是否为空
56 int QueueEmpty(LinkQueue Q){
57 if(Q.front == Q.rear){
58 return 1;
59 }
60 else{
61 return 0;
62 }
63 }
64
65 //球队列长度
66 int QueueLength(LinkQueue Q){
67 return Q.front->data;
68 }
69
70 //球队列的头指针
71 int GetHead(LinkQueue Q, QElemType *e){
72 if(Q.front->next == NULL){
73 perror("Queue is empty! ");
74 *e = -1;
75 return -1;
76 }
77 *e = Q.front->next->data;
78 return 0;
79 }
80
81 //将元素e插入队列
82 int InsertQueue(LinkQueue *Q, QElemType e){
83 QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
84 if(p == NULL){
85 perror("malloc error! ");
86 return -1;
87 }
88 p->data = e;
89 p->next = NULL;
90 (Q->rear)->next = p;
91 Q->rear = p;
92 Q->front->data++;
93 return 0;
94 }
95
96 //删除队列中的元素e
97 int DelQueue(LinkQueue *Q,QElemType *e){
98 if(Q->front == Q->rear){
99 perror("The queue is empty! ");
100 return -1;
101 }
102 QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
103 p = Q->front->next;
104 *e = p->data;
105 Q->front->next = p->next;
106 if(Q->rear == p){
107 Q->rear = Q->front;
108 }
109 free(p);
110 Q->front->data--;
111 return 0;
112 }
113
114 //输出队列
115 int PrintQueue(LinkQueue Q){
116 Q.front = Q.front->next;
117 while(Q.front != NULL){
118 printf("%d-----", Q.front->data);
119 Q.front = Q.front->next;
120 }
121 return 0;
122 }
123
124 int main(){
125 return 0;
126 }