Insertion, search, deletion, sorting and inversion of single linked list in data structure of C language version

Keywords: C data structure linked list

preface

The function definition, header file, structure type and main function in this paper
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct LINK{
	int data;
	struct LINK* next;
}*link;

link createlink(link );
void outlink(link );
void menu(link );
void deletelink(link );
link findlink(link );
void insertlink(link );
void deletelink1(link );
void deletelink2(link );
int puanduan(link ,int );
int link_length(link );
link findlink1(link );
void findlink2(link );
void turnlink(link );
int panduan2(link ,link );
bool is_empty(link );
void sortlink(link );
link comparelink(link );

int main(void)
{
	link phead;
	phead=createlink(phead);
	outlink(phead);
	system("pause");				                 // suspend
	system("cls");									 // Clear screen
	menu(phead);
	system("pause");
	return 0;
}


The function usage is described in detail below


1, Create single linked list

The function code of this module is:

// Create creates with a random number assignment
link createlink(link phead)
{
	link pend,body;
	phead=(link)malloc(sizeof(struct LINK));
	pend=phead;
	int length,i=0;
	printf("Please enter the length of the linked list:\n");
	scanf("%d",&length);
	srand(time(NULL));
	while(i<length)
	{
		body=(link)malloc(sizeof(LINK));
		body->data=rand()%101;
		pend->next=body;
		pend=body;
		i++;
	}
	pend->next=NULL;							//Tail insertion			
	printf("The linked list was created successfully!\n\n");
/*	phead->next=NULL;
	int length,i=0;
	printf("Please enter the length of the linked list: \ n "");
	scanf("%d",&length);
	while(i<length)
	{
		body=(link)malloc(sizeof(struct LINK));
		body->data=i+1;
		body->next=phead->next;
		phead->next=body;
		i++;
	}											//Head insert			*/
	return phead;
}

There are two ways to generate a single linked list:

1. Head insertion method

The code block is as follows:

	link pend,body;
	phead=(link)malloc(sizeof(struct LINK));
	phead->next=NULL;
	int length,i=0;
	printf("Please enter the length of the linked list:\n");
	scanf("%d",&length);
	while(i<length)
	{
		body=(link)malloc(sizeof(struct LINK));
		body->data=i+1;
		body->next=phead->next;
		phead->next=body;
		i++;
	}
	return phead;

Here is the initial value of the accumulated number (i+1)
The main usage of head inserting method is to form a head node, then insert each new node in the front of the head node, and then put the head node in the first position of the linked list, and so on


2. Tail interpolation
The code block is as follows:

	link pend,body;
	phead=(link)malloc(sizeof(struct LINK));
	pend=phead;
	int length,i=0;
	printf("Please enter the length of the linked list:\n");
	scanf("%d",&length);
	srand(time(NULL));
	while(i<length)
	{
		body=(link)malloc(sizeof(LINK));
		body->data=rand()%101;
		pend->next=body;
		pend=body;
		i++;
	}
	pend->next=NULL;							//Tail insertion			
	printf("The linked list was created successfully!\n\n");
	return phead;

Tail interpolation
The tail interpolation here mainly takes the random number as the initial value
The main writing method of tail interpolation is:
Place the new node after the last node in the current linked list
Note: the head node cannot move down, so pen in the previous code moves down instead of phead;
After adding a new node each time, pend must point to the last node;
After the last node accesses the linked list, pend points to a null pointer


The tail interpolation method is used in this paper

2, Output of linked list

The code is as follows:

void outlink(link phead)
{
	link body=phead->next;
	if(body==NULL)
	{
		printf("Currently empty table!\n");
		putchar(10);
		putchar(10);
		return;
	}
	int i=0;
	printf("\n----------------------------------------\n");
	printf("\n The elements of the linked list are:\n");
	while(body)
	{
		printf("%-6d",body->data);
		body=body->next;
		i++;
		if(i%5==0)						//Change every five
			putchar(10);
	}
	printf("\n----------------------------------------\n");
	putchar(10);											//Line feed is performed. This method will not be described below
	putchar(10);
}

emm, there's nothing to say

3, About insert, search, arrange, delete, reverse menu

The code is as follows:

