C language data structure - single linked list

Posted by jgetner on Mon, 20 Sep 2021 08:18:05 +0200

Concept of 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 realized through the pointer link order in the linked list.
Let's start the single linked list

Create a structure first

typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType data;
	struct SListNode* next;
}SListNode;

Dynamic application node

SListNode* BuySListNode(SLTDateType x)
{
	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	//Judge whether the development node is successful
	if (node == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	node->data = x;
	node->next = NULL;
	return node;
}

Tail interpolation of single linked list

How to insert the tail?
The idea of tail interpolation:
1. Conventional tail interpolation: point a plist pointer to the head node and find the following node through the plist pointer. After defining a tail pointer, find the tail node and let the tail node point to the new node
2. If the linked list is empty, point to the new node first and perform tail insertion in

After converting the idea of diagram into code

void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(pplist);//Address cannot be empty
	//The linked list is empty
	if (*pplist==NULL)
	{
		SListNode* newnode = BuySListNode(x);
		*pplist = newnode;
	}
	else
	{
		SListNode* tail = *pplist;
		//Tail finding
		while (tail->next !=NULL)
		{
			tail = tail->next;
		}
		SListNode* newnode = BuySListNode(x);
		tail->next = newnode;
	}
}
```c
//Test code
void test()
{
	SListNode *plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);


This is the tail insertion of the single linked list.

Header insertion of single linked list

// Header insertion of single linked list
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
   //pplist stores the address of plist. Dereference and find plist
	newnode->next = *pplist;
	*pplist = newnode;
}
	SListPushFront(&plist, -1);
	SListPrint(plist);


The head plug was successful.

Tail deletion of single linked list


Idea of tail deletion:
Define a tail pointer and find the tail. Directly judge whether tail - > next is empty. If it is empty, the loop will end. We directly release tail - > next - > next. When tail - > next = null. There is another node, release and empty it.

void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);//The linked list is empty and cannot be deleted
	if ((*pplist)->next==NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SListNode* tail = *pplist;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next->next);
		tail->next = NULL;
	}
}

Test it

	SListPopBack(&plist);
	SListPopBack(&plist);
	SListPrint(plist);

Header deletion of single linked list


Header deletion: define a next pointer. First save d1 - > next, and then let plist point to d2. If you don't save, release d1 first, and d2 will not be found.

void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);//The linked list cannot be deleted if it is empty
	SListNode* next = (*pplist)->next;
	
	free(*pplist);
	(*pplist) = next;
}
    SListPopFront(&plist);
	SListPrint(plist);


-1 is deleted successfully.

Search and modification of single linked list

Find: traverse directly

//Single linked list lookup
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	SListNode *cur = plist;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}

If we find it, we can modify it

SListNode* pos = SListFind(plist,2);
	if (pos)
	{
		printf("eureka\n");
	}
	else
	{
		printf("Can't find\n");
	}
	pos->data = 20;//modify
	SListPrint(plist);

Let's look for 2 and change it to 20

The single linked list inserts a value after the pos position


Point newnode to d3 and pos to newnode

void SlistInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode *newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

We tested inserting 30 after 3

    pos = SListFind(plist, 3);
	if (pos)
	{
		SlistInsertAfter(pos, 30);
	}
	SListPrint(plist);


You successfully inserted 30.

The single linked list inserts a value after the pos position


Make a next to save pos - > next - > next, and release next

void SlistEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode *next = pos->next;
	pos->next = next->next;
	free(next);
	next = NULL;
}

Test the deletion after 1

pos = SListFind(plist, 1);
	if (pos)
	{
		SListEraseAfter(pos);
	}
	SListPrint(plist);


The previous 20 was deleted.
The single chain list of this article is over!

Topics: C data structure linked list