数据结构栈和队列作业
来源:互联网 发布:无光源网络是什么意思 编辑:程序博客网 时间:2024/06/17 12:58
#include <stdio.h>
{
StackElementType elem[Stack_Size]; /*用来存放栈中元素的一维数组*/
int top; /*用来存放栈顶元素的下标,top为-1表示空栈*/
}SeqStack;
void InitStack(SeqStack * S)
}
int SeqStackFull(SeqStack * S)
{
if(S->top>Stack_Size)
return 1;
else
return 0;
int Push(SeqStack * S,StackElementType x)
{
printf("栈已满 进栈失败!\n"); /*表示栈已满*/
return -1;
}
S->top++; /*修改栈顶指针*/
S->elem[S->top] = x; /*x进栈*/
printf("进栈成功!\n");
return 0;
}
//顺序栈出栈运算
int Pop(SeqStack * S,StackElementType * x)
{
printf("SeqStack is Empty\n"); /*表示栈为空*/
*x= S->elem[S->top]; /*栈顶元素赋给x*/
S->top--; /*修改栈顶指针*/
int GetTop(SeqStack * S,StackElementType * x)
*x = S->elem[S->top]; /*将栈顶元素赋给x*/
{
SeqStack m;
StackElementType n;
StackElementType k;
InitStack(&m);
Push(&m,&n);
printf("%d\n",GetTop(&m));/*读栈顶元素*/
Pop(&m,&k);
printf("%d\n",GetTop(&m));
}
typedef int StackElementType;
#define Stack_Size 50 /*设栈中元素个数为50*/
{
StackElementType elem[Stack_Size]; /*用来存放栈中元素的一维数组*/
int top; /*用来存放栈顶元素的下标,top为-1表示空栈*/
}SeqStack;
void InitStack(SeqStack * S)
{
/*构造一个空栈S*/
S->top=-1;
printf("初始化成功!\n");}
int SeqStackFull(SeqStack * S)
{
if(S->top>Stack_Size)
return 1;
else
return 0;
}
int Push(SeqStack * S,StackElementType x)
{
/*将x置入S栈新栈顶*/
if(S->top==Stack_Size-1){
printf("栈已满 进栈失败!\n"); /*表示栈已满*/
return -1;
}
S->top++; /*修改栈顶指针*/
S->elem[S->top] = x; /*x进栈*/
printf("进栈成功!\n");
return 0;
}
//顺序栈出栈运算
int Pop(SeqStack * S,StackElementType * x)
{
/*将S栈顶元素弹出,放到x所指的存储空间中带出*/
if(S->top==-1){
printf("SeqStack is Empty\n"); /*表示栈为空*/
return -1;
else
{*x= S->elem[S->top]; /*栈顶元素赋给x*/
S->top--; /*修改栈顶指针*/
return 0;
}
}int GetTop(SeqStack * S,StackElementType * x)
{
/*将栈顶元素读出,放到x所指的存储空间中,栈顶指针保持不变*/
if(S->top==-1)
{
printf("SeqStack is Empty\n"); /*表示栈为空*/return -1 ;
}
else
{*x = S->elem[S->top]; /*将栈顶元素赋给x*/
return 0;
}
}
{
SeqStack m;
StackElementType n;
StackElementType k;
InitStack(&m);
Push(&m,&n);
printf("%d\n",GetTop(&m));/*读栈顶元素*/
Pop(&m,&k);
printf("%d\n",GetTop(&m));
}
阅读全文
0 0
- 数据结构栈和队列作业
- 【数据结构作业】队列
- 数据结构-栈和队列
- 数据结构-栈和队列
- 数据结构--栈和队列
- 数据结构-栈和队列
- 数据结构-栈和队列
- 数据结构 栈和队列
- 数据结构-栈和队列
- 数据结构-栈和队列
- 数据结构-栈和队列
- 数据结构:栈和队列
- 数据结构-栈和队列
- 数据结构--栈和队列
- 【数据结构】队列和栈
- 数据结构--栈和队列
- 【数据结构】-栈和队列
- 数据结构 栈和队列
- IDEA的project和module
- IntelliJ IDEA 学习笔记
- 要注意SecureCrt缓冲区的大小
- Struts2框架
- python 文件读写
- 数据结构栈和队列作业
- Linux内核裁剪的具体过程和方法
- 【Scikit-Learn 中文文档】多类和多标签算法
- 胶囊间的动态路由(2)
- Appium的安装与使用
- JAVASE之String
- 工具类分享之《JsonUtil》
- es6基础笔记整理
- 654. Maximum Binary Tree