c语言求入栈出栈的序列,求C语言 栈代码要求有入栈和出栈功能谢谢

求C语言 栈代码要求有入栈和出栈功能谢谢

答案:1  信息版本:手机版

解决时间 2019-10-05 06:29

已解决

2019-10-05 01:27

求C语言 栈代码要求有入栈和出栈功能谢谢

最佳答案

2019-10-05 02:18

#include "stdio.h"

#define MAXLEN 100

typedef struct stacknode

{

int data;

struct stacknode *next;

}stacknode;

typedef struct

{

stacknode *top;

}linkstack;

int Push (linkstack *s,int x)

{

stacknode *p=(stacknode*)malloc(sizeof(stacknode));

p->data=x;

p->next=s->top;

x=p->data;

s->top=p->next;

free(p);

return x;

}

int Pop(linkstack *s)

{

int x;

stacknode *p=s->top;

x=p->data;

s->top=p->next;

free(p);

return x;

}

void ShowStack (linkstack *s)

{

stacknode *p=s->top;

if(p==NULL)

printf("\t\tstack is empty.\n");

else

{printf("\t\tThe char of zhan is :");

while(p!=NULL)

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

p=p->next;

}

printf("\n");

}

}

void Conversion(int n)

{

linkstack s;

stacknode *p=(stacknode*)malloc(sizeof(stacknode));

int x;

s.top=NULL;

do

{ x=n%2;

n=n/2;

p->next=s.top;

s.top=p;

s.top->data=x;

}

while (n);

printf("\n\t\tThe converted binary int is:");

while(s.top)

{printf("%d",s.top->data);

p=s.top;

s.top=s.top->next;

free(p);

}

printf("\n");

getchar();

}

void Suffix()

{

char str[MAXLEN];

char stack[MAXLEN];

char exp[MAXLEN];

char ch;

int n,i,j,t,top=0;

printf("\n\t\t*input the expression,finished with #*\n");

printf("\n\t\tInput an interger expression:");

i=0;

do{i++;

scanf("%c",&str[i]);

}

while(str[i]!='#'&&i!=MAXLEN);

n=i;

t=1;

i=1;

ch=str[i];

i++;

while(ch!='#')

{

switch(ch)

{case '(':

top++;stack[top]=ch;

break;

case ')':

while(stack[top]!='(')

{ exp[t++]=stack[top--];

exp[t++]=',';

}

top--;

break;

case '+':

case '-':

while (top!=0&&stack[top]!='(')

{ exp[t++]=stack[top--];

exp[t++]=',';

}

stack[++top]=ch;

break;

case '*':

case '/':

while(stack[top]=='*'||stack[top]=='/')

{ exp[t++]=stack[top--];

exp[t++]=',';

}

stack[++top]=ch;

break;

case ' ':

break;

default:

while(ch>='0'&&ch<='9')

{

exp[t++]=ch;

ch=str[i++];

}

i--;

exp[t++]=',';

}

while(top!=0)

{

exp[t++]=stack[top--];

}

printf("\n\t\tInput expresstion:");

for(j=1;j printf("%c",str[j]);

printf("\n\t\tthe rear expression:");

for(j=1;j printf("%c",exp[j]);

printf("\n");

}

}

void main()

{

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

int i=1,j=1,choice,val,n;

char a;

s->top=NULL;

while(i)

{

printf("\n\n\n\n");

printf("\n\t\t\tThe stack list system\n");

printf("\n\t\t*************************************************");

printf("\n\t\t* 1--------Enter stack *");

printf("\n\t\t* 2--------Out stack *");

printf("\n\t\t* 3--------Show *");

printf("\n\t\t* 4--------Transfer *");

printf("\n\t\t* 5--------Rear *");

printf("\n\t\t* 0--------Return *");

printf("\n\t\t*************************************************");

printf("\n\t\tPlease choice the number(0----5):");

choice=getchar();

getchar ();

switch(choice)

{

case '1':

while(j)

{

printf("\t\tPlease enter an interger,0 to exit:");

scanf("%d",&val);getchar();

if(val!=0)

Push(s,val);

else

j=0;

}break;

case '2':if(s->top!=NULL)

printf("\t\tOut element is:%6d\n",Pop(s));break;

case '3':

ShowStack(s);break;

case '4':

printf("\n\t\tPlease input an interger:");

scanf("%d",&n);

Conversion(n);

break;

case '5':

Suffix();getchar();

break;

case '0':

i=0;

break;

default:;

}

if(choice=='1'||choice=='2'||choice=='3'||choice=='4'||choice=='5')

{

printf("\n\t\tPress enter to continue,press any key to return main neun");

a=getchar();

if(a!='\xA')

i=0;

}

}

}

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

大家都在看

推荐资讯

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值