苏嵌日志3
姓名: 朱启香
日期:2018年9月12日
今日学习任务:
栈:先进后出
队列:先进先出 顺序存储 键式存储
(队头:取出数据 队尾:存放数据)
队列的循序存储: 循环队列
空队:队头队尾重合
队尾指针:指向最后一个元素的后一个
循环队列长度:(rear–front+MAXSIZE)%MAXSIZE
判断队列是否满:(rear+1)%MAXSIZE==front
今日任务完成情况
基本能跟在老师后面,能顺利完成编译,今天的代码开发量400+。
今日开发中出现的问题
代码编写不清晰 、 而且无法独立编写程序,不能完全听懂。
今日开发收获
学会了如何初始化,清空,输入,输出,删除,销毁等一系列操作
学会了如何队列的顺序存储
今日自我评价
总体情况还行,代码写出来错误也不多。错误能及时改正并顺利运行程序。
今日程序如下
main.c
1 #include "queue.h"
2 #include <stdio.h>
3
4 int main()
5 {
6 int ret,i;
7 Q *queue;
8
9 ret = InitQueue(&queue);
10 if(ret ==SUCCESS)
11 {
12 printf("Init Success!\n");
13 }
14 else
15 {
16 printf("Init Failure!\n");
17 }
18 for(i=0;i<10;i++)
19 {
20 ret=EnterQueue(queue,i+1);
21 if(ret==SUCCESS)
22 {
23 printf("Enter %d Success!\n",i+1);
24 }
25 else
26 {
27 printf("Enter Failure!\n");
28 }
29
30 }
31
32 for(i=0;i<6;i++)
33 {
34 ret=DeleteQueue(queue);
35 if(ret==FAILURE)
36 {
37 printf("Delete Failure!\n");
38 }
39 else
40 {
41 printf("Delete %d Success!\n",ret);
42 }
43 }
44
45 ret=LengthQueue(queue);
46 printf("length is %d\n",ret);
47
48 ret=ClearQueue(queue);
49 if(ret==SUCCESS)
50 {
51 printf("Clear Success!\n");
52 }
53 else
54 {
55 printf("Clear Failure!\n");
56 }
57
58 ret=LengthQueue(queue);
59 printf("length is %d\n",ret);
60
61 ret=EmptyQueue(queue);
62 if(SUCCESS == ret)
63 {
64 printf("queue is empty!\n");
65 }
66 else
67 {
68 printf("queue is not empty!\n");
69 }
70
71 ret=DestroyQueue(&queue);
72 if(ret==SUCCESS)
73 {
74 printf("Destroy Success!\n");
75 }
76 else
77 {
78 printf("Destroy Failure!\n");
79 }
80
81 return 0;
82 }
`
queue.c
1 #include "queue.h"
2 #include <stdlib.h>
3
4 int InitQueue(Q **q)
5 {
6 if(NULL==q)
7 {
8 return FAILURE;
9 }
10
11 (*q)=(Q *)malloc(sizeof(Q));
12 if(NULL==(*q))
13 {
14 return FAILURE;
15 }
16
17 Node *p=(Node *)malloc(sizeof(Node));
18 if(NULL==p)
19 {
20 return FAILURE;
21 }
22
23 (*q)->front =(*q)->rear = p;
24
25 return SUCCESS;
26 }
27
28 int EnterQueue(Q *q, int e)
29{
30 if(NULL==q)
31 {
32 return FAILURE;
33 }
34
35 Node *p=(Node *)malloc(sizeof(Node));
36 if(NULL==p)
37 {
38 return FAILURE;
39 }
40 p->next=NULL;
41 p->data=e;
42
43 q->rear->next=p;
44 q->rear=p;
45
46 return SUCCESS;
47 }
48
49 int DeleteQueue(Q *q)
50 {
51 if(NULL==q)
52 {
53 return FAILURE;
54 }
55
56
57 if(q->rear==q->front)
58 {
59 return FAILURE;
60 }
61
62 Node *p=q->front->next;
63 int e=p->data;
64 q->front->next=p->next;
65 free(p);
66
67 if(q->rear==p)
68 {
69 q->rear=q->front;
70 }
71
72 return e;
73
74 }
75 int LengthQueue(Q *q)
76 {
77 if(NULL==q)
78 {
79 return FAILURE;
80 }
81
82 int length =0;
83
84 Node *p=q->front->next;
85 while (p)
86 {
87 length++;
88 p=p->next;
89 }
90
91 return length;
92 }
93
94 int ClearQueue(Q *q)
95 {
96 if(NULL==q)
97 {
98 return FAILURE;
99 }
100
101 Node *p=q->front->next;
102
103 while(p)
104 {
105 q->front->next=p->next;
106 free(p);
107 p=q->front->next;
108 }
109
110 q->rear=q->front;
111
112 return SUCCESS;
113 }
114 int EmptyQueue(Q *q)
115 {
116 if(NULL==q)
117 {
118 return FAILURE;
119 }
120
121 return(q->front==q->rear)? SUCCESS : FAILURE;
122 }
123
124 int DestroyQueue(Q **q)
125 {
126 if(NULL==q)
127 {
128 return FAILURE;
129 }
130
131 free((*q)->front);
132 free(*q);
133 *q=NULL;
134
135 return SUCCESS;
136 }```
queue.h
1 #ifndef QUEUE_H
2 #define QUEUE_H
3
4 #define SUCCESS 1000
5 #define FAILURE 1001
6
7 struct node
8 {
9 int data;
10 struct node *next;
11 };
12 typedef struct node Node;
13
14 struct queue
15 {
16 Node *front;
17 Node *rear;
18 };
19 typedef struct queue Q;
20
21 int InitQueue(Q **q);
22 int EnterQueue(Q *q, int e);
23 int DeleteQueue(Q *q);
24 int LengthQueue(Q *q);
25 int ClearQueue(Q *q);
26 int EmptyQueue(Q *q);
27 int DestroyQueue(Q **q);
28 #endif