ubuntu@ubuntu:week7day2$ gcc zuoye.c
ubuntu@ubuntu:week7day2$ ./a.out
入栈成功
入栈成功
入栈成功
20 王五 99
19 李四 88
18 张三 90
ubuntu@ubuntu:week7day2$ cat zuoye.c
#include<myhead.h>
typedef struct
{
int age;
char name[10];
int score;
}stu;
typedef struct node
{
stu data;
struct node *next;
}Node;
typedef struct
{
int len;
Node *top;
}Stack,*Pstack;
Pstack create_stack()
{
Pstack p = malloc(sizeof(Stack));//申请的栈顶指针节点
if(p==NULL)
{
printf("创建链栈失败\n");
return NULL;
}
p->len = 0;
p->top = NULL;
return p;//返回栈顶节点
}
int push_stack(Pstack L,stu e)
{
if(L==NULL)
{
printf("链栈不存在\n");
return -1;
}
Node *p = malloc(sizeof(Node));//生成正常的新节点
p->data =e;
p->next = L->top;
L->top = p;//栈顶指针指向新节点
L->len++;//计数器+1
printf("入栈成功\n");
return 0;
}
int output_stack(Pstack L)
{
if(L==NULL||L->len==0)
{
printf("栈不存在或者为空\n");
return -1;
}
int i;
Node* t = L->top;//指向第一个正常节点(栈顶)
for(i = 0;i<L->len;i++)//从栈顶输出到栈底
{
printf("%d %s %d\n",t->data.age,t->data.name,t->data.score);
t = t->next;
}
printf("\n");
return 0;
}
int main(int argc, const char *argv[])
{
Pstack L = create_stack();
stu st[3] ={{18,"张三",90},{19,"李四",88},{20,"王五",99}};
push_stack(L,st[0]);//入栈
push_stack(L,st[1]);//入栈
push_stack(L,st[2]);//入栈
output_stack(L);
return 0;
}