Linked list advanced operation 1: swap the positions of adjacent nodes and exchange all the odd and even positions of the nodes (graphic demonstration, easy to understand)

There are many achievable operations on linked lists. Next, let’s talk about some advanced operations on linked lists.

Let's first talk about how adjacent nodes are exchanged

One, create a linked list first

Go directly to the code (students who don’t know how to create a linked list can read my last article)

//需要的两个头文件
#include<stdio.h>
#include<stdlib.h>

//定义结构体(包含指针域和数据域)
typedef struct Node
{
    
    
	int data;
	struct Node *next;
 } Node;
 
 
 int main()
 {
    
    
 	Node *p1,*p2;      //定义两个指针进行各种操作(可看我的上一篇文章)
 	Node *head;       //定义head指针标记头结点

	int x; 	
 	head = NULL;
 	
 	
 	printf("请输入创建多少个链表:"); 
 	scanf("%d",&x);
 	
// 	创建链表节点并连接 
	for(int i=1;i<=x;i++)
 	{
    
    
 		p1 = (Node *)malloc(sizeof (Node));
 		if(head == NULL)
			{
    
    
		 		head = p1;
		 		p2 = p1;
			}
		else
		{
    
    
			p2->next =p1;
			p2 = p1;
		}
	}
	
	p2->next = NULL;    //把尾结点标记为NULL
	p = head;

At this time, the linked list diagram is:

Insert picture description here

The order of the linked list is already fixed. If we want to exchange the positions of adjacent nodes in the linked list, such as exchange 2 and 3 nodes, what should we do? How to achieve it with two pointers?

2. First try to change the positions of the second and third nodes

The position after the exchange is 1→3→2→4→...that is, we need to point the node 1 to the position of 3, the node 2 to the position of 4, and then invert the 2,3 nodes to realize the 2,3 node exchange.

The operation is shown in the figure:
Insert picture description here

It seems that this picture is not very intuitive. . If you don’t understand it, you still need to think about it by yourself. In fact, 1→2→3→4 becomes 1→3→2→4

Directly on the code:

为方便理解,我们先定义三个指针*p,*p1,*p2分别指向1,23的位置
即:p = head;
	p1 = p->next;
	p2 = p1->next;
调换位置的操作:
	p->next = p2;                1指向3的位置
	p1->next = p2->next;         2指向4的位置
	p2->next = p1;               3又指向2的位置
	

3. Input and output data, check whether the operation is correct

输入数据
	int q=1;
	while (p != NULL)
	{
    
    
		p->data = q;
		q++;
		printf("%d ",p->data);
		p = p->next;
	 } 
	
	printf("\n");
输出数据:
	p = head;
	
	while (p != NULL)
	{
    
    
		printf("%d ",p->data);
		p = p->next;
	 } 

operation result:


请输入创建多少个链表:4
1 2 3 4
1 3 2 4
--------------------------------
Process exited after 4.014 seconds with return value 0
请按任意键继续. . .

The operation to switch positions 2, 3 was successful

How to exchange the odd and even positions of all nodes in the linked list?

This is a question for my roommate. It is probably to realize the position exchange of 1 and 2, and the position of 3 and 4. I have studied for half an hour, and I want to send it out to share my thoughts.

Talk about my ideas

In fact, it is the advanced operation of swapping adjacent positions that I just talked about. Just add one more loop to achieve. The advantage of defining three pointers is that the p pointer is used to mark the position, and p1 and p2 are used to realize the function of swapping positions.

As can be seen from the above, in order to exchange the positions of two nodes, we at least need to know the position of the previous one of the exchanged nodes. In other words, if you want to change the positions of nodes 2, 3, you must know the position of node 1, otherwise, node 1 cannot point to the position of node 3 and the operation fails.

Note: As long as the positions of nodes 1, 2 are exchanged, there is no need to mark the position of the previous node, so we have to specialize (replace these two positions first)

1. Change the position of node 1, 2 first

代码如下: 
	p1=head;                 p1指向1 节点的位置 
 	p2=head->next;           p2指向2 节点的位置
 	
 			
	head=p2;                 因为2节点要变成头结点,所以p2标记为head
 	p1->next = p2->next;     1指向3
 	p2->next = p1;           2指向1

	已经完成了1,2节点位置的调换

Second, the position of the node after the design cycle is exchanged

We found that p-nodes need to mark the positions of even-numbered nodes. That is, swapping 3 and 4 will mark 2 nodes, and swapping 5 and 6 will mark 4 nodes.

Because it is always p1 from odd to even. So just use p to mark the position of p1 after the exchange is complete.

Directly on the code:

调换:

p = p1;                              标记上一次调换完成后p1的位置


for(a=1;a<=(x/2)-1;a++)              for循环语句,由于已经调换了1,2,所以循环次数-1
{
    
    
	p1 = p->next;                    
	p2 = p1->next;                   p→p1→p2           
	
	p->next = p2;                   
	p1->next = p2->next;             进行调换奇数偶数节点的调换
	p2->next = p1;
	
	p=p1;                            标记调换完成后p1的位置
}

3. Output the data of the linked list and check whether the operation is correct

代码如下:

	p = head;
	
	
	while(p != NULL)
	{
    
    
		printf("%d ",p->data);
		p = p->next;
	}
	

operation result:


请输入创建多少个链表:9
反转前:1 2 3 4 5 6 7 8 9
反转后:2 1 4 3 6 5 8 7 9
--------------------------------
Process exited after 2.78 seconds with return value 0
请按任意键继续. . .

The advanced operation will be updated from time to time in the future, I hope everyone can like it and follow the collection~ If
you have any questions, you can discuss it together, thank you for watching

Guess you like

Origin blog.csdn.net/qq_54547310/article/details/115279482