作业:链栈,自己实现一遍,但是节点存储不是整数,存储学生信息(年龄,分数,姓名)三级引用。
1、建立学生信息结构体,将data改为学生信息结构体类型。
2、循环入栈和入队。
#include <stdio.h>
struct students
{
int age;
float score;
char name[20];
};students
struct node
{
struct students data;
struct node *next;
};
struct node *top = NULL;
void push(struct students s)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = s;
new_node->next = top;
top = new_node;
}
void pop()
{
if (top == NULL)
{
printf(“栈为空!\n”);
return;
}
struct node *temp = top;
top = top->next;
free(temp);
}
void display()
{
if (top == NULL)
{
printf(“栈为空!\n”);
return;
}
struct node *temp = top;
while (temp!= NULL)
{
printf(“年龄:%d,分数:%.2f,姓名:%s\n”, temp->data.age, temp->data.score, temp->data.name);
temp = temp->next;
}
}
int main()
{
struct students s1 = {20, 80.5, “张三”};
struct students s3 = {22, 70.5, “王五”};
push(s1);
push(s2);
push(s3);
display();
pop();
display();
return 0;
}