[Algorithm and Data Structure (C Language)] Linear List ----- Linked List

Series of articles catalog: [Algorithms and Data Structures (C Language)] Linear List ----- Linked List  http://t.csdn.cn/sL6c6

Table of contents

foreword

 1. Concept and structure

 2. Classification of linked lists

 3. Realization of linked list 

4. Implementation of doubly linked list

 Second, the difference between sequence list and linked list

at last

Summarize



foreword

The linear list is divided into two parts. The previous article first described the conceptual structure and algorithm implementation of the sequence table. This article describes the conceptual structure, classification and function declaration of the linked list, and the realization of each function.

The following content is for reference only, and you are welcome to criticize and correct me~


提示:以下是本篇文章正文内容,下面案例可供参考

 1. Linked list of linear list 

 1. Concept and structure

The linked list is a non-continuous and non-sequential storage structure in the physical storage structure. The logical order of the data elements is realized through the link order of the pointers in the linked list.

                            

 2. Classification of linked lists

(1) Single or two-way

(2) To lead or not to lead

(3) Cyclic or non-cyclic

 3. Realization of linked list 

// 1、无头+单向+非循环链表增删查改实现
typedef int SLTDateType;
typedef struct SListNode
{
 SLTDateType data;
 struct SListNode* next;
}SListNode;

// 动态申请一个结点
SListNode* BuySListNode(SLTDateType x);

// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);

// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);

// 单链表的尾删
void SListPopBack(SListNode** pplist);

// 单链表头删
void SListPopFront(SListNode** pplist);

// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);

// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x);

// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
void SListEraseAfter(SListNode* pos);

 1.SListNode* BuySListNode(SLTDateType x)

SListNode* BuySListNode(SLTDataType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

 2.void SListPrint(SListNode* plist)

void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;
	while (cur != NULL)
	{
		//printf("[%d|%p]->", cur->data, cur->next);
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

 Note: The typedef keyword defines a new name for a data type (including custom data types (struct, etc.)), which can be used to simplify some more complex type declarations.

typedef int SLTDataType;

typedef struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
}SLTNode;

 3.void SLTPushBack(SLTNode** pphead, SLTDataType x)

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = BuySLTNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLTNode* tail = *pphead;
		// 找尾
		while (tail->next)
		{
			tail = tail->next;
		}

		tail->next = newnode;
	}
}

 4.void SLTPushFront(SLTNode** pphead, SLTDataType x)

void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

5. void SLTPopBack(SLTNode** pphead)

void SLTPopBack(SLTNode** pphead)
{
	assert(*pphead);

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

		free(tail->next);
		tail->next = NULL;
	}
}

6. void SLTPopFront(SLTNode** pphead)

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

7. SLTNode* SLTFind(SLTNode* phead, SLTDataType x)

SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}

		cur = cur->next;
	}

	return NULL;
}

8. void SLTInsertAfter(SLTNode* pos, SLTDataType x)

void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

9. void SLTEraseAfter(SLTNode* pos)

void SLTEraseAfter(SLTNode* pos)
{
	assert(pos);

	if (pos->next == NULL)
	{
		return;
	}
	else
	{
		SLTNode* nextNode = pos->next;
		//pos->next = pos->next->next;
		pos->next = nextNode->next;
		free(nextNode);
		//nextNode = NULL;
	}
}

4. Implementation of doubly linked list

// 2、带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
 LTDataType _data;
 struct ListNode* next;
 struct ListNode* prev;
}ListNode;

// 创建返回链表的头结点.
ListNode* ListCreate();

// 双向链表销毁
void ListDestory(ListNode* plist);

// 双向链表打印
void ListPrint(ListNode* plist);

// 双向链表尾插
void ListPushBack(ListNode* plist, LTDataType x);

// 双向链表尾删
void ListPopBack(ListNode* plist);

// 双向链表头插
void ListPushFront(ListNode* plist, LTDataType x);

// 双向链表头删
void ListPopFront(ListNode* plist);

// 双向链表查找
ListNode* ListFind(ListNode* plist, LTDataType x);

// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);

// 双向链表删除pos位置的结点
void ListErase(ListNode* pos);

1. ListNode* ListCreate()

2. void ListDestory(ListNode* plist)

The single-linked list can be used for reference, which is omitted here.

3. void LTPrint(LTNode* phead)

void LTPrint(LTNode* phead)
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

4. void LTPushBack(LTNode* phead, LTDataType x)

void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);

	LTNode* newnode = BuyListNode(x);
	LTNode* tail = phead->prev;

	tail->next = newnode;
	newnode->prev = tail;

	newnode->next = phead;
	phead->prev = newnode;
}

5. void LTPopBack(LTNode* phead)

void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);  


	LTNode* tail = phead->prev;
	LTNode* tailPrev = tail->prev;

	tailPrev->next = phead;
	phead->prev = tailPrev;
	free(tail);
}

6. void LTPushFront(LTNode* phead, LTDataType x)

void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);

	LTNode* newnode = BuyListNode(x);
	LTNode* first = phead->next;

	// phead newnode first 
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = newnode;
}

7. void LTPopFront(LTNode* phead)

void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead); 

	LTErase(phead->next);
}

8. LTNode* LTFind(LTNode* phead, LTDataType x)

LTNode* LTFind(LTNode* phead, LTDataType x)
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}

		cur = cur->next;
	}

	return NULL;
}

9. void LTInsert(LTNode* pos, LTDataType x)

void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);

	LTNode* prev = pos->prev;
	LTNode* newnode = BuyListNode(x);
	// prev newnode pos
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}

10. void LTErase(LTNode* pos)

void LTErase(LTNode* pos)
{
	assert(pos);

	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	free(pos);

	prev->next = next;
	next->prev = prev;
}

 Second, the difference between sequence list and linked list

difference

sequence table

linked list

storage space

must be physically continuous

Logically continuous, but not necessarily physically continuous

random access

supports O(1)

Not supported: O(N)

Insert or delete elements at any position

May need to move elements, low efficiency O(N)

Just modify the pointer to point to

insert

Dynamic sequence table, when the space is not enough, it needs to be expanded

no concept of capacity

Application Scenario

Efficient storage of elements + frequent access

Frequent insertion and deletion at any position

cache utilization

high

Low


at last

Happy time is always short. The above is what I want to talk about today. This article continues to briefly introduce Comrade Xiao Zhao’s preliminary cognition and realization of the linked list of algorithm and data structure (C language). Family members are welcome to criticize and correct. Comrade Xiao Zhao continues to update, the motivation for continuous learning is the support of Baozi with one button and three consecutive links~

                                                  


Guess you like

Origin blog.csdn.net/weixin_70411664/article/details/128347880