#include<myhead.h>
typedef struct student
{
int name[20];
int age;
int score;
}student;
typedef struct node
{
student data;
struct node *next;
}Node;
typedef struct
{
int len;
Node *top;
}Stack,*Pstack;
Pstack creat_stack()
{
Pstack p=malloc(sizeof(Stack));
if(p==NULL)
{
printf("创建失败\n");
return NULL;
}
p->len=0;
p->top=NULL;
return p;
}
int input_stack(Pstack L)
{
if(L==NULL)
{
printf("链栈不存在或空\n");
return -1;
}
Node *p;
student e[10];
int i;
for(i=0;i<4;i++)
{
p=malloc(sizeof(Node));
printf("请输入第%d个信息\n",i+1);
scanf("%ls %d %d",e[i].name,&e[i].score,&e[i].age);
p->data=e[i];
p->next=L->top;
L->top=p;
L->len++;
}
printf("\n");
return 0;
}
int output_stack(Pstack L)
{
if(L==NULL)
{
printf("链栈不存在或空\n");
return -1;
}
int i;
Node *t=L->top;
for(i=0;i<L->len;i++)
{
printf("姓名:%ls成绩:%d年龄:%d",t->data.name,t->data.score,t->data.age);
t=t->next;
}
printf("\n");
return 0;
}
int pop_stack(Pstack L)
{
if(L==NULL)
{
printf("栈链不存在或空\n");
return -1;
}
Node *Q=L->top;
L->top=Q->next;
free(Q);
Q=NULL;
L->len--;
printf("出栈成功\n");
return 0;
}
int main(int argc, const char *argv[])
{
Pstack L=creat_stack();
input_stack(L);
output_stack(L);
pop_stack(L);
output_stack(L);
return 0;
}
编译后: