顺序栈的基本操作实现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;
}
#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;
}
相关文章推荐
- 数据结构顺序栈和链栈基本操作----c++实现
- 循环双链表基本操作C++实现
- c++学习笔记—单链表基本操作的实现
- 单链表的基本操作——C++实现
- 【C++数据结构】模版类实现双循环链表的基本操作
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- C++实现有向权图的基本操作,界面友好,操作方便,运行流畅
- c++学习笔记—二叉树基本操作的实现
- C++实现有向权图的基本操作,界面友好,操作方便,运行流畅
- 顺序表的基本操作——C++实现
- 双链表基本操作C++实现
- 【数据结构】双向循环线性表的基本操作--C++/C实现
- 使用C++实现链表的基本操作
- c++学习笔记—二叉树基本操作的实现
- 详细的单链表基本操作C/C++实现
- C++实现shapefile文件的读写和基本功能的操作
- 栈的基本操作实现-c/c++
- c++实现链表的基本操作
- 【C++数据结构】几种单链表的模类板实现及基本操作
- 【数据结构】双向循环线性表的基本操作--C++/C实现