#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct stack
{
node *top;
}q;
void push(int n)
{
node *p;
int a,i;
printf("请输入要入栈的数据:\n");
for(i=1;i<=n;i++)
{
p=(node *)malloc(sizeof(node));
scanf("%d",&a);
p->data=a;
p->next=q.top;
q.top=p;
}
}
void pop(int n)
{
int i;
for(i=1;i<=n;i++)
{
node *p;
p=(node *)malloc(sizeof(node));
p=q.top;
q.top=p->next;
p->next=NULL;
}
}
void print()
{
node *p;
p=q.top;
printf("栈中的数据如下:\n");
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int i,n,k,j;
q.top=NULL;
printf("请输入要入栈的数据个数:\n");
scanf("%d",&n);
push(n);
while(1)
{
printf("请输入想进行的操作:\n");
printf("1、入栈\n2、出栈\n3、查看栈\n4、退出\n");
scanf("%d",&k);
if(k==1)
{
printf("请输入想入栈的数据个数:\n");
scanf("%d",&j);
push(j);
}
else if(k==2)
{
printf("请输入想出栈的数据个数:\n");
scanf("%d",&j);
pop(j);
}
else if(k==3)
{
print();
}
else
break;
}
}