1、建立学生信息结构体,将data改为学生信息结构体类型。
2、循环入栈和入队。
link.c
#include"link.h"
Pstack get_memory()
{
Pstack L=malloc(sizeof(Stack));
if(L==NULL)
{
printf("链栈不存在\n");
return NULL;
}
L->len = 0;
L->top = NULL;
return L;
}
int insert_stu(Pstack L,stu s)
{
if(L==NULL)
{
printf("链栈不存在\n");
return -1;
}
Pnode p = malloc(sizeof(Node));
p->data = s;
p->next= L->top;
L->top = p;
L->len++;
printf("插入成功\n");
return 0;
}
int output_info(Pstack L)
{
if(L==NULL||L->len==0)
{
printf("链栈不存在\n");
return -1;
}
Pnode t = L->top;
for(int i = 0;i<L->len;i++)
{
printf("第%d学生信息如下:\n",i+1);
printf("名字:%s,年龄:%d,分数:%d\n",
t->data.name,
t->data.age,
t->data.score);
t=t->next;
}
}
int out_stack(Pstack L)
{
if(L==NULL||L->len==0)
{
printf("链栈不存在\n");
return -1;
}
Pnode t = L->top;
L->top = t->next;
L->len--;
printf("\n出栈学生信息:姓名=%s,年龄=%d,成绩=%d\n",
L->top->data.name,
L->top->data.age,
L->top->data.score);
free(t);
t=NULL;
return 0;
}
int destroy_stack(Pstack L)
{
if(L==NULL)
{
printf("链栈不存在\n");
return -1;
}
Pnode t=L->top ;
Pnode Q;
while(t!=NULL)
{
Q = t;
t=t->next;
free(t);
}
free(L);
L=NULL;
printf("销毁成功\n");
}
link.h
#ifndef _LINK_H_
#define _LINK_H_
#include<myhead.h>
typedef char str[20];
typedef struct
{
int age;
int score;
str name;
}stu;
typedef struct node
{
stu data;
struct node *next;
}Node,*Pnode;
typedef struct
{
int len;
Pnode top;
}Stack,*Pstack;
Pstack get_memory();
int insert_stu(Pstack L,stu s);
int output_info(Pstack L);
int out_stack(Pstack L);
int destroy_stack(Pstack L);
#endif
my.c
#include"link.h"
int main(int argc, const char *argv[])
{
Pstack L = get_memory();
stu stu1[4]={{20,100,"小飞"},
{21,90,"小明"},
{22,80,"小黑"},
{25,99,"小白"}
};
for(int i = 0;i<4;i++){
insert_stu(L,stu1[i]);
}
output_info(L);
out_stack(L);
output_info(L);
destroy_stack(L);
return 0;
}