C/C++ data structure---sequence table---chained storage structure 2 (without head node)

 Personal homepage: There are still unknowns waiting to be explored_data structure, small projects, Luogu question brushing-CSDN blog

Topic Column: Data Structure_There are still unknown blogs waiting to be explored-CSDN Blog 

Previous article link: Data structure---sequence table---chain storage structure 1 (without head node)_There are still unknown blogs waiting to be explored-CSDN blog

Table of contents

I. Introduction

2. Basic operations of linked lists 

1. Add data (add nodes)

1) Ideas

2) Code presentation

2. Delete node 

1) Ideas

 2) Code implementation

3. Change node data 

1) Ideas

2) Code implementation 

4. Find data 

1) Ideas

 2) Code implementation

3. Summary


I. Introduction

        According to the explanation in the previous article, you must be able to create and initialize the linked list yourself. But for linked lists, these operations are not yet able to complete some functions and are not enough to apply it to some scenarios. Next, we need to implement operations such as adding, deleting, modifying, and checking the linked list to make the linked list more convenient for us to use. The linked list discussed in these two times is the most basic single linked list. Next, there are doubly linked lists, circular linked lists, etc. You must lay a solid foundation before learning these.

        Explain an operation and ask: Why should we allocate space for nodes and when should we allocate space? (This is what I just figured out recently. If you understand it wrong, please point it out and we can make progress together.) Answer: Pointers only occupy 4/8 bytes in memory (related to computers). Pointers can only store addresses, so pointers The data field and pointer field of the node cannot be stored in it. Therefore, space needs to be opened up to store data in the data field and pointer field.

2. Basic operations of linked lists 

1. Add data (add nodes)

1) Ideas

As always, let's think about it first, how to add nodes?

First, there must be a linked list (just the head pointer will do), and then there must be a node p to be inserted and its specific location.

When position P is found, you also need to consider whether to insert it in front of or behind position P, and finally start the insertion operation. How to insert it? Can this operation be performed normally when head is empty?

        Insert the p node into the linked list as shown above. It is nothing more than what is drawn in the picture above. Cut off the black line of serial number 3, and then connect the two blue lines of serial number 1 and serial number 2. (Is this when head is empty ? Does it also apply? I will verify it later when I write the code ). Now there is another question. Should I connect the line with serial number 1 or the wire with serial number 2 first?  Then if you connect the line with serial number 1 first, you need to disconnect the black wire with serial number 3 and then connect the wire with serial number 1. When you want to connect the wire with serial number 2, you find that you cannot connect because the node that the previous serial number 3 points to The address cannot be found.

        Finally, according to deduction, connect serial number 2 first and then serial number 1.

2) Code presentation

List ListAdd(List head,int X,Position P)
{
	//前插
	List r = head,q;
	q = (List)malloc(sizeof(struct LNode));
	q->next = NULL;
	q->data = X;
	if (r == NULL)
	{
		r = q;
		return head;
	}
	while (r!=NULL)
	{
		if (r->next == P)
		{
			q->next = P;
			r->next = q;
			return head;
		}
		r = r->next;
	}
	return NULL;
}

        When head is empty, it has no effect on the above steps. However, in the above code, to find the position to be inserted, you need to find the previous node of the position to be inserted. When traversing, you need to use its pointer field. If head is empty, head->next will be out of bounds, so you need to check whether head is There is no time to discuss it. 

        What is written above is to insert data in front of the given data, so how to insert it later? Answer: Post-insert can exchange the data of the inserted node with the data of the node at the specific location based on the forward-insert. Is it equivalent to performing a post-insert operation?

2. Delete node 

1) Ideas

        Deleting a node also requires a specific deletion location. As long as the pointer field of the previous node to be deleted points to the next node to be deleted, is it equivalent to directly jumping over the node to be deleted? The last thing to note is Use the free function to release the space of the node to be deleted.

 2) Code implementation

List ListDelete(List head, Position P)
{
	List r = head;
	if (r == NULL)
	{
		printf("链表为空,无需删除数据\n");
		return NULL;
	}
	while (r != NULL)
	{
		if (r->next == P)
		{
			if (P->next == NULL)//判断是否P为链表的最后一个
			{
				r->next = NULL;
			}
			else
			{
				r->next = P->next;
			}
			free(P);//释放P的空间
			return head;
		}
        r = r->next;
	}
	printf("未找到\n");
	return NULL;
}

3. Change node data 

1) Ideas

The most important thing for the node data is to find the node, and then find the data in the node.

2) Code implementation 

List ListChange(List head, Position P)
{
	if (head == NULL)
	{
		printf("此链表为空\n");
		return NULL;
	}
	else
	{
		List r = head;
		while (r != NULL)
		{
			if (r == P)
			{
				int data;
				scanf("%d", &data);
				P->data = data;
				return head;
			}
			r = r->next;
		}
		printf("未找到\n");
		return NULL;
	}
}

None of these functions are too difficult, you just need to consider each situation comprehensively.

4. Find data 

1) Ideas

Traverse, then compare the data of the nodes with the searched data one by one, and then output the results.

 2) Code implementation

List ListSearch(List head, int data)
{
	List r = head;
	if (r == NULL)
	{
		printf("此为空链表,未找到\n");
		return NULL;
	}
	else
	{
		while (r->data == data)
		{
			printf("找到了\n");
			return r;
		}
		printf("未找到\n");
		return NULL;
	}
}

3. Summary

The overall difficulty of this code idea is not high. The main thing is that it is careful and considers all situations. Finally, thank you for your support!

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/132943009