Detailed explanation of single linked list of data structure

Keywords: C data structure linked list

Linked list of data structure

----------------------------------------------------------Daily chart--------------------------------------------------

preface

In the last article, we analyzed and explained the function and implementation of linear table. In fact, linear table has many disadvantages. In this article, we will explain the linked list

  • Concept:
    Linked list is a non continuous and non sequential storage structure in physical storage structure. The logical order of data elements is through the pointer chain in the linked list
    It is realized in sequence.
    A linked list is like a train, which is composed of each carriage, and there are links between each carriage.
  • Classification: linked list is widely used. The simplest, such as mobile phone address book, is the simplest linked list. There are actually eight types of linked lists:
    1. Headless linked list and headless linked list

    The head node is also called sentry node. This node generally does not store or recommend storing data. It is specially used to facilitate you to find the first node storing valid data at any time. (it is not recommended that the header node store the length of the linked list!! because you are not sure of the node data type, such as double,char)

2. One way linked list and two-way linked list

A two-way linked list means that each node has two pointers, one pointing to the previous node and the other pointing to the next node

3. Circular linked list and non circular linked list

The circular linked list is the tail node. The pointer is not empty, but points to the head node. Equivalent to a circle.

Then the permutation and combination of 2 * 2 * 2 is a total of eight linked list categories. Next, we will focus on single linked list and two-way circular linked list

1, Single linked list

Then we implement it with headless + one-way + acyclic linked list addition, deletion, query and modification

1. Linked list structure

typedef int Datatype;

typedef struct SlistNode
{
	Datatype data;
	struct SlistNode* next;
}SLTNode;

2. Capacity expansion

SLTNode* BuySListnode(Datatype x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
	
}
  • Note here that the newnode node space applied in the function will not disappear with the destruction of the function stack frame. Newnode is the space opened up by malloc on the heap, so return the corresponding pointer and receive it with the pointer.

3. Printing

void SListPrint(SLTNode* phead)
{
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}printf("NULL\n");
}
  • Print normally

4. Insert

Tail insertion

  • Note: first of all, the parameter is the secondary pointer, that is, the primary pointer address. Once the primary pointer is received here, only the formal parameters of the primary pointer in the function are inserted and changed, which does not work in practice.
    Secondly, it is discussed in two cases:
    1) If the header pointer is null, directly give the new node to the header pointer
    2) Normal tail insertion of multiple nodes
void SListPushBack(SLTNode** pphead, Datatype x)
{
	SLTNode* newnode = BuySListnode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
	
}

Head insert

void SListPushFront(SLTNode** pphead, Datatype x)
{

	SLTNode* newnode = BuySListnode(x);
	newnode->next = *pphead;
	*pphead = newnode;

}

5. Delete

Tail deletion

  • There are three situations to discuss: 1) the header pointer is null. 2) The header pointer is just a node. 3) Multiple nodes
void SListPopBack(SLTNode** pphead)
{
	if (*pphead == NULL)
	{
		return;

	}
	else if((*pphead)->next == NULL){
		free(*pphead);
		*pphead = NULL;
	}
	else{
		SLTNode* p = *pphead;
		while (p->next->next != NULL)
		{
			p = p->next;
		}
		free(p->next);
		p->next = NULL;
	}
	
}

Header deletion

void SListPopFront(SLTNode** pphead)
{
	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}

6. Find

  • Normal traversal looks up one by one. If equal, the first occurrence address is returned.
SLTNode* SListFind(SLTNode* phead, Datatype x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

summary

Posted by dksmarte on Mon, 25 Oct 2021 02:38:43 -0700