// The current menu is used to realize insertion, deletion, search and reverse sorting;
// Find returns the element location (only if you choose to find by element);
void menu(link phead)
{
	link pnew;
	while(1)
	{
		printf("		----------------------------------------\n");
		printf("		-------------------menu-----------------\n");
		printf("		----------------1.Linked list insertion--------------\n");
		printf("		----------------2.Linked list deletion--------------\n");
		printf("		----------------3.Linked list lookup--------------\n");
		printf("		----------------4.Reverse linked list--------------\n");
		printf("		----------------5.Linked list sorting--------------\n");
		printf("		----------------0.Exit menu--------------\n");
		printf("		-------------------end------------------\n");
		printf("		----------------------------------------\n");
		printf("Please enter a selection:\n");
		int i;
		scanf("%d",&i);
		if(i==1)
		{
			insertlink(phead);
			system("pause");
			system("cls");					
		}
		else if(i==2)
		{
			deletelink(phead);
			system("pause");
			system("cls");
		}
		else if(i==3)
		{
			pnew=findlink(phead);
			system("pause");
			system("cls");
		}
		else if(i==0)
		{
			printf("Menu exited!\n\n");
			return;
		}
		else if(i==4)
		{
			turnlink(phead);
			system("pause");
			system("cls");
		}
		else if(i==5)
		{
			sortlink(phead);
			system("pause");
			system("cls");
		}
		else 
		{
			printf("Input error, need to re-enter!\n");
			system("pause");
			system("cls");
		}
	}
}

Multiple operations in a loop

4, About inserting linked list

The code is as follows:

// Insert is inserted at the element position. Use the loop to find the previous node that needs to be inserted at the element position, and then perform the insertion operation
void insertlink(link phead)
{
	int i=0,local;
	link body=phead;
	link pnew=(link)malloc(sizeof(struct LINK));
	outlink(phead);
	link_length(phead);
	printf("Please enter the location of the element to insert:\n");
	scanf("%d",&local);
	while(body&&i<local)
	{
		if(i+1==local)
			break;
		body=body->next;
		i++;
	}											//Find the previous node of the insertion location
	if(i>=local||!body)
	{
		printf("Incorrect input position!\n");
		return;
	}											//If not found, output an error message and end the current insertion
	printf("Please enter the size of the element to insert:\n");
	scanf("%d",&pnew->data);
	pnew->next=body->next;
	body->next=pnew;
	outlink(phead);								//Output new linked list after insertion
}

The main idea here has been written in the code block and will not be repeated

5, About deleting linked list

1. Deletion method

Here I have divided deletion by element position and deletion by element size
The following function is used to select the deletion type

// Here are only the selection functions of two deletion methods
void deletelink(link phead)
{
	if(is_empty(phead))
	{
		printf("It is currently an empty linked list. Please add an element, otherwise it cannot be deleted!\n");
		return;
	}											//Obviously, the empty table does not need to be deleted, and the deletion is ended directly

	printf("		---------------------------------------\n");
	printf("		---------------Two deletion methods------------\n");
	printf("		---------------1.Delete as element------------\n");
	printf("		---------------2.Delete by location------------\n");
	printf("		---------------------------------------\n");
	printf("\n Please enter your choice:\n");
	int number;
	scanf("%d",&number);
	if(number==1)
	{
		deletelink1(phead);						// Entry mode 1
	}
	else if(number==2)
	{
		deletelink2(phead);						// Entry mode 2
	}
	else
	{
		printf("Incorrect input, restart deletion!\n");
		deletelink(phead);
	}											//In order to satisfy the user, after the user enters the wrong option,
												//Let the user delete the selection again
}

2. Delete by element size

This method is applicable to the deletion of one or more identical elements
The code is as follows:

