单向无头链表-学生信息管理系统

这是一个用于练习链表操作的程序,通过无头单链表实现了学生信息的创建、插入、删除、遍历和查找功能,但未包含排序模块。
摘要由CSDN通过智能技术生成

这个程序,为练习链表所写,实现了无头单链表的创建、插入、删除、遍历、查找,没添加排序功能。

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"

struct stu
{
	int num;
	char name[20];
	struct stu* next;
};
struct stu* linklist_create(void);
struct stu* linklist_insert(struct stu *head);
void linklist_visit(struct stu* head);
void linklist_delete(struct stu* head);
void linklist_find(struct stu* head);

int main(void)
{
	char choose;
	struct stu *head;
	head=NULL;
	while(1)
	{
		printf("\n**************** Command Menu           ****************\n");
		printf("**************** 1:creat a new list     ****************\n");
		printf("**************** 2:insert a new element ****************\n");
		printf("**************** 3:delete an element    ****************\n");
		printf("**************** 4:print the whole list ****************\n");
		printf("**************** 5:search an element    ****************\n");
		printf("**************** 6:exit                 ****************\n\n");
		choose=getch();
		switch(choose)
		{
			case '1':
				{
					if(head!=NULL)
					{
						printf("A linklist exists already!\n\n");
						break;
					}
					else
						head=linklist_create();
					break;
				}
			case '2':
				{
					if(head==NULL)
					{
						printf("No list has been created!Choose again!\n\n");
						break;
					}
					else
						head=linklist_insert(head);
					break;
				}
			case '3':
				{
					if(head==NULL)
					{
						printf("No list has been created!Choose again!\n\n");
						break;
					}
					else
						linklist_delete(head);
					break;					
				}
			case '4':
				{
					if(head==NULL)
					{
						printf("No list has been created!Choose again!\n\n");
						break;
					}
					else
						linklist_visit(head);
					break;					
				}
			case '5':
				{	
					if(head==NULL)
					{
						printf("No list has been created!Choose again!\n\n");
						break;
					}
					else
						linklist_find(head);
					break;
				}
			case '6':
				{	
					printf("Are you sure to exit?N to cancel or the program will exit.\n");
					if(getch()=='N')
					{
						printf("Exit request cancelled!\n");
						break;
					}
					else 
					{
						printf("Program end!\n");
						return 0;
					}
				}
			default:
				printf("Warning!Error choose!\n\n");
				break;
		}
	}
	free(head);
	head=NULL;
	return 0;
}

struct stu*  linklist_create(void)
{
	int n;
	struct stu* head=NULL;
	struct stu* pf=NULL;
	struct stu* pb=NULL;
	printf("How many elements do you want to creat?\n");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("Input the ID and name.\n");
		pb=(struct stu*)malloc(sizeof(struct stu));
		scanf("%d %s",&pb->num,pb->name);
		if(i==0)
		{
			head=pb;
			pf=pb;
		}
		else
		{
			pf->next=pb;
			pf=pb;
		}		
	}
	pb->next=NULL;
	return head;
}

struct stu* linklist_insert(struct stu *head)
{
	struct stu *newstu;
	struct stu *pf=head;
	struct stu *pb=head;
	newstu=(struct stu*)malloc(sizeof(struct stu));
	printf("Input the ID and name.\n");
	scanf("%d %s",&newstu->num,newstu->name);
	while(pb->num<=newstu->num)
	{
		pf=pb;
		pb=pb->next;
		if(pb==NULL)
			break;
	}
	pf->next=newstu;
	newstu->next=pb;
	return head;
}

void linklist_visit(struct stu* head)
{
	do
	{
		printf("ID and name:%d	%s\n",head->num,head->name);		
	}while(head=head->next);
}

void linklist_delete(struct stu* head)
{
	int num;
	struct stu* pf;
	struct stu* pb;
	pf=head;
	pb=head;
	printf("Which element do you want to delete?");
	printf("Input the ID:");
	scanf("%d",&num);
	while(pb->num!=num)
	{
		pf=pb;
		pb=pb->next;
		if(pb==NULL)
		{
			printf("The inputed ID doesn't exist!\n");
			return ;
		}
	}
	pf->next=pb->next;
	printf("delete OK!\n");
}
void linklist_find(struct stu* head)
{
	int n;
	printf("Input the ID to find ...\n");	
	scanf("%d",&n);
	while(head->num!=n)
	{
		head=head->next;
		if(head==NULL)
		{
			printf("the inputed ID dosen't exist!");
			return ;
		}
	}
	if(head->num==n)
		printf("ID and name:%d	%s\n",head->num,head->name);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值