(C语言)数据结构 --- 队列、栈
2018-03-06 20:09
429 查看
1 /*
2 * FILE: test.c
3 * ============
4 */
5
6 #include "QUEUE_linkedlist.h"
7 #include "STACK_linkedlist.h"
8
9 #include <stdio.h>
10
11 int main(void)
12 {
13 int i;
14
15 for(i=0; i<10; i++)
16 QUEUEput(i);
17 printf("QUEUE: FIFO\n");
18 for(i=0; i<10; i++)
19 printf("%d ", QUEUEget(i));
20 printf("\n");
21
22 for(i=0; i<10; i++)
23 STACKpush(i);
24 printf("STACK: LIFO\n");
25 for(i=0; i<10; i++)
26 printf("%d ", STACKpop(i));
27 printf("\n");
28 return 0;
29 }队列 queue:入队 put、出对get 1 /*
2 * FILE: QUEUE_linkedlist.c
3 * ========================
4 * 队列 queue
5 * 入队 put:放在链表尾部,并更新指向链表尾部的指针tail
6 * 出队 get: 返回并删除链表头部节点,并更新指向链表头部的指针head
7 */
8
9 #include "QUEUE_linkedlist.h"
10
11 // static 限制全局变量的作用域
12 static struct node *head = NULL, *tail = NULL;
13
14 // static 限制函数作用域,只作用于本文件
15 static struct node *NEW(int data)
16 {
17 struct node *new = (struct node *)malloc(sizeof(struct node));
18 new->data = data;
19 new->next = NULL;
20 return new;
21 }
22
23 int QUEUEget()
24 {
25 int res = head->data;
26 struct node *p = head;
27 head = head->next;
28 free(p);
29 return res;
30 }
31
32 void QUEUEput(int data)
33 {
34 if(head == NULL)
35 {
36 head = tail = NEW(data);
37 return;
38 }
39 tail->next = NEW(data);
40 tail = tail->next;
41 }
42
43 void QUEUEempty()
44 {
45 struct node *p =head;
46 while(p != NULL)
47 {
48 struct node *temp = p;
49 p = p->next;
50 free(temp);
51 }
52 head = tail = NULL;
53 }
1 /* 2 * FILE: QUEUE_linkedlist.h 3 * ======================== 4 */ 5 6 #include <stdlib.h> 7 8 struct node{ 9 int data; 10 struct node *next; 11 }; 12 13 int QUEUEget(); 14 void QUEUEput(int data); 15 void QUEUEempty();栈 stack:入栈push、出栈pop 1 /*
2 * FILE: STACK_linkedlist.c
3 * ========================
4 * 栈 stack 后进先出
5 * 入栈 push:放在链表头部,并更新链表头指针head
6 * 出栈 pop:返回并删除链表头部节点,并更新链表头指针head
7 */
8
9 #include "STACK_linkedlist.h"
10
11 // static 静态全局变量,限制了全局变量的作用域
12 static struct STACKnode *head = NULL;
13
14 // static 静态函数,限制了函数的作用域
15 static struct STACKnode *NEW(int data, struct STACKnode *head)
16 {
17 struct STACKnode *new = (struct STACKnode *)malloc(sizeof(struct STACKnode));
18 new->data = data;
19 new->next = head;
20 return new;
21 }
22
23 void STACKpush(int data)
24 {
25 head = NEW(data, head);
26 }
27
28 int STACKpop()
29 {
30 int res = head->data;
31 struct STACKnode *p = head;
32 head = head->next;
33 free(p);
34 return res;
35 }
36
37 void STACKempty()
38 {
39 struct STACKnode *p = head;
40 while(p != NULL)
41 {
42 struct STACKnode *temp = p;
43 p = p->next;
44 free(temp);
45 }
46 head = NULL;
47 }
/* * FILE: STACK_linkedlist.h * ======================== */ #include <stdlib.h> struct STACKnode{ int data; struct STACKnode *next; }; void STACKpush(int data); int STACKpop(); void STACKempty();
相关文章推荐
- C语言数据结构-链队列
- 手工数据结构系列-C语言模拟队列和栈 hdu1702
- 数据结构C语言实现系列——队列
- 数据结构——c语言描述 第三章(3) 队列
- 数据结构 c语言实现顺序队列(输数字入队,字符出队)
- C语言数据结构练习——停车场管理系统(使用栈和队列)(草稿的草稿)
- C语言实现数据结构之队列
- 数据结构---栈和队列之顺序栈(C语言)
- 数据结构 C语言 队列 迷宫问题
- 数据结构C语言实现之链式队列的6种算法代码
- 算法与数据结构-队列的基本操作C语言实现
- 数据结构:循环队列(C语言实现)
- 循环队列–C语言实现–数据结构
- 数据结构:循环队列(C语言实现)
- 数据结构:循环队列(C语言实现)
- C语言数据结构之实现顺序存储结构队列
- 【数据结构】之队列(C语言描述)
- 数据结构 优先队列 C语言实现
- c语言数据结构——循环队列
- [数据结构]C语言队列的实现