C language realizes the initialization of singly linked list, the basic operation of adding, deleting, checking and modifying)

C language realizes the basic operation of singly linked list initialization, adding, deleting, checking and modifying

introduction

definition

★★★Single-linked list is a data structure for chain access of linear list, which uses a group of storage units with arbitrary addresses to store data in the linear list.

principle

<1> The data in the linked list is represented by nodes, and the composition of nodes: data field + pointer field.
<2> The data field is the storage unit for storing data, and the pointer field is the address of the subsequent node.

advantage

<1> When a singly linked list inserts or deletes an element, it only needs to change its predecessor element and the pointer of the inserted or deleted element.
<2> When the amount of stored data is unknown, the singly linked list opens up a space for each data stored, so the space can be well grasped, and the waste will not be so obvious.

shortcoming

<1> Singly linked list does not support random access, so the search efficiency is slow.
<2> When the number of data to be accessed is known, because each data in the singly linked list also includes a pointer field, the waste of space is serious. Moreover, for the singly linked list, each data address is randomly opened, so the storage space will be fragmented, and there will be many small fragmented spaces.

Compare

Click the link below to refer to the previous article to compare the single linked list and the sequential list.
Links: ★★★ link .

in time

<1> The structure of the sequence table is like an array, and its elements can be accessed by subscripts, so the data of the sequence table supports random access; in contrast, the data of the singly linked list is stored in a chain, and its Elements do not support random access. If you want to know an element, you can only traverse the entire linked list from the head node until you find the element. Therefore, the time complexity of accessing random elements in a sequential list is O(1), and the average time complexity of accessing random elements in a singly linked list is O(n).

<2> The elements of the sequence table are stored continuously. Therefore, to insert or delete an element at a specific position, all the elements after it need to be moved back or forward by one element, which takes a lot of time; while the single linked list is inserted or deleted. When deleting an element, you only need to change its predecessor element and the direction of the inserted or deleted element. Therefore, the average time complexity of inserting and deleting elements at a random position in a sequential list is O(n), and the time complexity of inserting and deleting elements at a random position in a singly linked list is O(1)

in space

<1> The space of the sequence table is generally opened up continuously, and the space for storing multiple elements will be opened up at one time, so when using the sequence table, multiple data can be written into the cache at one time, and then written into the main memory, the sequence table The CPU cache is more efficient, and the CPU pipeline will not always be interrupted; and the singly linked list only opens up space every time a data needs to be stored, so each data storage must be written to the cache area separately. Then write to the main memory, which causes the CPU cache efficiency of the single linked list to be low, and the CPU pipeline will often be interrupted.

<2> Whether the amount of data is known will also affect the selection and use of singly linked lists and sequential lists. This point of view can be stated in terms of the above pros and cons.

Experimental process and results

the code

//<库函数>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>

//<相关参数的宏定义>
#define Len sizeof(Node)

typedef int Elemtype;

typedef struct Node
{
    
    
	Elemtype data;
	struct Node* next;
}Node;

typedef struct Node Linklist;

//<单链表的初始化、增删查改、输出函数的定义>
Linklist* Creat_L();
void Out_Linklist(Linklist *h);
void Insert_List(Linklist *L,int i,Elemtype x);
void Delete_List(Linklist *L,int i);
void Found_List(Linklist *L,Elemtype x);
void Amend_List(Linklist *L,int i,Elemtype x);

// <主函数>
void main()
{
    
    
	Linklist *L;
	int k ;
	int i ;
	Elemtype x;

	printf("		《 Welcome to use the founction 》\n");
	printf("	    The founction can offer following service\n");
	printf("		*******Please first Initlist*******\n\n");
	printf("		1、Initlist   ->初始化单链表\n");
	printf("		2、Insertlist ->插入操作\n");
	printf("		3、Deletelist ->删除操作\n");
	printf("		4、Foundlist  ->查找操作\n");
	printf("		5、Amendlist  ->更改操作\n");
	printf("		0、Exitlist   ->退出操作\n\n\n");
	printf("        Please choose following digital:1、2、3、4、5、0\n");
	printf("               Finish your idea!!!!!!\n");
	do
	{
    
    
		printf("\n\nPlease Input your choice:");
		scanf("%d",&k);
		switch(k)
		{
    
    
		case 1:
			{
    
    
				L = Creat_L(); 
				Out_Linklist(L);
			}break;
		case 2:
			{
    
    
				printf("Input the position of Insert:");
				scanf("%d",&i);
				printf("Input the data of Insert:");
				scanf("%d",&x);
				Insert_List(L,i,x);
				Out_Linklist(L);
			}break;
		case 3:
			{
    
    
				printf("Input the position of Delete:");
				scanf("%d",&i);
				Delete_List(L,i);
				Out_Linklist(L);
			}break;
		case 4:
			{
    
    
				printf("Input the data of Found:");
				scanf("%d",&x);
				Found_List(L,x);
				//Out_Linklist(L);
			}break;
		case 5:
			{
    
    
				printf("Input the position of Amend:");
				scanf("%d",&i);
				printf("Amend data to:");
				scanf("%d",&x);
				Amend_List(L,i,x);
				Out_Linklist(L);
			}break;
		}
	}while(k!=0);
}

