[Data Structure] - Single Linked List of Linked List (Part 2)


foreword

The future is hidden in the mist, which makes people seem timid. If you step into it, the clouds will clear up.
This chapter is about the linked list in the data structure, the single linked list (Part 2)


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

1. Singly linked list (below)

1.1 Search+Modify

//SList.h
SLTNode* STFind(SLTNode* phead, SLTDataType x);
//SList.c
SLTNode* STFind(SLTNode* phead, SLTDataType x)
{
    
    
	SLTNode* cur = phead;
	while (cur)
	{
    
    
		if (cur->data == x)
		{
    
    
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
//Test.c
void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		pos->data = 30;
	}
	SLTPrint(plist);
}

insert image description here

1.2 Insert at any position

1.2.1 Insert at pos position (that is, before pos position)

//SList.h
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);//在pos之前插入
//SList.c
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
    
    
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
    
    
		SLPushFornt(pphead, x);
	}
	else
	{
    
    
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}
		SLTNode* newnode = BuyTNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}
//Test.c
void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLInsert(&plist, pos, 30);
	}
}

flow chart

  • multiple nodes

insert image description here

  • a node
    insert image description here

1.2.2 Insert after pos position

//SList.h
void SLInsertAfter(SLTNode* pos, SLTDataType x);//在pos之后插入
//SList.c
void SLInsertAfter(SLTNode* pos, SLTDataType x)
{
    
    
	assert(pos);
	SLTNode* newnode = BuyTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
//Test.c
void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 2);
	if (pos)//判断是否为空
	{
    
    
		SLInsertAfter(pos, 30);
	}
	SLTPrint(plist);
}

flow chart:
insert image description here
Notice:
There is a problem with the following writing method. newnode points to itself and causes an error.

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

flow chart:
insert image description here

1.3 Delete anywhere

1.3.1 Delete the pos position value

//SList.h
void SLErase(SLTNode** pphead, SLTNode* pos);
//SList.c
void SLErase(SLTNode** pphead, SLTNode* pos)
{
    
    
	assert(pphead);
	assert(pos);
	if (pos == *pphead)
	{
    
    
		SLPopFornt(pphead);//头删
	}
	else
	{
    
    
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}
//Test.c
void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLErase(&plist, pos);
	}
	SLTPrint(plist);
}

flow chart:
insert image description here

1.3.2 Delete the value behind the pos position

//SList.h
void SLEraseAfter(SLTNode* pos);
//SList.c
void SLEraseAfter(SLTNode* pos)
{
    
    
	assert(pos);
	assert(pos->next);
	SLTNode* next = pos->next;
	pos->next = next->next;
	free(next);
}
//Test.c
void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
		SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLEraseAfter(pos);
	}
	SLTPrint(plist);
}

flow chart:
insert image description here

2. Complete code

//SList.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

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

void SLTPrint(SLTNode* phead);
void SLPushFornt(SLTNode** pphead, SLTDataType x);
void SLPushBack(SLTNode** pphead, SLTDataType x);

void SLPopFornt(SLTNode** pphead);
void SLPopBack(SLTNode** pphead);


SLTNode* STFind(SLTNode* phead, SLTDataType x);

//任意位置插入
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);//在pos之前插入
void SLInsertAfter(SLTNode* pos, SLTDataType x);//在pos之后插入
void SLErase(SLTNode** pphead, SLTNode* pos);//删除pos位置的值
void SLEraseAfter(SLTNode* pos);//删除pos位置的值





//SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"

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

SLTNode* BuyTNode(SLTDataType x)
{
    
    
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

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

void SLPushBack(SLTNode** pphead, SLTDataType x)
{
    
    
	SLTNode* newnode = BuyTNode(x);
	//空链表
	if(*pphead==NULL)
	{
    
    
		*pphead = newnode;
	}
	//非空链表
	else
	{
    
    
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
    
    
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SLPopFornt(SLTNode** pphead)
{
    
    
	//空链表
	assert(*pphead);
	//一个节点
	if ((*pphead)->next == NULL)
	{
    
    
		free(*pphead);
		*pphead = NULL;
	}
	//多个节点:保存下一个节点,释放当前节点
	else
	{
    
    
		SLTNode* del = *pphead;
		*pphead = del->next;
		free(del);
	}
}

void SLPopBack(SLTNode** pphead)
{
    
    
	//空链表
	assert(*pphead);
	//有一个节点
	if ((*pphead)->next == NULL)
	{
    
    
		free(*pphead);
		*pphead = NULL;
	}
	有多个节点
	else
	{
    
    
	SLTNode* prev = NULL;
	SLTNode* tail = *pphead;
	//找尾
	while (tail->next == NULL)//或者写成while(tail->next)
	{
    
    
		prev = tail;
		tail = tail->next;
	}
	free(tail);
	prev->next = NULL;
		while (tail->next->next != NULL)
		{
    
    
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

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


void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
    
    
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
    
    
		SLPushFornt(pphead, x);
	}
	else
	{
    
    
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}
		SLTNode* newnode = BuyTNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}

//在pos之后插入
void SLInsertAfter(SLTNode* pos, SLTDataType x)
{
    
    
	assert(pos);
	SLTNode* newnode = BuyTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

//删除pos位置的值
void SLErase(SLTNode** pphead, SLTNode* pos)
{
    
    
	assert(pphead);
	assert(pos);
	if (pos == *pphead)
	{
    
    
		SLPopFornt(pphead);//头删
	}
	else
	{
    
    
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
    
    
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

//删除pos位置后面的值
void SLEraseAfter(SLTNode* pos)
{
    
    
	assert(pos);
	assert(pos->next);
	SLTNode* next = pos->next;
	pos->next = next->next;
	free(next);
}




//Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"

void TestSList()
{
    
    
	SLTNode* plist = NULL;
	SLPushFornt(&plist, 1);
	SLPushFornt(&plist, 2);
	SLPushFornt(&plist, 3);
	SLPushFornt(&plist, 4);
	SLTPrint(plist);
	SLPushBack(&plist, 5);
	SLPopFornt(&plist);
	SLTPrint(plist);
	SLPopFornt(&plist);
	SLTPrint(plist);
	SLPopFornt(&plist);
	SLTPrint(plist);
	SLPopFornt(&plist);
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLInsert(&plist, pos, 30);
	}
	SLTNode* pos = STFind(plist, 2);
	if (pos)//判断是否为空
	{
    
    
		SLInsertAfter(pos, 30);
	}
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLErase(&plist, pos);
	}
	SLTPrint(plist);
	SLTNode* pos = STFind(plist, 3);
	if (pos)//判断是否为空
	{
    
    
		SLEraseAfter(pos);
	}
	SLTPrint(plist);
}


int main()
{
    
    
	TestSList();
	return 0;
}

Summarize

Ending, this is the end of the content of today's single-linked list (below)~ If you want to know more, please follow me, one-click three links~

Guess you like

Origin blog.csdn.net/ljq_up/article/details/130309535