看到有个人发帖询问栈的问题.
这个是代码!
#include<stdio.h>
#define LEN sizeof(struct sqstack)
#include<malloc.h>
struct sqstack
{
char data;
struct sqstack *top;
};
struct sqstack *st;
void push(char k)
{
struct sqstack *newst=(struct sqstack*)malloc(LEN);
newst->data=k;
newst->top=st;
st=newst;
}
char pop()
{
char value;
struct sqstack *t;
value=st->data;
t=st;
st=st->top;
free(t);
return value;
}
void main()
{
int n;
printf("请输入字符个数 n:\n");
scanf("%d",&n);
char *arr=new char[n];
printf("请输入n个字符:\n");
for(int i=0;i<n;i++)
scanf("%c",&arr[i]);
printf("\n入栈顺序:");
for( i=0;i<n;i++)
{
printf("%c",arr[i]);
push(arr[i]);
}
printf("\n");
printf("输出");
for( i=0;i<n;i++)
{printf("%c",pop());}
printf("\n");
delete arr;
}