[Data Structure-C] Basic Operations and Graphical Analysis of Singly Linked Lists

content

Introduction to singly linked list​

Basic operation and graphic analysis of singly linked list

1. Create a new node

2. The head insertion method inserts a node into the singly linked list​

3. Tail insertion method inserts a node into a singly linked list​

4. Head delete method to delete singly linked list node​

5. Tail deletion method to delete singly linked list node​

6. Return the length of the singly linked list​

7. Delete any node of the singly linked list​

8. Find a node​

9. Insert a new node before a node

Summary​:


Introduction to singly linked list

Singly linked list node : data field + pointer field (pointing to the next node)

Node representation:

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode;

Singly linked list head pointer : always points to the first node in the linear list, no node points to NULL

The storage characteristics of the linear table chain type : it is a group of arbitrary storage units to store the data elements of the linear table (physical addresses can be continuous or discontinuous).

The logical characteristics of the linear list chain : the relationship between data elements is indicated by the head pointer and the pointer in the node.

Summarize:

The singly linked list can find the first node through the head pointer, and the first node can find the next node through its pointer field, and so on. found.

Basic operation and graphic analysis of singly linked list

1. Create a new node

LNode * NewNode(EleType data)
{
	LNode* newNode = (LNode*)malloc(sizeof(LNode));//创建新结点

	//初始化结点
	newNode->data = data;
	newNode->next = NULL;

	return newNode;//返回新结点指针
}

2. The head insertion method inserts a node into the singly linked list

When the linked list is empty:

 When the linked list is not empty

void LinkListPushFront(LinkList** pphead, EleType data)
{
	LNode* newNode = NewNode(data);

	if (*pphead == NULL)//如果链表为空,将头指针指向新结点
    {
		*pphead = newNode;
	}
	else 
    {
		newNode->next = *pphead;//新结点的指针域指向头结点
		*pphead = newNode;//头指针指向新结点
	}
}

3. Tail insertion method inserts a node into a singly linked list

When the linked list is empty:

When the linked list is not empty

void LinkListPushBack(LinkList** pphead, EleType data)
{
	LinkList* tail = *pphead;
	LNode* newNode = NewNode(data);
	if (*pphead == NULL)//链表为空,直接将头指针指向该节点
     {
		*pphead = newNode;
		return;
	}
	else
	{	
		while (tail->next != NULL)//找到最后一个节点
        {
			tail = tail->next;
		}

	}
	tail->next = newNode;//将最后一个节点和新节点连接
}

4. Head deletion method to delete singly linked list nodes

When the linked list is empty, it is not deleted.

When there are multiple elements in the linked list:

void LinkListPopFront(LinkList **pphead)
{
	if (NULL == *pphead)//检查链表是否为空链表
	{
		perror("NULL\n");
		return;
	}
	else
	{
		LinkList *temp = *pphead;
		*pphead = (*pphead)->next;//头指针指向第二个结点
		free(temp);//释放第一个结点空间
	}

}

5. Tail deletion method deletes singly linked list nodes

When the linked list is empty, it is not deleted.

When there is only one element in the linked list, the node is released, and the head pointer points to null.

When there are multiple nodes in the linked list:

void LinkListPopBack(LinkList **pphead)
{
	if (NULL == *pphead)//空
	{
		perror("链表为空\n");
		return;
	}
	else if ((*pphead)->next == NULL)//一个节点,直接删除
	{
		free(*pphead);
		*pphead =NULL;
	}
	else
	{
		LinkList *tail = *pphead;
		LinkList *pre = NULL;//保存倒数第二个结点的地址
		while (tail->next !=NULL)//寻找尾结点
		{
			pre = tail;
			tail = tail->next;
		}
		free(tail);//释放删除的结点
		pre->next = NULL;
	}
}

6. Return the length of the singly linked list

Time complexity: O(n)

int SizeLinkList(LinkList* pphead)
{
	int count = 0;//计数器

	if (pphead == NULL)
	{
		return 0;
	}
	else
	{
		while (pphead != NULL)
		{
			count++;
			pphead = pphead->next;
		}
		return count;
	}
}

7. Delete any node in the singly linked list

Time complexity: O(n)

Delete the first position as the head delete

int DeletElem(LinkList** pphead, int pos)
{
	if (*pphead == NULL)
	{
		return 0;
	}
	else if (pos == 1)
	{
		LinkListPopFront(pphead);
		return 1;
	}
	else
	{
		if (pos > SizeLinkList(*pphead))//判断删除的位置是否大于链表长度
		{
			return 0;
		}
		LinkList* tail = *pphead;//遍历链表节点 
		LinkList* pre = NULL;//删除结点的前驱结点
		while (tail->next != NULL && pos>1)
		{
			pre = tail;
			tail = tail->next;
	        pos--;
		}
        pre->next=tail->next;//将前驱结点和tail->next连接
        free(tail);
        return 1;
	}
}

8. Find a node

Time complexity: O(n)

LNode* FindElem(LinkList* pphead, EleType data)
{
	if (pphead == NULL)
	{ 
		return NULL;
	}
	else
	{
		while (pphead != NULL)
		{
			if(pphead->data == data)
			{
				return pphead;
			}
			pphead = pphead->next;
		}
		return NULL;
	}

9. Insert a new node before a node

Time complexity: O(n)

void LinkListInsert(LinkList** head, LNode* pos, EleType data)
{
	if (pos == *head)
	{
		//头插
		LinkListPushFront(head,data);
	}
	else if (pos == NULL)
	{
		return;
	}
	else 
	{
		LNode* newNode = NewNode(data);
		LNode* tail = *head;
		while (tail->next != pos )//查找待插入结点的前一个结点
		{
			tail = tail->next;
		}
		tail->next = newNode;
		newNode->next = pos;
	}
}

Summary :

Advantages of singly linked list: During insertion and deletion operations, only the link address of the node on the node to be deleted needs to be modified, and elements do not need to be moved, thus improving the disadvantage that insertion and deletion operations in sequential storage structures need to move a large number of elements .

Disadvantages of singly linked list:

1. The problem that the table length is difficult to determine caused by continuous storage allocation is not solved.

2. The random access characteristic of sequential storage structure is lost.

Finally, I wish you all: family happiness and success in your studies.

Guess you like

Origin blog.csdn.net/qq_52763385/article/details/122642165