数据结构——使用链表管理简单的学生信息

使用链表简单管理学生信息

链表:是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

链表的优缺点:使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的存取往往要在不同的排列顺序中转换。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。

问题 创建链表,插入学生信息,为学生信息排序,删除指定的学生信息,查询学生信息,输出学生信息。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef int Status;
typedef int ElemType;
typedef struct list
{
	int num;
	char name[15];
	struct list *next;
}LIST;
int Menu(void);
LIST* InsertList(LIST *L);
LIST* SwapList(LIST *L);//根据学号大小将学生信息从小到大排序
LIST* DeleteList(LIST *L,int x);//根据需要删除的学号,删除学生的信息
LIST* SearchList(LIST *L,int y);//根据需要查找的学号,找出学生的信息
LIST* PrintList(LIST *L);//输出学生的信息

int main()
{
	LIST SqL;
	LIST *res,*pos,*ret,*ros;//定义函数返回值
	int itemSelected;
	int number,number1;//定义学号变量
	while(1)
	{
		itemSelected=Menu();
		switch(itemSelected)
		{
		case 1:res=InsertList(&SqL);break;
		case 2:printf("The student's information is:\n");
			PrintList(res);break;
		case 3:ret=SwapList(res);
			printf("The abscending order is:\n");
			PrintList(ret);break;
		case 4:printf("按学号查询学生信息:\n");
			printf("please input the student's number:");
			scanf("%d",&number);
			fflush(stdin);
			pos=SearchList(res,number);
			if(pos!=NULL)
			{
				printf("该学生的信息为:%d %s\n",pos->num ,pos->name);
			}
			else
			{
				printf("该学生不存在!\n");
			}
			break;
		case 5:printf("请输入需要删除学生的学号:");
			scanf("%d",&number1);
			fflush(stdin);
			ros=DeleteList(res,number1);
			PrintList(ros);break;
		case 0:printf("End of the programe!\n");
			exit(0);
		default :printf("Input error!\n");
		}
	}
	
	return 0;
}


/*函数功能:显示菜单并获得用户键盘输入的选项*/
int Menu(void)
{
	int itemSelected;
	printf("\nManagement for student's information\n");
	printf("1.Insert student's information\n");
	printf("2.print student's information\n");
	printf("3.rand student's information\n");
	printf("4.use number to search student's information\n");
	printf("5.delete student's information\n");
	printf("0.end of the programe\n");
	printf("please input your choice:");
	scanf("%d",&itemSelected);
	return itemSelected;
}


/*函数功能:插入学生的信息*/
LIST* InsertList(LIST *L)
{
	int n;
	LIST *tail,*p;
	L=(LIST *)malloc(sizeof(LIST));
	L->next =NULL;
	tail=L;//创建头结点
	printf("please input student's number(when number=-1 end of the programe):");
	while(scanf("%d",&n) && n!=-1)
	{
		p=(LIST *)malloc(sizeof(LIST));
		p->num =n;
		printf("please input student's name:");
		scanf("%s",p->name );
		tail->next =p;
		tail=p;
		tail->next =NULL;
		printf("please input student's number:");
	}//插入学生的信息
	return L;
}

/*函数功能:根据学号大小,将学生信息递增排序*/
LIST* SwapList(LIST *L)
{
	int temp;
	char tempname[15];
	LIST *p=L,*q=NULL;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		for(q=p->next ;q!=NULL;q=q->next )
		{
			if(p->num >q->num )
			{
				temp=p->num ;
				p->num =q->num ;
				q->num =temp;
				strcpy(tempname,p->name );
				strcpy(p->name ,q->name );
				strcpy(q->name ,tempname);//用选择法交换学生的信息
			}
		}
	}
	return L;
}


/*函数功能:根据学生的学号,删除该学生的信息*/
LIST* DeleteList(LIST *L ,int num1)
{
	LIST *p1,*p2;
	if(L==NULL)
	{
		printf("list is empty!\n");//若结点为空,返回空指针
	}
	p1=L;
	while(num1!=p1->num && p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;//若没有找到,继续之后,指针依次往后移
	}
	if(num1==p1->num)
	{
		if(p1==L)
		{
			L=p1->next;
		}
		else
		{
			p2->next=p1->next;
		}//找到了,删除该结点
	}
	return L;
}


/*函数功能:根据需要查询的学号,找到该学生的信息*/
LIST* SearchList(LIST *L,int y)
{
	LIST *p=L;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		if(p->num == y)
		{
            return p;//找到该结点,返回结点所在的位置
		}
	}
	return NULL;//若没有找到,返回空指针
}

/*函数功能:输出学生的信息*/
LIST* PrintList(LIST *L)
{
	LIST *p=L;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		printf("%d %s\n",p->num ,p->name );//打印学生的信息
	}
	return L;
}

若代码有错误的地方或是其他代码,请各位指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值