顺序栈的置空入栈出栈和返回栈顶元素
来源:互联网 发布:激光打标机软件下载 编辑:程序博客网 时间:2024/05/05 02:29
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 100
#define OK 1
#define FALSE 0
//顺序栈结构体
typedef struct stack{
int data[MAXSIZE];
int top;
}*pstack, stack;
//置空顺序栈
int empty(pstack sq){
sq->top = -1;
printf("顺序栈已经置空!\n");
return OK;
}
//入栈操作
int push(pstack sq){
int x;
printf("请输入你想要入栈的数据:\n");
scanf("%d",&x);
sq->top = ++sq->top;
sq->data[sq->top]=x;
return OK;
}
//出栈操作
int pop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈已经为空!\n");
return FALSE;
}
else
{
printf("出栈数据是:%d\n", sq->data[sq->top]);
sq->top = --sq->top;
return OK;
}
}
//遍历顺序栈中的所有数据
int show(pstack sq){
int i;
if (sq->top == -1)
{
printf("这是个空栈!\n");
return FALSE;
}
else
{
printf("栈中的元素为:\n");
for (i = 0; i <= sq->top; i++)
{
printf("%d ", sq->data[i]);
}
}
printf("\n");
}
//返回顺序栈栈顶元素
int getstacktop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈中元素为空!\n");
return FALSE;
}
else
{
printf("栈顶元素是:%d\n", sq->data[sq->top]);
return OK;
}
}
int main()
{
stack S;
int n;
do{
printf("*******************\n");
printf("1.置空顺序栈\n2.进栈操作\n3.出栈操作\n4.顺序输出栈中元素\n5.返回栈顶元素\n6.退出程序\n");
printf("*******************\n");
printf("请输入你的选择:\n");
scanf("%d",&n);
switch (n){
case 1:
empty(&S);
break;
case 2:
push(&S);
break;
case 3:
pop(&S);
break;
case 4:
show(&S);
break;
case 5:
getstacktop(&S);
break;
case 6:
break;
}
} while (n!=6);
return 0;
}
#include "stdlib.h"
#define MAXSIZE 100
#define OK 1
#define FALSE 0
//顺序栈结构体
typedef struct stack{
int data[MAXSIZE];
int top;
}*pstack, stack;
//置空顺序栈
int empty(pstack sq){
sq->top = -1;
printf("顺序栈已经置空!\n");
return OK;
}
//入栈操作
int push(pstack sq){
int x;
printf("请输入你想要入栈的数据:\n");
scanf("%d",&x);
sq->top = ++sq->top;
sq->data[sq->top]=x;
return OK;
}
//出栈操作
int pop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈已经为空!\n");
return FALSE;
}
else
{
printf("出栈数据是:%d\n", sq->data[sq->top]);
sq->top = --sq->top;
return OK;
}
}
//遍历顺序栈中的所有数据
int show(pstack sq){
int i;
if (sq->top == -1)
{
printf("这是个空栈!\n");
return FALSE;
}
else
{
printf("栈中的元素为:\n");
for (i = 0; i <= sq->top; i++)
{
printf("%d ", sq->data[i]);
}
}
printf("\n");
}
//返回顺序栈栈顶元素
int getstacktop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈中元素为空!\n");
return FALSE;
}
else
{
printf("栈顶元素是:%d\n", sq->data[sq->top]);
return OK;
}
}
int main()
{
stack S;
int n;
do{
printf("*******************\n");
printf("1.置空顺序栈\n2.进栈操作\n3.出栈操作\n4.顺序输出栈中元素\n5.返回栈顶元素\n6.退出程序\n");
printf("*******************\n");
printf("请输入你的选择:\n");
scanf("%d",&n);
switch (n){
case 1:
empty(&S);
break;
case 2:
push(&S);
break;
case 3:
pop(&S);
break;
case 4:
show(&S);
break;
case 5:
getstacktop(&S);
break;
case 6:
break;
}
} while (n!=6);
return 0;
}
0 0
- 顺序栈的置空入栈出栈和返回栈顶元素
- 栈的创建、返回栈顶元素、删除栈顶元素、插入栈顶元素
- 获取顺序栈的栈顶元素
- 链栈的置空入栈,出栈,和返回栈顶元素操作
- c++ pop不返回栈顶元素的机制
- 栈的排序,栈顶元素最大.
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 栈模板的链表实现(包含头结点),实现了栈的压入,栈的弹出,返回栈顶元素,排序等操作
- c++ 实现顺序栈类(初始化,入栈,退栈。读栈顶元素以及顺序输出栈顶指针与栈中的元素<<计算机软件技术基础 徐士良>>
- 返回min元素的栈
- C++实现最小堆及插入,调整顺序,删除堆顶元素的操作
- C++栈的初始化,入栈,出栈,获取栈顶元素等操作
- 元素出入栈的顺序合法性判断
- stack容器的用法:入栈、出栈、访问栈顶元素,判断是否为空
- 数据结构--栈的构造、插入、删除、得到栈顶元素操作
- android监听应用缓存和任务栈顶的方法
- 切换tabbar并返回栈顶 崩溃问题
- 页面事件的顺序和返回 -- [翻译]
- Oracle rowid
- 使用函数登录,并返回用户类型
- super4412下调试cmos驱动是的nternal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM问题解决
- 从SIFT到SURF
- jboss开启远程访问
- 顺序栈的置空入栈出栈和返回栈顶元素
- MySQL导入sql脚本错误:2006 - MySQL server has gone away
- 二叉查找树的类模板实现
- android中的返回键与Activity
- HDU 1513 Palindrome 求回文串
- macbook safari上不了网,但是QQ可以上网的问题
- Eclipse中设置JVM内存
- Codeforces Round #275
- 错误:No resource found that matches the given name 'Theme.AppCompat.Light'解决方法