数据结构8.12

作业:创建单链表,存储4个学生信息(年龄,分数,姓名)。

1、建立学生结构体数组,存放4个学生信息,循环调用插入函数,建立整表

2、任意位置插入一个新学生。变量e是学生结构体变量。

3、任意位置删除一个学生。

4、单链表逆置后将学生信息输出。

main.c

cs 复制代码
#include"stu.h"
int main(int argc, const char *argv[])
{
	Pnode stu = input_stu();
	for(int i = 0;i<MAX;i++)
	{
		tail_insert(stu,input_stu());
	}
	return 0;
}

stu.c

cs 复制代码
#include"stu.h"

Pnode get_head()
{
	Pnode p = malloc(sizeof(Node));
	if(p==NULL)
	{
		printf("申请失败\n");
		return NULL;
	}
	p->len = 0;
	p->next = NULL;
	return p;
}

int input_stu(Pnode L)
{
	int i;
	for(i = 0;i<MAX;i++)
	{
		printf("请输入第%d个学生的姓名:\n",i+1);
		scanf("%s",L->data[i].name);
		printf("请输入第%d个学生的年龄:\n",i+1);
		scanf("%d",L->data[i].age);
		printf("请输入第%d个学生的成绩:\n",i+1);
		scanf("%d",L->data[i].score);
	}
	L->len++;
	return 0;

}

stu.h

cs 复制代码
#ifndef _STU_H_
#define _STU_H_
#include<myhead.h>
#define MAX 4

typedef struct stu
{
	char name[20];
	int age;
	int score;
};

typedef struct node
{
	union
	{
		stu data;
		int len;
	}
	struct node *next;
}Node,*Pnode;


#endif
相关推荐
ValhallaCoder17 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
月挽清风18 小时前
代码随想录第十五天
数据结构·算法·leetcode
NEXT0619 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
小妖6661 天前
js 实现快速排序算法
数据结构·算法·排序算法
独好紫罗兰1 天前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n1 天前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
2401_841495641 天前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
独好紫罗兰1 天前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495641 天前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
数智工坊1 天前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构