#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct
{
int *base1;
int *base2;
int *top1;
int *top2;
}sqstack;
int leftstacklength=0;
int rightstacklength=0;
void menu()
{
printf(" |************************************|\n");
printf(" |* ***|\n");
printf(" |* 1--popr ***|\n");
printf(" |* 2--popl ***|\n");
printf(" |* 3--empty ***|\n");
printf(" |* 4--full ***|\n");
printf(" |* 5--pushr ***|\n");
printf(" |* 6--pushl ***|\n");
printf(" |* 7--clear ***|\n");
printf(" |* 0--BREAK ***|\n");
printf(" |* ***|\n");
printf(" |***********************************|\n");
}
sqstack initstack(sqstack H)
{
int stackmaxlength;
printf("\nplease input the max length of twostacks: ");
scanf("%d",&stackmaxlength);
H.base1=(int *)malloc(stackmaxlength*sizeof(int));
if(!H.base1) exit(1);
H.top1=H.base1;
H.base2=H.base1+stackmaxlength-1;
H.top2=H.base2;
printf("\INIT left stacks");
printf("\nplease enter the left stack data,end with 0:\n");
for(;scanf("%d",H.top1); (H.top1++,leftstacklength++))
{
if(*H.top1==0) break;
if(H.top1>=H.top2)
{ printf("\n twostacks FULL!!\n");
H.top1++;
leftstacklength++;break;
}
}
if(H.top1>H.top2)
{ printf("\n twostacks FULL!!\n");}
else
{printf("\n INIT right stack");
printf("\n please enter the data to right stack,end with 0:\n");
for(;scanf("%d",H.top2);(H.top2--,rightstacklength++))
{
if(*H.top2==0) break;
if(H.top1>=H.top2)
{ printf("\n twostacks FULL!!\n");
H.top2--;
rightstacklength++;break;
}
}
}
return H;
}
sqstack clear(sqstack s)
{
int e;
if(s.base1==s.top1) printf("\nleft stack empty\n");
else
{
printf("\nthe %d data of left stack is:\n",leftstacklength);
while(s.base1!=s.top1)
{
s.top1--;
e=*s.top1;
printf("%d ",e);
}
}
if(s.base2==s.top2) printf("\n right stack empty:\n");
else
{
printf("\nthe %d data of right stack is:\n",rightstacklength);
while(s.base2!=s.top2)
{
++s.top2;
e=*s.top2;
printf("%d ",e);
}
}
if(s.top1==s.base1&&s.top2==s.base2)
{printf("\ntwostack is been empty!!,\n");
printf("again other programm please NINT the two stacks!!\n");}
leftstacklength=0;
rightstacklength=0;
return s;
}
sqstack popl(sqstack s)
{
int e;
int *p;
if(s.top1==s.base1)
{
printf("\nleft stack empty!!!\n");
}
else
{ p=--s.top1;
e=*p;
leftstacklength--;
printf("\n the pop data of left stack is:%d",e);
}
if(s.top1>s.top2)
{ printf("\ntwo stack FULL!!!\n");}
return s;
}
sqstack popr(sqstack s)
{
int e;
int *p;
if( s.top2==s.base2)
{
printf("\nright stack empty!!!\n");
}
else
{ p=++s.top2;
e=*p;
rightstacklength--;
printf("\n the pop data of right stack is:%d",e);
}
printf("\n\n");
return s;
}
void empty(sqstack s)
{ if((s.top1==s.base1)&&(s.top2==s.base2))
printf("\ntwostack empty!!\n\n");
else
{ printf("\n two stack is not empty!!\n");}
if(s.top1==s.base1)
{printf("\n left stack empty!!\n"); }
if(s.top2==s.base2)
{printf("\n right stack empty!!\n");}
}
void full(sqstack s)
{
if(s.top1>s.top2)
{ printf("\n twostacks FULL!!\n\n");}
else
{ printf("\n twostacks is not FULL!!\n\n");}
}
sqstack pushl(sqstack s)
{
int data;
if(s.top1>s.top2)
{ printf("\n twostacks FULL!!\n");}
else
{ printf("\nenter a data to left stack: ");
scanf("%d",&data);
*s.top1=data;
s.top1 ++;
leftstacklength++;
}
printf("\n\n");
return s;
}
{ int data;sqstack pushr(sqstack s)
if(s.top1>s.top2)
{ printf("\n twostacks FULL!!\n");}
else
{ printf("\n enter a data to right stack: ");
scanf("%d",&data);
*s.top2=data;
s.top2 --;
rightstacklength++;
}
printf("\n\n");
return s;
}
void print(sqstack s)
{ int *left,*right;
int e;
left=s.top1;
right=s.top2;
if(s.base1==left) printf("\nleft stack empty,%d data in left stack!!\n",leftstacklength);
else
{
printf("\nthe %d data of left stack is:\n",leftstacklength);
while(s.base1!=left)
{
left--;
e=*left;
printf("%d ",e);
}
}
if(s.base2==right) printf("\nright stack empty,%d data in right stack!!\n",rightstacklength);
else
{
printf("\nthe %d data of right stack is:\n",rightstacklength);
while(s.base2!=right)
{
++right;
e=*right;
printf("%d ",e);
}
}
if(s.top1==s.base1&&s.top2==s.base2)
{printf("\ntwostack is empty!!,no data in the twostack\n");}
}
void main()
{
sqstack H;
char choose,yes_no;
H=initstack(H);
printf("\n");
do
{
menu();
printf("\nplease choose the programm number(0-9):");
choose=getche();
switch(choose)
{
case '1': H=popr(H);print(H);break;
case '2': H=popl(H);print(H);break;
case '3': empty(H); break;
case '4': full(H);print(H); break;
case '5': H=pushr(H);print(H);printf("\n");break;
case '6': H=pushl(H);print(H);printf("\n");break;
case '7': H=clear(H);printf("\n");break;
case '0': break;
default: printf("\nERROR CHOOSE NUMBER !!\n");break;
}
printf("\nagain the program enter Y(y): ");
yes_no=getch();
printf("\n\n");
} while(yes_no=='y'||yes_no=='Y');
}
没有合适的资源?快使用搜索试试~ 我知道了~
会定义顺序栈和链栈的结点类型。
共1个文件
c:1个
需积分: 12 8 下载量 151 浏览量
2008-09-10
16:37:43
上传
评论 2
收藏 2KB RAR 举报
温馨提示
二、 实验要求 1、 定义栈的存储结构。 2、 编写程序实现双向栈的基本操作:1)初始化;2)判断栈是否为空;3)判断栈是否已满;4)入栈;5)出栈;6)清空栈;7)取栈顶元素。 3、 所写源代码编程风格良好,有详细注释。 4、 程序运行界面良好,使用菜单实现每个基本操作。
资源推荐
资源详情
资源评论
收起资源包目录
2双栈的实现.rar (1个子文件)
2双栈的实现.C 6KB
共 1 条
- 1
资源评论
lanys2008
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功