//						In the deletion of mode 1, the main ideas are as follows:
//		1. Judge whether the deleted element exists. If it exists, proceed to the next step. If it does not exist, give a prompt and exit the deletion
//		2. If there is more than one element to be deleted, the cycle will not be terminated after deletion, but the tail of the linked list will be found all the time
//	  (2) in the deletion, the previous node of the element to be deleted should be found, so it is: body - > next - > data = = number;
//	  (2) when the element to be deleted is not found, and there is no node after the element, exit the deletion;
//		3. Be careful here that the last element is also an element that should be deleted; however, the method in the previous (Supplement 2) is no longer applicable and the new method is added
//		4. If there is only one useful node in the linked list at this time, release the node directly
void deletelink1(link phead)
{
	outlink(phead);
	printf("Please enter the element to be deleted:\n");
	int number;
	scanf("%d",&number);
	if(puanduan(phead,number)==0)			// 1
	{
		printf("No such number!\n");
		putchar(10);
		putchar(10);
		return;
	}
	else if(puanduan(phead,number)==1)				// 2
	{
		if(phead->next->next!=NULL)
		{
			while(1)
			{
				link body=phead;
				while(body->next->data!=number && body->next->next)               //2 supplement
					body=body->next;
				if(body->next->data!=number && NULL==body->next->next)
					break;
				if(phead->next->data==number && phead->next->next==NULL)		  //3
				{
					link p=body->next;
					printf("The deleted element is%d\n",phead->next->data);
					phead->next=NULL;
					free(p);
					break;
				}
				link p=body->next;
				printf("The deleted element is%d\n",p->data);
				body->next=body->next->next;
				free(p);
			}
			outlink(phead);
		}
		else																	//4
		{
			link p=phead->next;
			printf("The deleted element is%d\n",phead->next->data);						
			phead->next=NULL;
			free(p);
			outlink(phead);
		}
	}
}

The description has been placed in the code block

3. Delete by element position

The code is as follows:

// When deleting at the element position, you need to find the previous node of the deleted element and free the memory of the element
void deletelink2(link phead)
{
	outlink(phead);
	int length=link_length(phead);
	printf("Please enter the location of the element to be deleted:\n");
	int local,i=1;
	scanf("%d",&local);
	link body=phead;
	if(i>local || local>length)						//Judge whether the input position is legal
	{
		printf("Wrong element position!\n\n\n");
		return;
	}
	while(i<local && body->next)
	{
		body=body->next;
		i++;
	}
	link p=body->next;
	printf("The deleted element is%d\n",p->data);
	body->next=body->next->next;
	free(p);
	outlink(phead);
}

emm, which will not be repeated, is to find the location of the element to be deleted and store the pointer field of the previous node in the location of the next node to be deleted

6, On the search of linked list

1. Search method

In order to avoid disputes, I also divide search into search by element size and search by element position
The code is as follows:

//At this time, only two search methods are selected
link findlink(link phead)
{
	printf("		---------------------------------------\n");
	printf("		---------------Two search methods------------\n");
	printf("		---------------1.Find by element------------\n");
	printf("		---------------2.Find by location------------\n");
	printf("		---------------------------------------\n");
	printf("\n Please enter your choice:\n");
	int number;
	scanf("%d",&number);
	if(number==1)							//When method 1 finds multiple locations of the same element, the location is stored in
											//Linked list, and return to the menu;
	{
		link pnew=findlink1(phead);
		if(pnew!=NULL)
			printf("The element position has been saved in the linked list!\n\n\n");
		return pnew;
	}
	else if(number==2)
	{
		findlink2(phead);
	}
	else
	{
		printf("Error in input. Start searching again!\n");
		findlink(phead);
	}										//Remind the user that there is an error in the input and selection, and search again
}

2. Find by element size

This is also applicable to the search of multiple elements with the same element. When the element position is found, it is stored in a linked list, and at the end, the linked list is returned to the menu for insertion and deletion
The code is as follows:

//						     		Main ideas for finding method 1:
//		1. When searching, first judge whether there is this number in the previous judgment function; (you can also judge whether the linked list is empty)
//									  With this number
//		2. At this time, there may be more than one element to be searched, so search until the end of the linked list, and store the found positions in the new linked list pnew
//		3. Enter the element location information found in pnew
//		4. Return pnew for subsequent use.
link findlink1(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return NULL;
	}
	outlink(phead);
	printf("Please enter the element you want to find:\n");
	int number,j=1;
	scanf("%d",&number);
	link body=phead->next;
	link pnew=(link)malloc(sizeof(struct LINK));
	link pend=pnew;
	if(puanduan(phead,number)==0)                      //1
	{
		printf("No such number!\n");
		putchar(10);
		putchar(10);
		return NULL;
	}
	else if(puanduan(phead,number)==1)
	{
		while(body)
		{
			if(body->data==number)
			{
				link pbody=(link)malloc(sizeof(struct LINK));
				pbody->data=j;
				pend->next=pbody;
				pend=pbody;
			}
			j++;
			body=body->next;
		}											  //2
	}
	pend->next=NULL;
	printf("\n The element location is:\n");
	body=pnew->next;
	while(body)
	{
		printf("%-5d",body->data);
		body=body->next;
	}												 //3
	putchar(10);
	putchar(10);
	return pnew;								     //4
}

