栈的基本操作
来源:互联网 发布:淘宝813 编辑:程序博客网 时间:2024/06/08 04:17
顺序栈
/** Copyright (c) 2017* 安徽大学计算机科学与技术学院* All rights reserved.*/#include <cstdio>#include<iostream>#include<bits/stdc++.h>#include <malloc.h>using namespace std;#define StackInitSize 20typedef struct{ int *base; int *top; int stacksize;}SqStack;int InitStack(SqStack &S); //初始化栈int StackEmpty(SqStack S); //判断栈是否为空int GetTop(SqStack S, int &e); //获取当前栈顶的值int Push(SqStack &S, int e); //进栈int Pop(SqStack &S, int &e); //出栈int main(){ SqStack *S = (SqStack*)malloc(sizeof(SqStack)); int len, e; int i; InitStack(*S); len = StackLength(*S); if(StackEmpty(*S)) printf("空栈\n"); printf("执行1-20进栈\n"); for (i=1; i<=StackInitSize; i++) Push(*S, i); if(StackEmpty(*S)) printf("空栈\n"); GetTop(*S, e); printf("栈顶元素是:%d\n", e); printf("执行出栈操作\n"); for (i=1; i<=StackInitSize; i++){ Pop(*S, e); printf("%d ", e); } printf("\n"); ClearStack(*S); return 0;}int InitStack(SqStack &S){ S.base = (int *)malloc(StackInitSize*sizeof(int)); if (S.base == NULL) exit(1); S.top = S.base; S.stacksize = StackInitSize; return 1;}int StackEmpty(SqStack S){ if (S.top == S.base) return 1; else return 0;}int StackLength(SqStack S){ int len; len = S.top - S.base; return len;}int GetTop(SqStack S, int &e){ if (S.base == S.top) { printf("栈中无元素\n"); exit(2); } e = *(S.top - 1); return 0;}int Push(SqStack &S, int e){ if (S.top - S.base >= S.stacksize) { if (S.base == NULL) { exit(3); } S.top = S.base + S.stacksize; S.stacksize += StackInitSize; } *(S.top++) = e; return 0;}int Pop(SqStack &S, int &e){ if (S.top == S.base) exit(4); e = *--S.top; return 0;}
/** Copyright (c) 2017* 安徽大学计算机科学与技术学院* All rights reserved.*/#include <cstdio>#include<iostream>#include<bits/stdc++.h>using namespace std;typedef struct node{ int data; struct node* next;}stacknode, *linkstack; //初始化栈void initstack(linkstack *s){ *s=new stacknode; (*s)->next=NULL;} //判断是否为空bool stackempty(linkstack s){ if(s->next==NULL) return 1; return 0;} //入栈操作void pushstack(linkstack s,int e){ linkstack p; p=new stacknode; p->data=e; p->next=s->next; s->next=p;} //出栈操作void popstack(linkstack s){ linkstack p; p=s->next; if(stackempty(s)) cout<<"栈空"<<endl; else { s->next=p->next; free(p); }}//获取栈顶元素void getstack(linkstack s){ if(stackempty(s)) cout<<"空栈"; else { cout<<(s->next)->data<<" "; }}int main(){ int i; linkstack s; initstack(&s); cout<<"将1-20压入栈"<<endl; for(i=1;i<=20;i++) pushstack(s,i); cout<<"出栈"<<endl; for(i=1;i<=20;i++) { getstack(s); popstack(s); }}
0 0
- 栈的基本操作
- 栈的基本操作
- 栈 的基本操作。
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 有关Unable to start activity ComponentInfo的一点BUG
- Java核心技术 卷一 笔记十 参数传递设计的调用
- 使用虚拟沙箱安装Tensorflow
- [bzoj3743][Coci2015]Kamp
- HDU3397区间合并 延迟标记
- 栈的基本操作
- 关于IP遇到的坑
- ML2 配置 OVS VxLAN
- python学习语法
- STM32 —— 多路ADC采集
- Mysql 创建定时任务
- 写操作系统只能用C语言和汇编语言吗
- Linux 下编译运行C程序
- Mybatis 中 Cannot find class: BaseResultMap问题所在