数据结构(王道版本,主讲人:闲鱼学长)P19-P31

第三章 栈和队列

3.1.1 栈的基本概念

3.1.2 顺序栈的实现

#include <stdio.h>#define MaxSize 10typedef struct {int date[MaxSize];int top;}SqStack;void InitStack(SqStack& S){S.top = -1;}bool StackEmpty(SqStack S){if (S.top == -1){return true;}else{return false;}}bool Push(SqStack& S, int x){printf("请输入要进栈的数:");while(S.top!=MaxSize - 1){scanf_s("%d", &x);S.top = S.top   1;S.date[S.top] = x;}return true;}void print(SqStack& S){for (int i = 0; i <= S.top; i  ){printf("%d ", S.date[i]);}printf("\n");}bool Pop(SqStack& S, int &x){if (S.top == -1){return false;}x = S.date[S.top];S.top = S.top - 1;return true;}void main(){int x = 0;SqStack S;InitStack(S);StackEmpty(S);Push(S, x);printf("进栈的数有:");print(S);Pop(S,x);printf("出栈的栈顶为:%d\n",x);printf("新栈的数有:");print(S);}//请输入要进栈的数:65 43 43 76 65 34 76 23 43 12//进栈的数有 : 65 43 43 76 65 34 76 23 43 12//出栈的栈顶为 : 12//新栈的数有 : 65 43 43 76 65 34 76 23 43
#include <stdio.h>#define MaxSize 10typedef struct {int date[MaxSize];int top;}SqStack;void InitStack(SqStack& S){S.top = 0;}bool StackEmpty(SqStack S){if (S.top == 0){return true;}else{return false;}}bool Push(SqStack& S, int x){printf("请输入要进栈的数:");while(S.top!=MaxSize){scanf_s("%d", &x);S.date[S.top] = x;S.top = S.top   1;}return true;}void print(SqStack& S){for (int i = 0; i < S.top; i  ){printf("%d ", S.date[i]);}printf("\n");}bool Pop(SqStack& S, int &x){if (S.top ==0){return false;}S.top = S.top - 1;x = S.date[S.top];return true;}void main(){int x = 0;SqStack S;InitStack(S);StackEmpty(S);Push(S, x);printf("进栈的数有:");print(S);Pop(S,x);printf("出栈的栈顶为:%d\n",x);printf("新栈的数有:");print(S);}//请输入要进栈的数:65 43 43 76 65 34 76 23 43 12//进栈的数有 : 65 43 43 76 65 34 76 23 43 12//出栈的栈顶为 : 12//新栈的数有 : 65 43 43 76 65 34 76 23 43

3.1.3 链栈的实现

来源:https://www.icode9.com/content-4-862951.html

(0)

相关推荐