数据结构——栈的基本操作
来源:互联网 发布:淘宝网红模特有哪些 编辑:程序博客网 时间:2024/05/16 08:41
顺序栈:
#include<bits/stdc++.h>#define MAX 100 //栈的最大值using namespace std;typedef int SElemType;struct SqStack{ SElemType *base; SElemType *top; int stacksize;};void menu(){printf("\t1 初始化\n");printf("\t2 入栈\n");printf("\t3 出栈\n");printf("\t4 是否为空栈\n");printf("\t5 退出\n");}void init(SqStack &s){s.base = (SElemType*)malloc(MAX * sizeof(SElemType));s.top = s.base;s.stacksize = MAX;}void push(SqStack &s, SElemType x){if(s.top - s.base >= s.stacksize) { s.base = (SElemType*)realloc(s.base, sizeof(SElemType) * s.stacksize * 2); s.stacksize *= 2; } *(s.top++) = x;}int Stack_pop(SqStack &s){if(s.top == s.base) { printf("Empty!\n"); return 0; }s.top--;return 1;}void display(SqStack &s){if(s.top == s.base){ printf("Empty!\n"); return ;} printf("base ----> top\n");for(int i = 0; s.base + i < s.top; i++)printf("%d ", *(s.base + i)); cout << endl;}void is_Empty(SqStack s){ if(s.top == s.base) puts("Yes"); else puts("No");}int main(){SqStack s;int num, now;while(1){menu();scanf("%d", &num);switch(num){case 1 : init(s); break;case 2 : printf("Input\n"); scanf("%d",&now); push(s, now); display(s); break;case 3 : if(Stack_pop(s)){ printf("after delete\n"); display(s); } break; case 4: is_Empty(s);}if(num == 5) break;}return 0;}
链栈:
#include<bits/stdc++.h>#define N 100using namespace std;typedef int SElemType;typedef struct snode{ SElemType data; struct snode *next; struct snode *pre;}StackNode, *LinkStack;void menu(){printf("\t1 初始化\n");printf("\t2 入栈\n");printf("\t3 出栈\n");printf("\t4 是否为空栈\n");printf("\t5 退出\n");}void init(LinkStack &L, LinkStack &R){L = (LinkStack)malloc(sizeof(StackNode));L->next = L->pre = NULL;R = L;}void push(LinkStack &L, LinkStack &R, int x){ LinkStack nx = (LinkStack)malloc(sizeof(StackNode)); nx->data = x; nx->next = NULL; nx->pre = R; R->next = nx; R = nx;}bool Stack_pop(LinkStack &L, LinkStack &R){ if(L == R) return false; LinkStack p = R->pre; free(R); R = p; R->next = NULL; return true;}void display(LinkStack L, LinkStack R){ if(L == R) { puts("Empty!"); return ; }printf("base ----> top\n");L = L->next;while(L) { printf("%d ", L->data); L = L->next; } printf("\n");}void is_Empty(LinkStack L, LinkStack R){ if(L == R) puts("Yes"); else puts("No");}int main(){LinkStack L, R;int num, now;while(1){menu();scanf("%d", &num);switch(num){case 1 : init(L, R); break;case 2 : printf("Input\n"); scanf("%d",&now); push(L, R, now); display(L, R); break; case 3 : if(Stack_pop(L, R)) { printf("after delete\n"); display(L, R); } else printf("Empty!\n"); break; case 4: is_Empty(L, R);}if(num == 5) break;}return 0;}
阅读全文
0 0
- 数据结构——栈的基本操作
- 数据结构——栈的基本操作
- 数据结构——栈的基本操作
- 数据结构——图的基本操作
- 数据结构——单链表的基本操作
- 数据结构——单链表的基本操作
- 数据结构——单链表的基本操作
- 数据结构——队列的基本操作
- 数据结构——堆栈的基本操作
- 数据结构——字符串的基本操作
- 数据结构—基本树的操作小结
- 数据结构初学————栈的基本操作
- 数据结构——链栈的基本操作
- 数据结构之——栈的创建于基本操作
- 数据结构实验——基于数组的栈基本操作
- 数据结构复习整理——栈的基本操作
- 【数据结构】 栈的基本操作
- 数据结构栈的基本操作
- spring boot 最佳实践(一)--使用jackson
- 读写流-fgetc、fputc、fgets、fputs、fread、ferror、fwrite、clearerr、ungetc
- if语句和if else语句简单运用
- java设计模式
- warning :uninstalling will remove the application data!
- 数据结构——栈的基本操作
- 多线程之生产者消费者模型
- 欢迎使用CSDN-markdown编辑器
- 在javaScript对象字面量中定义访问器属性
- I2C的简单实用案例
- C#学习笔记(二)---在C#中创建类(下)
- dp斜率优化 hdu 2829 Lawrence 题解
- 【LeetCode】50. Pow(x, n)
- 闲谈Android中的@和?符号的引用区别