栈与队列 练习
2018-03-27 20:14
204 查看
栈
#include<stdio.h> #include<windows.h> #include<assert.h> typedef int DataType; #define Stack_size 10 typedef struct Stack { DataType* _array;//数组指针 size_t _top; //栈顶 size_t _end;//最大容量 }Stack; ----------------------------------------------------------- // 栈的实现接口 void StackInit(Stack* s)//栈的初始化 { assert(s); s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size); s->_end = Stack_size; s->_top = 0; } ----------------------------------------------------------- void StackPush(Stack* s, DataType x)//入栈 { assert(s); if (s->_end == s->_top)//栈已满 { s->_end *= 2; s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end)); s->_array[s->_top] = x; (s->_top)++; } else { s->_array[s->_top] = x; (s->_top)++; } } --------------------------------------------------------- void Stack_print(Stack *s) { if (s == NULL) { return; } while ((s->_top)--) { printf("%d\t", s->_array[s->_top]); } } --------------------------------------------------------- void StackPop(Stack* s)//出栈 { assert(s); if (s->_top == 0) { printf("the stack is empty"); } else { s->_top--; } } --------------------------------------------------------- DataType StackTop(Stack* s)//取栈顶元素 { assert(s); if (s->_top == 0) { printf("the stack is empty"); return; } else { int num = s->_top; int i = s->_array[(--num)]; return i; } } --------------------------------------------------------- size_t StackSize(Stack* s)//栈的长度 { assert(s); return s->_end; } --------------------------------------------------------- int StackEmpty(Stack* s)//判断栈是否为空 { if (s->_top == 0) { return 1; } else { return 0; } }
队列
#include<stdio.h> #include<windows.h> #include<assert.h> typedef int DataType; typedef struct QueueNode { DataType _data; //链式队列 struct QueueNode* _next; }QueueNode; typedef struct Queue { QueueNode* _head;//队头指针 QueueNode* _tail;//队尾指针 }Queue; ------------------------------------------------ void QueueInit(Queue* q)//队列初始化 { q->_head = (QueueNode*)malloc(sizeof(QueueNode)); assert(q->_head); q->_tail = q->_head; q->_tail->_next = NULL; } ------------------------------------------------- void QueuePush(Queue* q, DataType x)//入队 { assert(q); QueueNode *newnode = (QueueNode*)malloc(sizeof(QueueNode)); assert(newnode); newnode->_data = x; newnode->_next = NULL; q->_tail->_next = newnode; q->_tail = newnode; } --------------------------------------------------- void QueuePop(Queue* q)//出队 { assert(q); if (q->_head == q->_tail) { printf("the queue is empty."); return; } QueueNode *p = q->_head->_next; q->_head = p; } --------------------------------------------------- DataType QueueFront(Queue* q)//取队头数据 { assert(q); if (q->_head == q->_tail) { printf("the queue is empty."); return 0; } return (q->_head->_next)->_data; } ---------------------------------------------------- DataType QueueBack(Queue* q)//取队尾数据 { assert(q); if (q->_head == q->_tail) { printf("the queue is empty."); return 0; } QueueNode *p = q->_head->_next; while (p != q->_tail) { p = p->_next; } return p->_data; } ------------------------------------------------- size_t QueueSize(Queue* q)//计算队列长度 { assert(q); if (q->_head == q->_tail) { return 0; } QueueNode *flag = q->_head->_next; size_t i = 0; while (flag != q->_tail) { flag = flag->_next; i++; } return i+1 ; } ----------------------------------------------- int QueueEmpty(Queue* q)//判断队列是否为空 { assert(q); if (q->_head == q->_tail) { printf("the queue is empty."); return 0; } return 1; }
相关文章推荐
- 2016夏季练习——单调队列
- Codevs_P3185 队列练习1
- Java 基础一些代码练习笔记(队列)
- 《算法导论》第六章----优先级队列(代码实现+部分练习)
- Codevs 队列练习 合并版
- 关于Posix消息队列简单的发送接收消息的练习
- 3186 队列练习 2
- STL练习: queue ——队列
- 剑指offer-算法题练习:part5 用两个栈实现队列
- 队列练习
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- Codevs_P3186 队列练习2
- 《算法导论》第六章----优先级队列(代码实现+部分练习)
- 练习代码,写个消息队列发送接收
- HDOJ 1053 Huffman编码 自写优先队列的ADT 权当做练习数据结构
- 3187 队列练习 3
- 笨办法学 Python · 续 练习 15:栈和队列
- C++编程练习(5)----“实现简单的循环队列的顺序存储结构“
- jzoj 1365. 【队列练习】奇怪的电梯
- Codevs_P3187 队列练习3