The main idea is to look at the code comments. pnew in it stores the element position (tail interpolation)
The puanduan function determines whether the element exists in the searched linked list

3. Find by element position

The code is as follows:

// This method is to find the element by the element position; traverse to the element position and directly output the element.
void findlink2(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link body=phead->next;
	int length=link_length(phead);
	printf("Please enter the location of the element you want to find:\n");
	int local;
	scanf("%d",&local);
	int i=0;
	if(local<1||local>length)
	{
		printf("\n Wrong input position!\n\n\n");
		return;
	}
	while(i<local-1)
	{
		body=body->next;
		i++;
	}
	printf("\n The location element is:%d\n\n",body->data);
}

It's similar to the idea of finding elements when deleting inserts

7, On the inverse of linked list

The code is as follows:

//									Main idea of inverse linked list
//		1. If the linked list is empty, a prompt will be given and it will not be reversed; if there is only one node, it will not be reversed
//							      When the linked list has more than one node
//		2. Divide the previous linked list into two parts: the head node (pnew) and the back of the head node (body)
//		3. Traverse to the last node after talking about the body each time, and mark the last two nodes; after connecting the last node to pnew;
//		  The penultimate node of the original body points to null, and pnew points to the last node of pnew node
//		4. When there is the last node left in the body, it is no longer traversed and directly connected after pnew; At this point, the last node must point to NULL
void turnlink(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link body=phead->next,body1=body;
	link body2,pnew=phead;
	if(phead->next->next==NULL)
	{
		outlink(phead);
		return;
	}
	while(1)
	{
		while(body->next!=NULL)
		{
			body2=body;
			body=body->next;
		}
		body2->next=NULL;
		pnew->next=body;
		pnew=pnew->next;
		body=body1;
		if(body1->next==NULL)
			break;
	}
	pnew->next=body1;
	outlink(phead);
}

emm, I think the comments in my code block are very detailed (manual funny)

8, On the sorting of linked lists

The sort here is from large to small
My method is:
1. Divide the original linked list into the nodes after the head node (pnew) and the head node (pbody - > next);
2. Find the largest number of nodes (q) after the current head node (pbody) every time, and delete the first position after the head node to find the number (cannot be released)
3. Insert the number of nodes into the position after the head node (Q - > next = pnew - > next; pnew - > next = q;), and then move the head node to the position of the newly connected node (pnew = pnew - > next)
4. Wait until the last node is found and exit the loop (the condition for jumping out of the loop here is that the last position pointed by the header pointer is empty)


The specific codes are as follows:

void sortlink(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link pnew=phead;
	link body=phead;
	while(1)
	{
		link q=comparelink(body->next);
		while(body->next)
		{
			if(body->next==q)
			{
				body->next=body->next->next;
				break;
			}
			body=body->next;
		}										//Release the maximum value in the current linked list
		q->next=pnew->next;
		pnew->next=q;
		pnew=pnew->next;						//Insert the maximum value into the current linked list
		body=pnew;
		if(pnew->next==NULL)
			break;
	}
	outlink(phead);
}

// Find the maximum value in the current linked list in turn
link comparelink(link phead)
{
	link body=phead;
	link s=phead;
	while(body)
	{
		if(s->data<body->data)
			s=body;
		body=body->next;
	}
	return s;
}

I'll try to write it later

9, Other auxiliary functions and all codes

This determines whether the linked list is empty. If it is empty, it returns true, not false

//  Determine whether it is an empty table (used when deleting elements)
bool is_empty(link head)
{
	if(head->next==NULL)
		return true;
	else
		return false;
}	

The following is the total code, which can be copied, pasted and run
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct LINK{
	int data;
	struct LINK* next;
}*link;

link createlink(link );
void outlink(link );
void menu(link );
void deletelink(link );
link findlink(link );
void insertlink(link );
void deletelink1(link );
void deletelink2(link );
int puanduan(link ,int );
int link_length(link );
link findlink1(link );
void findlink2(link );
void turnlink(link );
int panduan2(link ,link );
bool is_empty(link );
void sortlink(link );
link comparelink(link );

int main(void)
{
	link phead;
	phead=createlink(phead);
	outlink(phead);
	system("pause");				                 // suspend
	system("cls");									 // Clear screen
	menu(phead);
	system("pause");
	return 0;
}

