PTA 02-线性结构3 Reversing Linked List 题目解析

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构全AC代码与题目解析(C语言)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10^5) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next     

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218     
    

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

题目解析:

单链表翻转,给定链表长度N和常数K,在链表中每K个元素进行链表翻转,最后不足K个的保持原来顺序

输入说明:

第一行三个数分别表示链表头的地址,链表长度N以及每多少个元素进行翻转的K值

之后紧接N行,每一行都表示一个链表结点信息,注意此处给出的链表结点顺序是乱序的。每一行的结点信息有三个数,依次是该结点地址、结点值以及连接的下一个元素地址(如果是最后一个元素则为-1)

输出说明:

将链表按照翻转后的顺序输出,每一个结点占一行,输出格式和输入的结点格式相同

题目分析:

该题目可以分为如下几步:

  1. 读入所有结点到数组中
  2. 将所有结点链接起来
  3. 每隔K个元素进行翻转
  4. 输出结果链表

该题有几个陷阱需要注意:

  1. 输入的链表结点信息可能某个结点的下一个连接位置在所有给定的输入中找不到,这时候实际的链表数小于给定链表长度
  2. 在第2步里如果每次查找某个结点的下一个结点是对所有进行遍历查找时会运行超时,即时间复杂度不能为O(n^2)。因为每个结点的地址都是用整形数表示,因此可以提前声明一个大数组,将每个结点按照其地址存到对应索引位置,这样查找某节点的下一个结点的时间复杂度就为O(1),则第2步的时间复杂度就变为了O(n)
  3. 翻转链表结点时,每个结点指向的下一个结点地址也要改变

Code:(C实现)

#include 
#include 

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

#define MaxSize 100000
struct Node arr[MaxSize];

List ReadList(Position firstAddr, int *length)
{
    int i, list_size;
    Position addr, pos;
    List L, p, temp;
    L = (List)malloc(sizeof(struct Node)); L->Nextptr = NULL;
    p = L;
    memset(&arr, -1, sizeof(struct Node));

    for ( i = 0; i < (*length); ++i ) {
        scanf("%d", &addr);
        arr[addr].Address = addr;
        scanf("%d %d", &arr[addr].Data, &arr[addr].Next);
    }

    list_size = 0;
    pos = firstAddr;
    while (arr[pos].Address != -1) {
        p->Nextptr = &arr[pos];
        p = p->Nextptr;
        ++list_size;
        if (arr[pos].Next == -1)
            break;

        pos = p->Next;
    }

    *length = list_size;
    temp = L; L = L->Nextptr; free(temp);
    return L;
}

List Reverse(List L, int reverse_length)
{
    List p1, p2, p3, rear;
    p1 = L; p2 = L->Nextptr; p3 = p2->Nextptr;

    while (reverse_length--) {
        p2->Nextptr = p1;
        p1 = p2;
        p2 = p3;
        if(p3) p3 = p3->Nextptr;
    }
    rear = L->Nextptr;
    rear->Nextptr = p2;
    L->Nextptr = p1;
    return rear;
}

List ReverseList(List L, int length, int reverse_length)
{
    if (reverse_length == 1)
        return L;

    int t;
    List head, p, temp;
    head = (List)malloc(sizeof(struct Node)); head->Nextptr = L;
    p = head;
    t = length / reverse_length;
    while (t--) {
        p = Reverse(p, reverse_length);
    }
    if (length % reverse_length == 0)
        p->Nextptr = NULL;
    temp = head;
    head = head->Nextptr;
    free(temp);
    return head;
}

void PrintList( List L )
{
    List p;
    p = L;
    while (p) {
        if (!p->Nextptr)
            printf("%.5d %d %d\n", p->Address, p->Data, -1);
        else
            printf("%.5d %d %.5d\n", p->Address, p->Data, p->Nextptr->Address);
        p = p->Nextptr;
    }
}

int main()
{
    Position firstAddr;
    int N, K;
    List L;
    scanf("%d %d %d", &firstAddr, &N, &K);

    L = ReadList(firstAddr, &N);
    L = ReverseList(L, N, K);
    PrintList(L);

    return 0;
}

你可能感兴趣的:(C语言,数据结构,链表)