顺序队列
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "./01_sequeue.h"
4
5 /*
6 * function:
7 * @param [ in]
8 * @param [out]
9 * @return
10 */
11 Sequeue* create_sequeue(void)
12 {
13 Sequeue* sq = (Sequeue*)malloc(sizeof(Sequeue));
14 if(NULL == sq)
15 {
16 printf("创建失败\n");
17 return NULL;
18 }
19 sq->front = sq->rear = 0;
20 return sq;
21 }
22
23 /*
24 * function: 判断队列是否为满
25 * @param [ in]
26 * @param [out]
27 * @return
28 */
29 int isSequeue_full(Sequeue* sq)
30 {
31 return sq->front == (sq->rear+1)%N? 1:0;
32 }
33
34 /*
35 * function: 入队
36 * @param [ in]
37 * @param [out]
38 * @return
39 */
40 void insert_sequeue(Sequeue* sq, dataType data)
41 {
42 if(isSequeue_full(sq))
43 {
44 printf("队列满了\n");
45 return;
46 }
47 sq->data[sq->rear] = data;
48 sq->rear = (sq->rear+1)%(N+1);
49 return;
50 }
51
52 /*
53 * function: 判空
54 * @param [ in]
55 * @param [out]
56 * @return
57 */
58 int isSequeue_empty(Sequeue* sq)
59 {
60 return sq->front == sq->rear ?1: 0;
61 }
62
63 /*
64 * function: 出队
65 * @param [ in]
66 * @param [out]
67 * @return
68 */
69
70 dataType delete_sequeue(Sequeue*sq)
71 {
72 if(isSequeue_empty(sq))
73 {
74 printf("队列为空\n");
75 return (dataType)-1;
76 }
77
78 dataType data = sq->data[sq->front];
79 sq->front = (sq->front+1)%(N+1);
80 return data;
81 }
82
83 /*
84 * function: 遍历
85 * @param [ in]
86 * @param [out]
87 * @return
88 */
89 void show_sequeue(Sequeue* sq)
90 {
91 for(int i = sq->front; i!=sq->rear; i=(i+1)%(N+1))
92 {
93 printf("%d\t",sq->data[i]);
94 }
95 putchar(10);
96 }
97

| 1 #include <stdio.h>
| 2 #include "./03_seqstack.h"
| 3 #include <stdlib.h>
| 4 /*
| 5 * function: 创建一个空栈
| 6 * @param [ in]
| 7 * @param [out]
| 8 * @return
| 9 */
y| 10
| 11 Seqstack* create_seqStack()
%| 12 {
| 13 Seqstack* sq = (Seqstack*)malloc(sizeof(Seqstack));
| 14 if(NULL == sq)
| 15 {
]| 16 printf("失败\n");
| 17 return NULL;
)| 18 }
| 19 sq->pos = 0;
| 20 return sq;
| 21 }
| 22
| 23
| 24 /*
| 25 * function: 入栈 + 判断是否满栈
| 26 * @param [ in]
| 27 * @param [out]
| 28 * @return
| 29 */
| 30
| 31 void ru_seqStack(Seqstack* sq, dataType data)
| 32 {
| 33 //判满
| 34 if(sq->pos>=N)
| 35 {
| 36 printf("满了\n");
| 37 return;
| 38 }
| 39 sq->data[sq->pos] = data;
| 40 sq->pos++;
| 41 return;
| 42 }
| 43
| 44 /*
| 45 * function: 出栈 + 判断是否为空
| 46 * @param [ in]
| 47 * @param [out]
| 48 * @return
| 49 */
| 50 dataType chu_seqStack(Seqstack* sq)
| 51 {
| 52 //判空
| 53 if(0 ==sq->pos)
| 54 {
| 55 printf("空\n");
| 56 return (dataType)-1;
| 57 }
| 58 sq->pos--;
| 59 return sq->data[sq->pos];
| 60 }
| 61
| 62 /*
| 63 * function: 遍历栈
| 64 * @param [ in]
| 65 * @param [out]
| 66 * @return
| 67 */
| 68
| 69 void show_seqStack(Seqstack* sq)
| 70 {
| 71 for(int i = sq->pos-1;i>=0; i--)
| 72 {
| 73 printf("[%d] %d\n",i, sq->data[i]);
| 74 }
| 75 }
| 76
| 77
| 78
| 79
| 80
| 81
| 82
|~
|~
|~
|~
|~
|~
|~
|~
|~
|~
|~
|~

双向链表
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "./03_doublelist.h"
4
5 /*
6 * function: 创建一个空的双向链表
7 * @param [ in]
8 * @param [out]
9 * @return
0 */
1
2 Dublinklist* create_doublelist()
3 {
4 Dublinklist* head = (Dublinklist*)malloc(sizeof(Dublinklist));
5 if(NULL == head)
6 {
7 printf("失败\n");
8 return NULL;
9 }
0
1 head->msg.len = 0;
2 head->next = NULL;
3 head->prev = NULL;
4
5 return head;
6 }
7
8
9 /*
0 * function: 尾插
1 * @param [ in]
2 * @param [out]
3 * @return
4 */
5 void insert_doublelisttail(Dublinklist* head, datatype data)
6 {
7 Dublinklist* temp = (Dublinklist*)malloc(sizeof(Dublinklist));
8 if(NULL == temp)
9 {
0 printf("创建失败\n");
1 return;
2 }
3
4 temp->msg.data = data;
5 temp->next = NULL;
6 temp->prev = NULL;
7
8 Dublinklist* p = head;
9 while(p->next != NULL)
0 {
1 p = p->next;
2 }
3
4 temp->next = p->next;
5 p->next = temp;
6 temp->prev = p;
7
8 head->msg.len++;
9 return;
0 }
1
2 /*
3 * function: 尾删
4 * @param [ in]
5 * @param [out]
6 * @return
7 */
8 datatype del_doublelisttail(Dublinklist* head)
9 {
0 if(NULL == head->next)
1 {
2 printf("删除失败");
3 return(datatype)-1;
4 }
5
6 Dublinklist* p =head;
7 while(p->next !=NULL)
8 {
9 p=p->next;
0 }
1 p->prev->next = NULL;
2 datatype data = p->msg.data;
3 free(p);
4 head->msg.len--;
5 return data;
6 }
7
8
9
0
1
2
3
4
5
6 /*
7 * function: 遍历
8 * @param [ in]
9 * @param [out]
0 * @return
1 */
2 void show_doublist(Dublinklist* head)
3 {
4 Dublinklist* p = head;
5 while(p->next != NULL)
6 {
7 p = p->next;
8 printf("%d\t", p->msg.data);
9 }
0 printf("\n");
1 }
2
3
4

2410

被折叠的 条评论
为什么被折叠?



