数据结构--栈的基本操作
来源:互联网 发布:阿铭linux 编辑:程序博客网 时间:2024/06/06 05:56
#include<stdio.h> //栈的顺序存储 #include<stdlib.h>#define stack_int_size 100#define stack_increment 10typedef struct {int *top;int *base;int sqlist; //栈的当前大小 }Stack;void init(Stack *p) //初始化栈 {p->base=(int *)malloc(stack_int_size*sizeof(int));if(!p->base) exit(-1);p->top=p->base;p->sqlist=stack_int_size;}int getTop(Stack *p) //栈非空,返回栈顶元素 {if(p->top==p->base) return 0;else return *(p->top-1); }int insertTop(Stack *p,int m) //插入元素m到栈顶 {if(p->top-p->base>=p->sqlist) //栈满 {p->base=(int *)realloc(p->base,(stack_increment+stack_int_size)*sizeof(int));if(!p->base) exit(-1); //分配失败p->top=p->base+p->sqlist;p->sqlist+=stack_increment; }*(p->top++)=m;}void scanf_1(Stack *p,int n) {int i;for(i=0;i<n;i++){scanf("%d",p->top);p->top++;}}void print_1(Stack *p){int i;for(i=0;p->top!=p->base;i++)printf("%d\n",*(--p->top)); }int main(){int n;while(scanf("%d",&n)!=EOF){Stack m,*p;p=&m;init(p); scanf_1(p,n);printf("输出栈顶元素:");printf("%d\n",getTop(p));printf("请输入要插入到栈顶的元素:");int u;scanf("%d",&u); insertTop(p,u); printf("栈里面元素如下排列:");printf("\n");print_1(p); }}
运行结果如下:
0 0
- 【数据结构】 栈的基本操作
- 数据结构栈的基本操作
- 栈的基本操作-数据结构
- 【数据结构】栈的基本操作
- [数据结构]栈的基本操作
- 数据结构--栈的基本操作
- 数据结构--栈的基本操作
- 数据结构:栈的基本操作
- 数据结构-栈的基本操作
- 数据结构--栈的基本操作
- 栈的简单基本操作(数据结构)
- 数据结构---链栈的基本操作
- 数据结构--顺序栈的基本操作
- 数据结构 顺序栈的基本操作
- 数据结构 共用栈的基本操作
- 数据结构 链栈的基本操作
- 数据结构——栈的基本操作
- 数据结构第三章栈的基本操作
- 今天用到ajax5级联动,后台调用struts的action,这里只记录前台调用的代码
- iOS开发数据库篇—SQLite的应用
- 如何用Maven创建web项目
- 后续遍历---树、深搜
- qt实现多平台的二维码展示
- 数据结构--栈的基本操作
- (枚举)算法竞赛入门经典(7.1.2)最大乘积
- JnI调用机制
- 分享一个链接
- Html之表单标签
- 在类中声明函数而不去定义编译通过
- POJ 3767 I Wanna Go Home【最短路floyd】
- 查找(一)史上最简单清晰的红黑树讲解
- bfs