// Create creates with a random number assignment
link createlink(link phead)
{
	link pend,body;
	phead=(link)malloc(sizeof(struct LINK));
	pend=phead;
	int length,i=0;
	printf("Please enter the length of the linked list:\n");
	scanf("%d",&length);
	srand(time(NULL));
	while(i<length)
	{
		body=(link)malloc(sizeof(LINK));
		body->data=rand()%101;
		pend->next=body;
		pend=body;
		i++;
	}
	pend->next=NULL;							//Tail insertion			
	printf("The linked list was created successfully!\n\n");
/*	phead->next=NULL;
	int length,i=0;
	printf("Please enter the length of the linked list: \ n "");
	scanf("%d",&length);
	while(i<length)
	{
		body=(link)malloc(sizeof(struct LINK));
		body->data=i+1;
		body->next=phead->next;
		phead->next=body;
		i++;
	}											//Head insert			*/
	return phead;
}

void outlink(link phead)
{
	link body=phead->next;
	if(body==NULL)
	{
		printf("Currently empty table!\n");
		putchar(10);
		putchar(10);
		return;
	}
	int i=0;
	printf("\n----------------------------------------\n");
	printf("\n The elements of the linked list are:\n");
	while(body)
	{
		printf("%-6d",body->data);
		body=body->next;
		i++;
		if(i%5==0)
			putchar(10);
	}
	printf("\n----------------------------------------\n");
	putchar(10);											//Line feed is performed. This method will not be described below
	putchar(10);
}


// The current menu is used to realize insertion, deletion, search and reverse sorting;
// Find returns the element location (only if you choose to find by element);
void menu(link phead)
{
	link pnew;
	while(1)
	{
		printf("		----------------------------------------\n");
		printf("		-------------------menu-----------------\n");
		printf("		----------------1.Linked list insertion--------------\n");
		printf("		----------------2.Linked list deletion--------------\n");
		printf("		----------------3.Linked list lookup--------------\n");
		printf("		----------------4.Reverse linked list--------------\n");
		printf("		----------------5.Linked list sorting--------------\n");
		printf("		----------------0.Exit menu--------------\n");
		printf("		-------------------end------------------\n");
		printf("		----------------------------------------\n");
		printf("Please enter a selection:\n");
		int i;
		scanf("%d",&i);
		if(i==1)
		{
			insertlink(phead);
			system("pause");
			system("cls");					
		}
		else if(i==2)
		{
			deletelink(phead);
			system("pause");
			system("cls");
		}
		else if(i==3)
		{
			pnew=findlink(phead);
			system("pause");
			system("cls");
		}
		else if(i==0)
		{
			printf("Menu exited!\n\n");
			return;
		}
		else if(i==4)
		{
			turnlink(phead);
			system("pause");
			system("cls");
		}
		else if(i==5)
		{
			sortlink(phead);
			system("pause");
			system("cls");
		}
		else 
		{
			printf("Input error, need to re-enter!\n");
			system("pause");
			system("cls");
		}
	}
}


// Insert is inserted at the element position. Use the loop to find the previous node that needs to be inserted at the element position, and then perform the insertion operation
void insertlink(link phead)
{
	int i=0,local;
	link body=phead;
	link pnew=(link)malloc(sizeof(struct LINK));
	outlink(phead);
	link_length(phead);
	printf("Please enter the location of the element to insert:\n");
	scanf("%d",&local);
	while(body&&i<local)
	{
		if(i+1==local)
			break;
		body=body->next;
		i++;
	}											//Find the previous node of the insertion location
	if(i>=local||!body)
	{
		printf("Incorrect input position!\n");
		return;
	}											//If not found, output an error message and end the current insertion
	printf("Please enter the size of the element to insert:\n");
	scanf("%d",&pnew->data);
	pnew->next=body->next;
	body->next=pnew;
	outlink(phead);								//Output new linked list after insertion
}


// Here are only the selection functions of two deletion methods
void deletelink(link phead)
{
	if(is_empty(phead))
	{
		printf("It is currently an empty linked list. Please add an element, otherwise it cannot be deleted!\n");
		return;
	}											//Obviously, the empty table does not need to be deleted, and the deletion is ended directly

	printf("		---------------------------------------\n");
	printf("		---------------Two deletion methods------------\n");
	printf("		---------------1.Delete as element------------\n");
	printf("		---------------2.Delete by location------------\n");
	printf("		---------------------------------------\n");
	printf("\n Please enter your choice:\n");
	int number;
	scanf("%d",&number);
	if(number==1)
	{
		deletelink1(phead);						// Entry mode 1
	}
	else if(number==2)
	{
		deletelink2(phead);						// Entry mode 2
	}
	else
	{
		printf("Incorrect input, restart deletion!\n");
		deletelink(phead);
	}											//In order to satisfy the user, after the user enters the wrong option,
												//Let the user delete the selection again
}

