数据结构813

作业:链栈,自己实现一遍,但是节点存储不是整数,存储学生信息(年龄,分数,姓名)三级引用。
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值