顺序表输入栈元素c语言,用顺序表实现栈的基本操作(基于C语言)求解答

/顺序栈

#include

#include

#include

#define STACK_INIT_SIZE 100;

#define STACKINCREMENT 10;

typedef struct

{

int *base;

int *top;

int stacksize;

}SqStack;

typedef int ElemType;

int InitStack(SqStack &S) //为栈S分配存储空间,并置S为空栈

{

int size = STACK_INIT_SIZE;

S。

base=(int *)malloc(size*sizeof(ElemType));

if(!S。base)

return 0;

S。top=S。base; //置栈S为空栈

S。

stacksize=STACK_INIT_SIZE;

return 1;

}

int GetTop(SqStack S,int &e) //若栈不空,则用e返回S的栈顶元素

{

if(S。

top==S。base) return 0;

e=*(S。top-1);

return 1;

}

int Push(SqStack &S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/

{ if(S。

top-S。base>=S。stacksize) /*栈满,追加存储空间*/

{

int stackinvrement = STACKINCREMENT;

S。base=(ElemType *) realloc(S。

base,(S。stacksize stackinvrement)*sizeof(ElemType));

if(!S。base)

return 0; /*存储分配失败*/

S。stacksize =STACKINCREMENT;

}

*S。

top =e;

return 1;

}

int Pop(SqStack &S,int &e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/

{ if(S。

top==S。base) return 0;

e=*--S。top;

return 1;

}

void OutputStack(SqStack &S)

{int *q;

q=S。

top-1;

for(int i=0;i

#include

typedef struct SNode

{

int data;

struct SNode *next;

}SNode,*LinkStack;

LinkStack top;

LinkStack PushStack(LinkStack top,int x) //入栈

{

LinkStack s;

s=(LinkStack)malloc(sizeof(SNode));

s->data=x;

s->next=top;

top=s;

return top;

}

LinkStack PopStack(LinkStack top) //退栈

{

LinkStack p;

if(top!=NULL)

{

p=top;

top=top->next;

free(p);

printf("退栈已完成

");

return top;

}

else printf("栈是空的,无法退栈!

"); return 0;

}

int GetStackTop(LinkStack top) //取栈顶元素

{

return top->data;

}

bool IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型

{

return top==NULL ? true:false;

}

void Print()

{

SNode *p;

p=top;

if(IsEmpty())

{

printf("The stack is empty!

");

return;

}

while(p)

{

printf("%d ", p->data);

p=p->next;

}

printf("

");

}

void main()

{

int x,a,b;

char m;

do { printf("

");

printf("###############链栈的基本操作##################

");

printf("××××××××1。

置空栈××××××××××

");

printf("××××××××2。进栈×××××××××××

");

printf("××××××××3。退栈×××××××××××

");

printf("××××××××4。

取栈顶元素××××××××

");

printf("××××××××5。

退出程序×××××××××

");

printf("##############################################

");

printf("

请选择一个字符:");

scanf("%c",&m);

switch(m){

case '1':

top=NULL;

printf("

栈已置空!");

break;

case '2':

printf("

请输入要进栈的元素个数是:");

scanf("%d",&a);

printf("

请输入要进栈的%d个元素:",a);

for(b=0;b。

全部

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值