// <初始化函数>
Linklist* Creat_L()
{
    
    
	//int i;
	Linklist *h,*p,*s;
	Elemtype x;
	h = (Node*)malloc(Len);
	h->next = NULL;	
	p = h;
	printf("Please Enter the FIRST Node data: ");
	scanf("%d",&x);
	while(x!=99)                              //假设终止条件
	{
    
    
		s = (Node*)malloc(Len);	
		s->data = x;                          //赋值		
		s->next = NULL;
		p->next = s;
		p = s;	
		printf("Please Enter the NEXT  Node data: ");
		scanf("%d",&x);
	}
	return h;
}

//<输出函数>
void Out_Linklist(Linklist *h)
{
    
    
	Linklist *p;
	p  = h->next;
	printf("Output Linklist:");
	while(p)
	{
    
    
		if(p->next!=NULL)
		{
    
    
			printf("%d -> ",p->data);
			p = p->next;
		}
		else
		{
    
    
			printf("%d",p->data);
			p = p->next;
		}
	}
}

//<插入函数>
void Insert_List(Linklist *L,int i,Elemtype x)
{
    
    
	int j;
	Linklist *p,*s;
	p = L;

	if(i<0)
	{
    
    
		printf("The position is unreasonable!!!!!!!!!\n");
		printf("Please Input Again!!!!!!!\n");
		exit(0);
	}

	for(j=0;j<=i;j++)
	{
    
    
		if(j == i)
		{
    
    
			s = (Node*)malloc(Len);      //声明一个新节点 s ,并填充相关数据,插入链表
			s->data = x;
			s->next = p->next;
			p->next = s;
			break;
		}
		p = p->next;
	}
}

// <删除函数>
void Delete_List(Linklist *L,int i)
{
    
    
	Linklist *p,*s;
	int j;
	Elemtype x;

	p = L;

	if(i<0||p->next == NULL)
	{
    
    
		printf("The position is unreasonable!!!!!!!!!\n");
		printf("Please Input Again!!!!!!!\n");
		exit(0);
	}
	for(j=0;j<=i;j++)
	{
    
    
		if(j==i)
		{
    
    
			s = p->next;             //定义变量 s  指向牺牲节点,并释放
			x = s->data;
			p->next = s->next;			
			free(s);
		}
		p = p->next;
	}
}

// <查找函数>
void Found_List(Linklist *L,Elemtype x)
{
    
    
	Linklist *p;
	int i = 0;
	p = L;
	while(p)
	{
    
    		
		if(p->data == x)
		{
    
    
			printf("Found data in %d position\n",i);
			break;
		}
		else
		{
    
    
			i++;
			p = p->next;
		}		
	}
	if(p==NULL)
		printf("Sorry , The data is not in the Linklist!!!!!!\n");
}

//<修改函数>
void Amend_List(Linklist *L,int i,Elemtype x)
{
    
    
	Linklist *p;
	int j = 0;
	p = L;
	while(p)
	{
    
    
		if(j == i)
		{
    
    
			p->data = x;
			break;
		}
		else
		{
    
    
			j++;
			p = p->next;
		}	
	}
	if(p==NULL)
		printf("The position of inputting is incorrect!!!!!!\n");
}

operation result

★★★Initialization, adding, deleting, checking and modifying basic operations are completed in turn★★★

insert image description here

Summarize

To sum up the above, the sequential list and the singly linked list each have their own advantages and disadvantages. Which one is better to use depends on specific problems and cannot be generalized.

Guess you like

Origin blog.csdn.net/MZYYZT/article/details/113275710