进出栈
来源:互联网 发布:网络蛋糕店 编辑:程序博客网 时间:2024/04/30 00:00
很久没有看多数据结构了,今天遇到一道题,哎!不会啊,
一狠心就写一遍了!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
int * base;
int * top;
int stacksize;
}Sqstack;
int InitStack(Sqstack & S)
{
S.base = (int *) malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
void DisplayStack(Sqstack S)
{
if(S.top == S.base)
cout<<"\t\t\t栈是空栈\n";
int * p;
printf("\t\t\t");
for(p=S.base; p<S.top; p++)
printf("%3d",*p);
cout<<endl;
}
int menu_select()
{
char c;
do {
system("cls");
cout<<"\t\t*************************\n";
cout<<"\t\t 1.入栈 \n";
cout<<"\t\t 2.出栈 \n";
cout<<"\t\t 0.退出 \n";
cout<<"\t\t*************************\n";
cout<<"\t\t给出你的选择(0-2):";
c = getchar();
}while(c<'0' ||c>'2');
return (c-'0');
}
void Push(Sqstack & S)
{
int n,e;
printf("请输入此次近栈的元素个数");
cin>>n;
while(n > 0)
{
if(S.top - S.base >=S.stacksize)
{
S.base = (int *)realloc(S.base,(S.stacksize + LISTINCREMENT)*sizeof(int));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += LISTINCREMENT;
}
cout<<"\t\t请输入入栈元素:";
cin >> e;
*S.top ++= e;
n --;
}
DisplayStack(S);
}
void Pop(Sqstack &S)
{
int n,e;
if(S.top == S.base)
cout<<"\t\t栈是空栈,无法进行操作!\n";
else
{
cout<<"请输入此次出栈的元素个数";
cin >>n;
while(n < 1 || n > S.top - S.base)
{
cout<<"输入有误,请重新输入:";
cin>>n;
}
while(n > 0)
{
e = * --S.top;
cout<<"出栈元素是:"<<e<<endl;
n --;
}
}
cout<<"\t\t栈内剩余元素:\n";
DisplayStack(S);
}
void main()
{
int f;
Sqstack S;
f = InitStack(S);
if(f == 1)
{
for(;;)
{
switch(menu_select())
{
case 1:
cout<<"\t\t\t对于以下元素进行入栈操作!\n";
DisplayStack(S);
Push(S);
cout<<"\t\t\t";
system("pause");
break;
case 2:
cout<<"\t\t\t对于以下元素进行出栈操作\n";
DisplayStack(S);
Pop(S);
cout<<"\t\t\t";
system("pause");
break;
case 0:
cout<<"\t\t\t欢迎使用!\n";
system("pause");
exit(0);
}
}
}
}
- 进出栈
- 进出栈
- 简单进出栈程序
- STL栈 - 火车进出栈
- 栈的进出序列问题
- 火车进出站问题---栈
- 序列是否能够由进出栈得到
- 卡特兰数(进出栈情况)
- HDU-1022 火车进出站问题【栈】
- 栈与队列(一)进出栈(含二转十进制栈应用)
- 机试准备 进出栈的迭代 解法
- 数据结构 ——进出栈操作的算法
- fragment的学习(2)传递参数和进出栈
- 全排列、判断栈序、火车进出站
- 进出异常
- 进出登记
- 进出队列
- Fastjson序列化map时,保留map的进出栈顺序的方法
- 《研磨struts2》 前言
- 断开客户端samba用户链接
- 在 vs2008 里使用 SGI STL 的内存池特性
- Google Q2 财报:营收增长35% 净利润27.9亿美元
- sh: ./boa: not found问题解决
- 进出栈
- 《研磨struts2》 第一章 Struts2概述 之 MVC模式
- C/C++中static关键字详解
- java protected 与默认权限的区别
- Freemarker 空值处理
- 工作中的SQL Server常用语法
- 《研磨struts2》 第一章 Struts2概述 之 Struts2基础
- Android的Activity屏幕切换动画-左右滑动切换
- Oracle带游标的存储过程在plus中的调用实例