数据结构---栈的学习

#include <iostream>

#include <stdlib.h>

#define MAX_SIZE 100

typedef int ElemType; 

typedef struct  //定义栈结构体

{

int top0;

int top1;

ElemType data[200];

 

}SqStack;

void InitStack(SqStack *&s) //初始化栈

{

s=(SqStack *)malloc(sizeof(SqStack));

s->top0=-1;

s->top1=99;

}

int Push(SqStack *s,int i,int e) //进栈,int i来选择是在那个栈进行,0是在栈0,1表示在栈1

{

if(i==0)

{

if(s->top0==99)

return false;

s->top0++;

s->data[s->top0]=e;

return true;

}

else if(i==1)

{

if(s->top1==199)

return false;

s->top1++;

s->data[s->top1]=e;

return true;

}

else

printf("type is wrong/n");

return 0;

 

}

int Pop(SqStack *s,int i,int e) //出栈同上例

{

if(i==0)

{

if(s->top0==-1)

return false;

e=s->data[s->top0];

s->top0--;

}

else if(i==1)

{

if(s->top1==99)

return false;

e=s->data[s->top1];

s->top1--;

}

else

printf("pop type is wrong/n");

return 0;

}

 

int main()

{

ElemType e;

int i;

SqStack *s=NULL;

InitStack (s);

printf("input int to push :/n");

scanf("%d",&e);

printf("input which stack:(i=0,1?)/n");

scanf("%d",&i);

printf("your choose int %d to push in %d/n",e,i);

Push(s,i,e);

switch (i)

{

case 0:

printf("ruzhan zhi %d/n",s->data[s->top0]);

break;

case 1:

printf("ruzhan zhi %d/n",s->data[s->top1]);

break;

default:

printf("error/n");

break;

}

printf("input which stack:(i=0,1?)/n");

scanf("%d",&i);

Pop(s,i,e);

printf("chu zhan de shi %d/n",e);

free(s);

return 0;

}

运行结果为:
input int to push :
123
input which stack:(i=0,1?)
1
your choose int 123 to push in 1
ruzhan zhi 123
input which stack:(i=0,1?)
1
chu zhan de shi 123
Press any key to continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值