Practical application of single linked list

Keywords: C data structure linked list

First ask a question: what is the algorithm?
In my opinion, algorithm is a way to solve problems. When your problem is more complex, the algorithm you need is more complex. More and more complex code is needed to implement it. In the following example, there is the embodiment of the algorithm.

The following is an example to explain the application level operation of single linked list. In fact, when working on a specific project, it is similar to this example. You should understand what to do to realize this function. This is the key point. When you understand it and then write the code, you have to choose a good sentence to implement the function (if? while? switch?...), and finally debug to see if there is any problem with the program written by yourself, as well as in product development.

As follows:
There is an emp.dat, and each employee record contains employee number (no), name, department number (depno) and salary information.
Design a program to complete the following functions
1. Read out the employee records from the emp.dat file and establish a single linked list L of the leading node.
2. Enter an employee record.
3. Display all employee records.
4. Sort all employee records incrementally by no.
5. Sort all employee records incrementally by department number (depno).
6. Sort all employee records incrementally by salary.
7. Delete the employee record with the specified employee number.
8. Delete all records in the employee file.
9. Store all employee records in the single linked table L in the employee file emp.dat.

When you see this example, you need to think about how to combine these contents with the linked list, how to define your node and how to define the data field in the node. In the implementation of the 8-point operation, in fact, the central idea is the addition, deletion, modification and query of the linked list. You need to be familiar with the basic operation algorithm of the single linked list.

Let's analyze it slowly:
To make reading data more intuitive, you need to define a data structure

typedef struct
{
	int no;
	char name[10];
	int depno;
	float salary;
}EmpType;

Then define a node type, in which there is only one structure data.

typedef struct node
{
	EmpType data;
	struct node * next;
}EmpList;

The variable definition is completed. The following describes the actual operation of the linked list

Delete all records in employee file

void DelAll(EmpList *&L)	
{
	FILE *fp;
	if ((fp=fopen("emp.dat","wb"))==NULL)	//Rewrite and empty the emp.dat file
	{	
		printf("  Tips:Cannot open employee file\n");
		return;
	}
	fclose(fp);
	DestroyEmp(L);						//Release employee single chain list L
	L=(EmpList *)malloc(sizeof(EmpList));	
	L->next=NULL;						//Create an empty employee single chain table L
	printf("  Tips:Employee data cleared\n");
}

Release employee single chain list L

void DestroyEmp(EmpList *&L)
{
	EmpList *pre=L,*p=pre->next;
	while (p!=NULL)
	{
		free(pre);
		pre=p;
		p=p->next;
	}
	free(pre);
}

Read emp.dat file to establish employee single key table L

void ReadFile(EmpList *&L)
{
	FILE *fp;
	EmpType emp;
	EmpList *p,*r;
	int n=0;
	L=(EmpList *)malloc(sizeof(EmpList));	//Create header node
	r=L;
	if ((fp=fopen("emp.dat","rb"))==NULL) //The emp.dat file does not exist
	{	
		 if ((fp=fopen("emp.dat","wb"))==NULL) 
			 printf("  Tips:Cannot create emp.dat file\n");
	}
	else		//If emp.dat file exists
	{
		while (fread(&emp,sizeof(EmpType),1,fp)==1)
		{	//The tail interpolation method is used to establish the single chain table L
			p=(EmpList *)malloc(sizeof(EmpList));
			p->data=emp;
			r->next=p;
			r=p;
			n++;
		}
	}
	r->next=NULL;
	printf("  Tips:Employee single key table L Established,yes%d Records\n",n);
	fclose(fp);
}

Add an employee record

void InputEmp(EmpList *&L)
{
	EmpType p;
	EmpList *s;
	printf("  >>Enter employee number(-1 return):");
	scanf("%d",&p.no);
	if (p.no==-1) return;
	printf("  >>Enter name department number salary:");
	scanf("%s%d%f",&p.name,&p.depno,&p.salary);
	s=(EmpList *)malloc(sizeof(EmpList));
	s->data=p;
	s->next=L->next;		//Insert node s by head inserting method
	L->next=s;
	printf("  Tips:Added successfully\n");
}

Delete an employee record

void DelEmp(EmpList *&L)	
{
	EmpList *pre=L,*p=L->next;
	int no;
	printf("  >>Enter employee number(-1 return):");
	scanf("%d",&no);
	if (no==-1) return;
	while (p!=NULL && p->data.no!=no)
	{
		pre=p;
		p=p->next;
	}
	if (p==NULL)
		printf("  Tips:The specified employee record does not exist\n");
	else
	{
		pre->next=p->next;
		free(p);
		printf("  Tips:Delete succeeded\n");
	}
}

Using direct insertion method, the single linked list L is sorted by salary increasing order

void Sortsalary(EmpList *&L)
{
	EmpList *p,*pre,*q;
	p=L->next->next;
	if (p!=NULL)
	{
		L->next->next=NULL;
		while (p!=NULL)
		{
			q=p->next;
			pre=L;
			while (pre->next!=NULL && pre->next->data.salary<p->data.salary)
				pre=pre->next;
			p->next=pre->next;
			pre->next=p;
			p=q;
		}
	}
	printf("  Tips:Press salary Incremental sort complete\n");
}

Output all employee records

void DispEmp(EmpList *L)	
{
	EmpList *p=L->next;
	if (p==NULL)
		printf("  Tips:There are no employee records\n");
	else
	{
		printf("    Employee name department number       wages\n");
		printf("   ----------------------------------\n");
		while (p!=NULL)
		{
			printf("  %3d%10s    %-8d%7.2f\n",p->data.no,p->data.name,p->data.depno,p->data.salary);
			p=p->next; 
		}
		printf("   ----------------------------------\n");
	}
}

The last is the main function. The writing method of the main function is classic and needs to be deeply understood

int main()
{
	EmpList *L;
	int sel;
	printf("from emp.dat Document establishment employee single key table L\n");
	ReadFile(L);
	do
	{	
		printf(">1:Add 2:Display 3:Sort by employee No. 4:Sort by department number 5:Sort by salary\n");
		printf(">6:Delete 9:Delete all 0:Exit, please select:");
		scanf("%d",&sel);
		switch(sel)
		{
		case 9:
			DelAll(L);
			break;
		case 1:
			InputEmp(L);
			break;
		case 2:
			DispEmp(L);
			break;
		case 3:
			Sortno(L);
			break;
		case 4:
			Sortdepno(L);
			break;
		case 5:
			Sortsalary(L);
			break;
		case 6:
			DelEmp(L);
			break;
		}
	} while (sel!=0);
	SaveFile(L);
	return 1;
}

Roughly, the program is written in this way to realize the corresponding functions.

The following is the complete project link:
Link: https://pan.baidu.com/s/1VHVtjDS12WwLajzbqg5KTA
Extraction code: lyqd

Welcome to communicate together.

Posted by daloss on Sat, 09 Oct 2021 21:51:28 -0700