# 线性栈 **Repository Path**: jiang-dianyun/linear-stack ## Basic Information - **Project Name**: 线性栈 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-03-18 - **Last Updated**: 2022-04-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 线性栈 #我自己后面又写了一个,希望大家多多指教! :smile: #include #include #define MaxSize 10 //定义顺序栈 typedef struct { int data[MaxSize];//最大容量 int top;//栈顶指针 }SqStack; //初始化 void InitStack(SqStack& s) { s.top = -1; } //判断栈是否为空 bool Empt_SqStack(SqStack& s) { if (s.top == -1) return true; else return false; } //入栈 bool Push_SqStack(SqStack& s, int m) { if (s.top == MaxSize - 1) return false; else { s.top++; s.data[s.top] = m; } return true; } //打印 void PrintStack(SqStack& s) { if (Empt_SqStack(s)) { printf("顺序栈为空!"); } else { for (int i = s.top; i >= 0; i--) printf("%d->", s.data[i]); } } //出栈 bool Pop_SqStack(SqStack& s, int& m) { if (Empt_SqStack(s)) return false; else { m = s.data[s.top]; s.top--; } return true; } //读取栈顶元素 bool Gettop(SqStack& s, int& m) { if (Empt_SqStack(s)) return false; else { m = s.data[s.top]; } return true; } int main() { SqStack s; int x;//存储出栈的数据 int n, m; InitStack(s); Empt_SqStack(s); //进栈 printf("请输入元素个数\n"); scanf("%d", &n); printf("请输入你想要入栈的元素:\n"); for (int i = 0; i < n; i++) { scanf("%d", &m); Push_SqStack(s, m); } //出栈 printf("出栈的元素为:\n"); for (int i = 0; i < n; i++) { Pop_SqStack(s, m); printf("%d", &m); } PrintStack(s); Gettop(s, m); printf("\n此时栈顶的元素为:%d\n", &m); return 0; }