Student management system implemented by singly linked list

This student management system is implemented through a singly linked list. This management system is to allow us to better operate on the linked list.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

struct Node
{
	char name[10];
	int num;
	int age;
	struct Node* next;
};

//Create a list of students
struct Node* createlist()
{
	struct Node* list = (struct Node*)malloc(sizeof(struct Node));//Allocate a memory space to the list
	list->next = NULL;
	return list;
}


//Create a student node
struct Node* createnode(char *newname, int newnum, int newage)
{
	struct Node* node = (struct Node*)malloc(sizeof(struct Node)); //Allocate a memory space to the node
	strcpy(node->name, newname); //Put the information into the node
	node->age = newage;
	node->num = newnum;
	node->next = NULL;
	return node;
}

//Insert a student's information at the end
void insertback(struct Node* list, char *newname, int newnum, int newage)
{
	struct Node* newnode = createnode(newname, newnum, newage);
	//find end node
	struct Node* temp = list;//The moved node is used to find the end node
	while (temp->next != NULL) //Move until the last node
	{
		temp = temp->next;
	}
	newnode->next = NULL;    
	temp->next = newnode;
	
}


//delete student information
//When deleting information, we need to define two moving nodes to find the node to be deleted and the node in front of it
void deleteinfo(struct Node* list, int num)
{
	struct Node* temp = list; //Used to find the previous node to be deleted
	struct Node* p = list->next;//Point to the node to be deleted
	while (p->num != num)
	{
		temp = temp->next;
		p = p->next;
	}

	temp->next = p->next;
	free(p);
}

//find student information
//similar to deleting students
void searchinfo(struct Node* list, char *name)
{
	struct Node* temp = list->next;
	while (strcmp(temp->name, name) != 0)
	{
		if (temp->next == NULL)
		{
			printf("There is no information for this student\n");
			return;
		}
		temp = temp->next;
		
	}
	printf("Name: %s\nNumber: %d\nAge: %d\n\n", temp->name, temp->num, temp->age);
}

void print(struct Node* list)//Print all information
{
	struct Node* temp = list->next;
	if (temp == NULL)
	{
		printf("No student information\n");
	}
	while (temp)
	{
		printf("Name: %s\nNumber: %d\nAge: %d\n\n", temp->name, temp->num, temp->age);
		temp = temp->next;
	}

}

//struct Node* student = createlist();
void menu()//Menu interface
{
	printf("****************************************\n");
	printf("* 1. Insert student information*\n");
	printf("* 2. Find student information*\n");
	printf("* 3. Print all information*\n");
	printf("* 4. Delete student information*\n");
	printf("* 5. Exit the management system*\n");
	printf("****************************************\n");
}

int choice()//Select the desired operation
{
	int choice;
	printf("Please select the action you want to perform\n");
	scanf("%d", &choice);
	while (choice < 1 || choice > 5)
	{
		printf("Your input is incorrect, please try again\n");
		scanf("%d", &choice);
	}
	return choice;
}


void work(struct Node* student)
{
	
	menu();
	int a = choice();

	switch (a)
	{
		case 1:
		{
			char name[10] = "0";
			int num = 0;
			int age = 0;
			printf("Enter the information name, number, age\n");
			scanf("%s",name);
			scanf("%d", &num);
			scanf("%d", &age);
			insertback(student, name, num, age);
			
		}break;
		case 2:
		{
			char searchname[10] = "0";
			printf("Please enter the name of the student you are looking for\n");
			scanf("%s", searchname);
			searchinfo(student, searchname);
		}break;
		case 3:
		{
			print(student);
		}break;
		case 4:
		{
			int deletenum = 0;
			printf("Please enter the number of the student you want to delete\n");
			scanf("%d", &deletenum);
			deleteinfo(student, deletenum);
		}break;
		case 5:
		{
			exit(0);
		}break;	
		default: printf("Input error\n"); break;
	}
}

intmain()
{
	struct Node* student = createlist();
	
	while (1)
	{
		work(student);
	}

	system("pause");
	return 0;
}
If there is anything you don't understand, or something is wrong, you can point it out in the comments, and you are welcome to browse.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948985&siteId=291194637