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

2024年11月16日 20:58
有1个网友回答
网友(1):

#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();
}
/*-------------Suffix ----------------------------*/
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;
}
}

}