数据结构-栈的静态顺序存储表示-初始化压栈弹栈
2017-09-19 21:56
316 查看
和上一个博客,动态方式一样
#include<iostream>
#include<stdlib.h>
#define MAX_STACKSIZE 100
#define OK 1
#define ERROR 0
using namespace std;
typedef struct sqstack
{
int stack_array[MAX_STACKSIZE];
int top;
int bottom;
}SqStack;
SqStack s;
void init_stack()
{
s.top=s.bottom=0;
}
int insert_stack(int num)
{
if(s.top>=MAX_STACKSIZE-1) return ERROR;
s.stack_array[++s.top]=num;
return OK;
}
int pop_stack(int *e)
{
if(s.top==0) return ERROR;
*e=s.stack_array[s.top--];
return OK;
}
int show_stack()
{
if(s.top==s.bottom) return ERROR;
for(int i=1;i<=s.top;i++)
{
cout<<s.stack_array[i]<<" ";
}
cout<<endl;
return OK;
}
int main()
{
init_stack();
while(1)
{
int temp;
cout<<"压栈1弹栈2显示3"<<endl;
cin>>temp;
if(temp==1)
{
int num;
cin>>num;
if(!insert_stack(num))
{
cout<<"插入失败"<<endl;
}
}
else if(temp==2)
{
int num;
if(pop_stack(&num))
{
cout<<"弹出元素:"<<num<<endl;
}
else cout<<"弹栈失败"<<endl;
}
else
{
if(!show_stack()) cout<<"空栈"<<endl;
}
}
return 0;
}
/*
1
12
1
34
1
2
1
34
1
67
1
567
3
1
234
1
3
2
2
2
2
2
2
*/
#include<iostream>
#include<stdlib.h>
#define MAX_STACKSIZE 100
#define OK 1
#define ERROR 0
using namespace std;
typedef struct sqstack
{
int stack_array[MAX_STACKSIZE];
int top;
int bottom;
}SqStack;
SqStack s;
void init_stack()
{
s.top=s.bottom=0;
}
int insert_stack(int num)
{
if(s.top>=MAX_STACKSIZE-1) return ERROR;
s.stack_array[++s.top]=num;
return OK;
}
int pop_stack(int *e)
{
if(s.top==0) return ERROR;
*e=s.stack_array[s.top--];
return OK;
}
int show_stack()
{
if(s.top==s.bottom) return ERROR;
for(int i=1;i<=s.top;i++)
{
cout<<s.stack_array[i]<<" ";
}
cout<<endl;
return OK;
}
int main()
{
init_stack();
while(1)
{
int temp;
cout<<"压栈1弹栈2显示3"<<endl;
cin>>temp;
if(temp==1)
{
int num;
cin>>num;
if(!insert_stack(num))
{
cout<<"插入失败"<<endl;
}
}
else if(temp==2)
{
int num;
if(pop_stack(&num))
{
cout<<"弹出元素:"<<num<<endl;
}
else cout<<"弹栈失败"<<endl;
}
else
{
if(!show_stack()) cout<<"空栈"<<endl;
}
}
return 0;
}
/*
1
12
1
34
1
2
1
34
1
67
1
567
3
1
234
1
3
2
2
2
2
2
2
*/
相关文章推荐
- 数据结构-栈的动态顺序存储表示-初始化压栈弹栈
- javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现
- 数据结构--串--定长顺序存储表示
- 队列的顺序存储和链式表示方法-数据结构学习笔记2.3
- 《数据结构》(C语言版)——栈的顺序存储表示
- 【数据结构】算法3.1-3.2 栈的顺序存储表示
- 栈的静态顺序存储表示
- 数据结构--数组和广义表--数组的顺序存储表示和实现
- 数据结构Java实现——④数组——>稀疏矩阵三元组顺序存储
- 非静态类的静态成员与非静态成员的初始化顺序
- Java的初始化块、静态初始化块、构造函数的执行顺序
- java的初始化块、静态初始化块、构造函数的执行顺序
- 数据结构-栈的顺序存储
- java中类/对象的初始化顺序以及静态代码块的使用
- 数据结构(C语言)读书笔记10:串的定长顺序存储表示
- [SDUT](1130)数据结构上机测试1:顺序表的应用 ---顺序存储(线性表)
- 数据结构:有向图的十字链表存储表示(c实现)
- java静态方法,静态变量,初始化顺序
- 数据结构编程笔记十二:第五章 数组和广义表 数组顺序存储的实现
- 数据结构:栈的顺序存储(二)