//  Determine whether it is an empty table (used when deleting elements)
bool is_empty(link head)
{
	if(head->next==NULL)
		return true;
	else
		return false;
}							


//						In the deletion of mode 1, the main ideas are as follows:
//		1. Judge whether the deleted element exists. If it exists, proceed to the next step. If it does not exist, give a prompt and exit the deletion
//		2. If there is more than one element to be deleted, the cycle will not be terminated after deletion, but the tail of the linked list will be found all the time
//	  (2) in the deletion, the previous node of the element to be deleted should be found, so it is: body - > next - > data = = number;
//	  (2) when the element to be deleted is not found, and there is no node after the element, exit the deletion;
//		3. Be careful here that the last element is also an element that should be deleted; however, the method in the previous (Supplement 2) is no longer applicable and the new method is added
//		4. If there is only one useful node in the linked list at this time, release the node directly
void deletelink1(link phead)
{
	outlink(phead);
	printf("Please enter the element to be deleted:\n");
	int number;
	scanf("%d",&number);
	if(puanduan(phead,number)==0)			// 1
	{
		printf("No such number!\n");
		putchar(10);
		putchar(10);
		return;
	}
	else if(puanduan(phead,number)==1)				// 2
	{
		if(phead->next->next!=NULL)
		{
			while(1)
			{
				link body=phead;
				while(body->next->data!=number && body->next->next)               //2 supplement
					body=body->next;
				if(body->next->data!=number && NULL==body->next->next)
					break;
				if(phead->next->data==number && phead->next->next==NULL)		  //3
				{
					link p=body->next;
					printf("The deleted element is%d\n",phead->next->data);
					phead->next=NULL;
					free(p);
					break;
				}
				link p=body->next;
				printf("The deleted element is%d\n",p->data);
				body->next=body->next->next;
				free(p);
			}
			outlink(phead);
		}
		else																	//4
		{
			link p=phead->next;
			printf("The deleted element is%d\n",phead->next->data);						
			phead->next=NULL;
			free(p);
			outlink(phead);
		}
	}
}

// Does the element to be deleted exist
int puanduan(link phead,int number)
{
	while(1)
	{
		link body=phead;
		while(body->next->data!=number && body->next->next)
			body=body->next;
		if(body->next->data!=number && NULL==body->next->next)
			return 0;
		else 
			return 1;
	}
}

// When deleting at the element position, you need to find the previous node of the deleted element and free the memory of the element
void deletelink2(link phead)
{
	outlink(phead);
	int length=link_length(phead);
	printf("Please enter the location of the element to be deleted:\n");
	int local,i=1;
	scanf("%d",&local);
	link body=phead;
	if(i>local || local>length)						//Judge whether the input position is legal
	{
		printf("Wrong element position!\n\n\n");
		return;
	}
	while(i<local && body->next)
	{
		body=body->next;
		i++;
	}
	link p=body->next;
	printf("The deleted element is%d\n",p->data);
	body->next=body->next->next;
	free(p);
	outlink(phead);
}

// Judge the length of the current linked list. This function is used when searching
int link_length(link phead)
{
	int i=0;
	link body=phead->next;
	while(body)
	{
		body=body->next;
		i++;
	}
	printf("The current linked list length is:%d\n",i);
	putchar(10);
	return i;
}

//At this time, only two search methods are selected
link findlink(link phead)
{
	printf("		---------------------------------------\n");
	printf("		---------------Two search methods------------\n");
	printf("		---------------1.Find by element------------\n");
	printf("		---------------2.Find by location------------\n");
	printf("		---------------------------------------\n");
	printf("\n Please enter your choice:\n");
	int number;
	scanf("%d",&number);
	if(number==1)							//When method 1 finds multiple locations of the same element, the location is stored in
											//Linked list, and return to the menu;
	{
		link pnew=findlink1(phead);
		if(pnew!=NULL)
			printf("The element position has been saved in the linked list!\n\n\n");
		return pnew;
	}
	else if(number==2)
	{
		findlink2(phead);
	}
	else
	{
		printf("Error in input. Start searching again!\n");
		findlink(phead);
	}										//Remind the user that there is an error in the input and selection, and search again
}


