栈(Stack)的C语言实现
来源:互联网 发布:深入浅出的数据分析 编辑:程序博客网 时间:2024/05/01 10:17
栈(Stack)实现的是一个后进先出策略。元素弹出的顺序正好和它们压入的次序相反。S.top表示栈顶元素,S.size表示栈的大小。如果试图对一个S.top=0的栈进行弹出操作,则称栈下溢。如果S.top超过了S.size,则称栈上溢。
这里使用动态分配内存的方式实现栈,并可以动态的调整栈的大小,如有错误敬请指正。
//Item.htypedef struct ITEM { int key; void * statellite;} item_t;
//Stack.h#ifndef STDIO_H#define STDIO_H#include <stdio.h>#endif#ifndef STDLIB_H#define STDLIB_H#include <stdlib.h>#endif#ifndef ITEM_H#define ITEM_H#include "Item.h"#endiftypedef struct STACK{ int top; int size; item_t* array;} stack_t;int stack_init(stack_t * S, int size);int stack_resize(stack_t * S, int size);int stack_free(stack_t * S);int stack_empty(stack_t * S);int stack_push(stack_t * S, item_t item);int stack_pop(stack_t * S, item_t *item);void stack_info(stack_t * S);
//Stack.c#include "Stack.h"int stack_init(stack_t * S, int size) { S->top = 0; S->array = (item_t*)calloc(size, sizeof(item_t)); if (S->array != NULL) { S->size = size; return 1; } else { S->size = 0; fprintf(stderr, "Stack init fail.\n"); return 0; }}int stack_resize(stack_t * S, int size) { S->array = (item_t*)realloc(S->array, size * sizeof(item_t)); if (S->array != NULL) { S->size = size; if (S->top > S->size) { S->top = S->size; } return 1; } else { S->top = 0; S->size = 0; fprintf(stderr, "Stack resize fail.\n"); return 0; }}int stack_free(stack_t * S) { free(S->array); S->array = NULL; S->top = 0; S->size = 0; return 1;}int stack_empty(stack_t * S) { if (S->array == NULL) { fprintf(stderr, "Stack is not initialized.\n"); return 1; } if (S->top == 0) return 1; else return 0;}int stack_push(stack_t * S, item_t item) { if (S->array == NULL) { fprintf(stderr, "Stack is not initialized.\n"); return 0; } if (S->top < S->size) { S->array[S->top++] = item; return 1; } else { fprintf(stderr, "Stack overflow.\n"); return 0; }}int stack_pop(stack_t * S, item_t * item) { if (S->array == NULL) { fprintf(stderr, "Stack is not initialized.\n"); return 0; } if (!stack_empty(S)) { *item = S->array[--S->top]; return 1; } else { fprintf(stderr, "Stack underflow\n"); return 0; }}void stack_info(stack_t * S) { static int count = 0; printf("%d:\n", count++); if (S->array == NULL) { printf("stack is not initialized.\n-------------------------------\n"); return; } printf("stack top:%d\n", S->top); printf("stack size:%d\n", S->size); printf("stack element:\n"); for (int i = S->top - 1; i >= 0; i--) { printf("%d ", S->array[i].key); } printf("\n-------------------------------\n");}
阅读全文
0 0
- 栈(Stack)的C语言实现
- C语言,简单栈的实现 Stack
- 动态栈Stack的C语言实现
- 栈(Stack) C 语言实现
- c语言 ---- Stack实现
- 栈(stack)C语言链表实现&&数组实现
- c语言栈Stack简单实现
- C语言实现stack模板
- C语言Stack简单实现
- 栈(stack):栈的C语言示例代码
- C语言——栈(Stack)
- [leetcode]Min Stack (获取栈的最小元素C语言实现)
- C++:栈(stack)的模板类实现
- 【转】C语言实现stack模板
- 栈(Stack)的实现
- 纯C的Stack实现
- [C#]Stack类的实现
- C语言验证栈(Stack)的增长方向
- linux关掉cpu命令
- POJ1222 EXTENDED LIGHTS OUT 高斯消元入门
- 【CUGBACM15级BC第22场 B】hdu 5143 NPY and arithmetic progression
- nvprof tx1 or tx2
- spring表达语言(SpEL)快速掌握
- 栈(Stack)的C语言实现
- spring-boot-笔记-2-thymeleaf初步了解
- 即时搜索的input和propertychange方法
- 如何从思维模式上彻底改变学习模式?
- Pacemaker
- [IOI1994]The Castle
- [编程题] 度度熊回家
- Java程序实现欧几里得算法-计算两个数的最大公约数
- 匈牙利算法