数据结构--顺序栈实现(c语言)
来源:互联网 发布:java socket服务器 编辑:程序博客网 时间:2024/04/28 10:56
练习了一下数据结构的栈的实现,感悟就是栈的容量好像没有在数据结构中引用。不过最后测试都通过了。还是老习惯,自己的练习自己看,留下来,提供一下参考,我先把数据结构学习一下,再来研究有应用的技术。基础打扎实点再说别的。稳住!
#include <stdio.h>#include <alloc.h>typedef struct array_stack{int*space;inttop;intbase;intsize;intvolume;}array_stack;/***visit element method.*/void visit(intdata){printf("%d\t",data);}/***init the stack.*/void stack_init(array_stack *stack){(*stack).volume=100;(*stack).space=(int *)malloc(sizeof(int)*(*stack).volume);(*stack).base=0;(*stack).top=-1;(*stack).size=0;}/***destroy the stack.*/void stack_destroy(array_stack *stack){(*stack).base=0;(*stack).top=-1;(*stack).size=0;(*stack).volume=0;free((*stack).space);}/***clear the stack.*/void stack_clear(array_stack *stack){(*stack).base=0;(*stack).top=-1;(*stack).size=0;}/***jugde the stack is empty.*/intstack_empty(array_stack *stack){return (*stack).size==0?1:0;}/***get the stack length.*/intstack_length(array_stack *stack){return (*stack).size;}/***get the stack top.*/intstack_get_top(array_stack *stack){return (*stack).space[(*stack).top];}/***push a element to the stack.*/void stack_push(array_stack *stack,intelement){intmove_index=(*stack).base;int*new_space=NULL;if((*stack).volume == (*stack).size){new_space=(int*)malloc(sizeof(int)*(*stack).volume*2);for(;move_index< (*stack).size;move_index++){new_space[move_index]=(*stack).space[move_index];}free((*stack).space);(*stack).space=new_space;(*stack).volume=(*stack).volume*2;}(*stack).top++;(*stack).space[(*stack).top]=element;(*stack).size++;}/***pop a element from the stack.*/void stack_pop(array_stack *stack,int*element){*element=(*stack).space[(*stack).top];(*stack).top--;(*stack).size--;}/***traverse the stack.*/void stack_traverse(array_stack *stack,void (*ptr)(intdata)){intmove_index=(*stack).base;for(;move_index<=(*stack).top;move_index++){ptr((*stack).space[move_index]);}}intmain(){intmove_index=0;intpop_data=0;array_stack test_stack;stack_init(&test_stack);stack_traverse(&test_stack,visit);for(;move_index<300;move_index++){stack_push(&test_stack,move_index);}stack_traverse(&test_stack,visit);stack_pop(&test_stack,&pop_data);printf("\nthe pop number:%d",pop_data);printf("\nthe stack is empty ? :%d",stack_empty(&test_stack));printf("\nthe stack length:%d",stack_length(&test_stack));printf("\nthe stack top:%d",stack_get_top(&test_stack));stack_clear(&test_stack);printf("\n");stack_traverse(&test_stack,visit);stack_destroy(&test_stack);}
数据结构,越写越觉得熟练,理解也更加深刻。
0 0
- 数据结构--顺序栈实现(c语言)
- C语言实现数据结构中的顺序栈
- 数据结构C语言实现之顺序栈
- <数据结构>顺序栈的C语言实现
- 数据结构-顺序栈-C语言实现
- 【数据结构】实现顺序表(c语言)
- 数据结构--顺序栈的实现(C语言)
- 数据结构-顺序栈(C语言)
- 数据结构(C语言)顺序栈
- 数据结构:顺序表的实现(C语言实现)
- C语言数据结构-顺序栈
- 数据结构学习系列四-顺序栈(c语言实现)
- 数据结构 学习笔记之:顺序栈的C语言实现
- 数据结构——顺序栈(C语言实现)
- 数据结构之---c语言实现栈的顺序存储
- 数据结构--C语言实现栈的顺序存储结构
- 数据结构中顺序栈c语言代码实现
- 数据结构--线性表.顺序表(C语言实现)
- 一个比较笨笨的方法,可以定时发微信(部分转自知乎)
- linux进程间通信的几种机制的比较及适用场合
- STRUTS2标签中符号#,$,%的用法示例
- 算法思维导图
- 封装将json对象转换为java集合对象
- 数据结构--顺序栈实现(c语言)
- 语法杂记
- 改变UINavigationcontroller的方法 记录下来
- 修复Linux引导(一):unknown filesystem,修复引导菜单
- android SDK 更新超时问题
- 观察者模式
- VC6.0 MFC 动态创建ODBC数据源
- Android学习总结---集成库
- 自动推荐(机器学习)流程--基于mahout