//						     		Main ideas for finding method 1:
//		1. When searching, first judge whether there is this number in the previous judgment function; (you can also judge whether the linked list is empty)
//									  With this number
//		2. At this time, there may be more than one element to be searched, so search until the end of the linked list, and store the found positions in the new linked list pnew
//		3. Enter the element location information found in pnew
//		4. Return pnew for subsequent use.
link findlink1(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return NULL;
	}
	outlink(phead);
	printf("Please enter the element you want to find:\n");
	int number,j=1;
	scanf("%d",&number);
	link body=phead->next;
	link pnew=(link)malloc(sizeof(struct LINK));
	link pend=pnew;
	if(puanduan(phead,number)==0)                      //1
	{
		printf("No such number!\n");
		putchar(10);
		putchar(10);
		return NULL;
	}
	else if(puanduan(phead,number)==1)
	{
		while(body)
		{
			if(body->data==number)
			{
				link pbody=(link)malloc(sizeof(struct LINK));
				pbody->data=j;
				pend->next=pbody;
				pend=pbody;
			}
			j++;
			body=body->next;
		}											  //2
	}
	pend->next=NULL;
	printf("\n The element location is:\n");
	body=pnew->next;
	while(body)
	{
		printf("%-5d",body->data);
		body=body->next;
	}												 //3
	putchar(10);
	putchar(10);
	return pnew;								     //4
}


// This method is to find the element by the element position; traverse to the element position and directly output the element.
void findlink2(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link body=phead->next;
	int length=link_length(phead);
	printf("Please enter the location of the element you want to find:\n");
	int local;
	scanf("%d",&local);
	int i=0;
	if(local<1||local>length)
	{
		printf("\n Wrong input position!\n\n\n");
		return;
	}
	while(i<local-1)
	{
		body=body->next;
		i++;
	}
	printf("\n The location element is:%d\n\n",body->data);
}


//									Main idea of inverse linked list
//		1. If the linked list is empty, a prompt will be given and it will not be reversed; if there is only one node, it will not be reversed
//							      When the linked list has more than one node
//		2. Divide the previous linked list into two parts: the head node (pnew) and the back of the head node (body)
//		3. Traverse to the last node after talking about the body each time, and mark the last two nodes; after connecting the last node to pnew;
//		  The penultimate node of the original body points to null, and pnew points to the last node of pnew node
//		4. When the last node is left in the body, it will not be traversed and directly connected after pnew; at this time, the last node must point to NULL
void turnlink(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link body=phead->next,body1=body;
	link body2,pnew=phead;
	if(phead->next->next==NULL)
	{
		outlink(phead);
		return;
	}
	while(1)
	{
		while(body->next!=NULL)
		{
			body2=body;
			body=body->next;
		}
		body2->next=NULL;
		pnew->next=body;
		pnew=pnew->next;
		body=body1;
		if(body1->next==NULL)
			break;
	}
	pnew->next=body1;
	outlink(phead);
}

void sortlink(link phead)
{
	if(is_empty(phead))
	{
		printf("Empty linked list!!\n\n");
		return;
	}
	link pnew=phead;
	link body=phead;
	while(1)
	{
		link q=comparelink(body->next);
		while(body->next)
		{
			if(body->next==q)
			{
				body->next=body->next->next;
				break;
			}
			body=body->next;
		}										//Release the maximum value in the current linked list
		q->next=pnew->next;
		pnew->next=q;
		pnew=pnew->next;						//Insert the maximum value into the current linked list
		body=pnew;
		if(pnew->next==NULL)
			break;
	}
	outlink(phead);
}

// Find the maximum value in the current linked list in turn
link comparelink(link phead)
{
	link body=phead;
	link s=phead;
	while(body)
	{
		if(s->data<body->data)
			s=body;
		body=body->next;
	}
	return s;
}

epilogue


In fact, this is the first time I uploaded the code I wrote in csdn. In fact, I originally wrote this to share some ideas with you, listen to others' opinions and make up for my shortcomings (I just started to contact data structures and am a college student) If there is any bad or incorrect usage in the code, please leave a message in the comment area to discuss and make progress together. Thank you

Posted by dcampbell18 on Sun, 03 Oct 2021 18:26:09 -0700