数据结构 顺序栈初始化及入出栈
发布日期:2021-05-10 03:16:58 浏览次数:20 分类:精选文章

本文共 673 字,大约阅读时间需要 2 分钟。

#include
#include
using namespace std;#define Stack_size 20typedef struct Stack{ int elem[Stack_size]; int top;}Stack;Stack * init(){ //初始化一个空栈 Stack *S=(Stack *)malloc(sizeof(Stack)); S->top=-1; return S;}void print(Stack *S){ //打印栈元素 for(int i=0;i<=S->top;i++){ cout<
elem[i]<<" "; } cout<
top==Stack_size-1)return -1; S->top++; S->elem[S->top]=num; return 1;}int pop(Stack *S){ //出栈 if(S->top!=-1){ int num=S->elem[S->top--]; return num; } return -99999;}int main(int argc, char const *argv[]){ Stack *S; S=init(); push(S,1); print(S); push(S,2); push(S,3); push(S,4); print(S); cout<<"pop "<
<
上一篇:数据结构 双端栈初始化和入出栈
下一篇:数据结构 双链表的增删改查

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月02日 12时48分53秒