数据结构----栈的操作
来源:互联网 发布:反恐数据库 编辑:程序博客网 时间:2024/05/22 15:52
栈的内容感觉比起链表要容易,因为它涉及到内存的操作不如链表那样抽象:
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define MAX 10typedef struct stack{ int elem[MAX]; int top;}stack;stack * stack_init()//栈初始化{ stack *S; S=(stack*)malloc(sizeof(stack)); if(S) { S->top=-1; printf("空栈成功\n"); return S; } else { return NULL; }}int Empty_stack(stack *S){ if(S->top==-1) { return 1; } else { return 0; }}int push_stack(stack *S){ int n; if(S->top==MAX-1) { printf("栈是满的\n"); return 0; } else { printf("请输入你要插入的元素的个数:\n"); scanf("%d",&n); printf("请依次输入你要插入的元素:\n"); while(S->top<n-1) { S->top++; scanf("%d",&S->elem[S->top]); } printf("插入元素成功:\n"); return 1; }}int Pop_stack(stack *S,int *tem){ if(Empty_stack(S)) { printf("栈是空的:\n"); } else { *tem=S->elem[S->top]; S->top--; return 1; }}int gettop_stack(stack *S,int *tem){ if(Empty_stack(S)) { printf("栈是空的:\n"); return 0; } else { *tem=S->elem[S->top]; return 1; }}void Print_stack(stack *S){ int i=S->top; for(;i>=0;i--) { printf("%d",S->elem[i]); } printf("\n");}void main(){ int select,data,k,k1; stack*s; printf(" \n"); printf("------------------------\n"); printf(" 1.栈的初始化\n"); printf(" 2.数据入栈\n "); printf(" 3.数据出栈\n"); printf(" 4.取栈顶元素.\n"); printf(" 5.取出栈中的元素\n"); printf(" 6.退出\n"); printf("------------------------\n"); scanf("%d",&select); while(select) { switch(select) { case 1:s=stack_init();break; case 2:push_stack(s);break; case 3:k=Pop_stack(s,&data); if(k)printf("出栈的元素是:%d\n",data);break; case 4:k1=gettop_stack(s,&data); if(k1)printf("当前栈顶的元素是:%d",data);break; case 5:Print_stack(s);break; case 6:exit(0); default:printf("error \n"); } printf("请选择相应选项:\n"); scanf("%d",&select); }}
0 0
- 数据结构--栈的操作
- 数据结构 -- 栈的操作
- 数据结构----栈的操作
- 数据结构栈的操作
- 数据结构栈的操作
- 数据结构-栈的操作
- 【数据结构】链式栈的操作
- 【数据结构】 栈的基本操作
- 数据结构栈的基本操作
- 栈的基本操作-数据结构
- 【数据结构】栈的基本操作
- [数据结构]栈的基本操作
- 数据结构--栈的基本操作
- 数据结构--栈的基本操作
- 数据结构:栈的基本操作
- 数据结构-栈的基本操作
- 数据结构栈的一系列操作
- 数据结构--栈的基本操作
- Flow Control in TCP(tcp的流量控制实现)
- Reading comprehension (BestCoder Round #8) (hdoj 4990)
- JSON解析的时候遇到NSNull
- 过桥问题(美团2014笔试题)
- hdf4
- 数据结构----栈的操作
- TestNG开源插件Arrow介绍
- 时间序列拟合模型诊断
- 多线程编程
- Java事务
- 职责链模式(Chain of Responsibility)
- 浅谈STC跟踪算法
- springMVC form表单提交---包含时间类型的数据
- EQ读书笔记1