您的位置:首页 > 编程语言 > C语言/C++

顺序栈的基本操作实现c++

2016-08-06 20:48 274 查看
注:若有问题或需要补充的内容请告之,新手上路,谢谢。

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<ctype.h>
using namespace std;

#define MAXSIZE 100

/*结构体*/
typedef struct lnode
{
int data[MAXSIZE];
int top;
}seqstack;
/*初始化*/
seqstack *init_stack()
{
seqstack *s;
s=(seqstack*)malloc(sizeof(seqstack));
s->top=-1;
return s;
}
/*判断栈空*/
bool is_empty_seqstack(seqstack s)
{
if(s.top==-1)
{
return true;
}
else
{
return false;
}
}
/*销毁栈*/
void delete_stack(seqstack *s)
{
s->top=-1;
printf("栈销毁成功\n");
}
/*判断栈满*/
bool is_full_seqstack(seqstack s)
{
if(s.top==MAXSIZE-1)
{
return true;
}
else
{
return false;
}
}
/*进栈*/
void push(seqstack *s,int e)
{
if(is_full_seqstack(*s))
{
printf("栈满");
}
else
{
s->data[++s->top]=e;
}
}
/*出栈*/
void pop(seqstack *s,int &e)
{
if(is_empty_seqstack(*s))
{
printf("栈空");
}
else
{
e=s->data[s->top--];
}
}
/*取栈顶元素*/
void get_top(seqstack s,int &e)
{
if(is_empty_seqstack(s))
{
printf("栈空");
}
else
{
e=s.data[s.top];
}
}
/*输出栈*/
void print(seqstack s)
{
while(!is_empty_seqstack(s))
{
printf("%d",s.data[s.top--]);
}
}
int main()
{
seqstack *s;
int l,e;
s=init_stack();
printf("请输入需要入栈的元素个数:");
scanf("%d",&l);
printf("请依次输入入栈的元素:");
for(;l>0;l--)
{
scanf("%d",&e);
push(s,e);
}
printf("依次出栈的元素为:");
while(!is_empty_seqstack(*s))
{
pop(s,e);
printf("%d",e);
}
printf("\n");
delete_stack(s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 顺序栈