C/C++ data structure---Singly linked list reversal (PTA)

Homepage:

There are still unknowns waiting to be explored_Data structure, C language difficulties, small projects-CSDN Blog

Topic columns:

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

Table of contents

I. Introduction

2. Title

Function interface definition:

Sample Referee Test Procedure:

Output sample:

3. Understanding + Code

1. Understand

2.Analysis 

3.Code 


I. Introduction

        For those who are learning data structures for the first time, be sure to write the most basic and simple examples very proficiently. Each time you write them, you will increase your understanding of the structure and lay a solid foundation for the more difficult knowledge in the future. ! You must not blindly copy other people's code. In the end, you must write it yourself!

2. Title

This question requires the implementation of a function to reverse a given singly linked list.

Function interface definition:

List Reverse( List L );

The Liststructure is defined as follows:

typedef struct Node *PtrToNode;
struct Node {
    ElementType Data; /* 存储结点数据 */
    PtrToNode   Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */

LGiven a singly linked list, the function Reverseshould return the reversed linked list.

Sample Referee Test Procedure:

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表 */

List Reverse( List L );

int main()
{
    List L1, L2;
    L1 = Read();
    L2 = Reverse(L1);
    Print(L1);
    Print(L2);
    return 0;
}

/* 你的代码将被嵌在这里 */

5
1 3 4 5 2

Output sample:

1
2 5 4 3 1

Code length limit

16 KB

time limit

400 ms

memory limit

64 MB

3. Understanding + Code

1. Understand

When you see this question, you may suddenly feel very familiar, as if it is very similar to the string reversal you have learned before.

There are many ideas for string reversal :

1. Non-recursive: Just use two variables, one variable left points to the first character of the string, and the other variable right points to the last character of the string, and then exchange them to determine the end sign. (The key code is as follows)

while(left<right)
{
    //交换
}

2. Recursion, realizing reverse order according to the characteristics of recursion

#include<stdio.h>
int my_strlen(char* arr)
{
	if (*arr != '\0')
		return 1 + my_strlen(arr + 1);
	else
		return 0;
}
char* rev(char* arr)
{
	char tmp = *arr;
	int len = my_strlen(arr);
	*arr = *(arr + len - 1);
	*(arr + len - 1) = '\0';
	if (my_strlen(arr+1) >=2)
		rev(arr + 1);
	*(arr + len - 1) = tmp;
}
int main()
{
	char arr[] = "abcdef";
	rev(arr);
	printf("%s", arr);
	return 0;
}

Question: Speaking of which, can the reverse order of a singly linked list also be used in this way?

Answer: 1. If you use the first idea, how do you move the pointer to the last node forward? This requires a loop to find the address of the previous node that only wants the last node, which is more troublesome. 2. If you use the second idea, just write it on paper. I feel that it can be used. Here is a small question for everyone to think about (Imitate it based on the above recursive writing method and see if it can be used) accomplish).

2.Analysis 

In addition to the ideas used in string reversal, is there any way to complete the reversal of this singly linked list based on the characteristics of the linked list?

When creating a linked list, I introduced two methods, one is the head insertion method, and the other is the tail insertion method. What are the characteristics of these two methods? Do you still remember? (The link I don’t remember is as follows:) Data structure---sequence table---chain storage structure 1 (without leading node)_There are still unknown blogs waiting to be explored-CSDN blog 

The most essential feature of head interpolation and tail interpolation is the order of data storage and the order of input data after creation. The head interpolation method is that the storage order is opposite to the order of the input data, and the tail interpolation method is that the storage order is the same as the order of the input data.

Therefore, according to the idea of ​​​​head interpolation, can we read the data of the singly linked list one by one in order, and then use the head interpolation method to create a new linked list for storage? The answer is yes. Moreover, this method is simpler than the previous idea and better embodies some characteristics of the linked list.

  //The head insertion method does not require judging whether the head is empty, which was also explained in the previous article.

3.Code 

List Reverse( List L )
{
    List p=L,head=NULL,q;//创建一个头指针
    //头插法不需要对head是否为空进行判断,再之前的文章中也讲解过
    while(p!=NULL)
    {
        q=(List)malloc(sizeof(struct Node));
        q->Next=NULL;
        q->Data=p->Data;
        q->Next=head;
        head=q;
        p=p->Next;
    }
    if(L!=NULL)
        L->Next=NULL;
    return head;
}

 thanks for your support!

Guess you like

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