美文网首页
2-3 Reversing Linked List

2-3 Reversing Linked List

作者: Allen的光影天地 | 来源:发表于2018-11-01 20:47 被阅读7次

    02-线性结构3 Reversing Linked List (25 分)

    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个元素做逆序
      接下来就是N行数据输入,三个数分别代表,该元素地址,该元素,该元素指向的下个节点的地址
    • 输出就很容易理解了

    我的答案:

    // C++版本,更简单一些
    
    #include<iostream>
    #include<stdio.h>
    #include<algorithm>    ///使用到reverse 翻转函数
    using namespace std;
    
    #define MAXSIZE 1000010   ///最大为五位数的地址
    
    struct node    ///使用顺序表存储data和下一地址next
    {
        int data;
        int next;
    }node[MAXSIZE];
    
    int List[MAXSIZE];   ///存储可以连接上的顺序表
    int main()
    {
        int First, n, k;
        cin>>First>>n>>k;   ///输入头地址 和 n,k;
        int Address,Data,Next;
        for(int i=0;i<n;i++)
        {
            cin>>Address>>Data>>Next;
            node[Address].data=Data;
            node[Address].next=Next;
        }
    
        int j=0;  ///j用来存储能够首尾相连的节点数
        int p=First;   ///p指示当前结点
        while(p!=-1)
        {
            List[j++]=p;
            p=node[p].next;
        }
        int i=0;
        while(i+k<=j)   ///每k个节点做一次翻转
        {
            reverse(&List[i],&List[i+k]);
            i=i+k;
        }
        for(i=0;i<j-1;i++)
            printf("%05d %d %05d\n",List[i],node[List[i]].data,List[i+1]);
        printf("%05d %d -1\n",List[i],node[List[i]].data);
        return 0;
    }
    

    c版本:

    // C 实现版本, 与老师讲的一致
    
    #include <stdio.h>
    #include <stdlib.h>
    #define MaxSize 100000
    typedef int Ptr;
    //结构数组模拟链表
    struct node{
        int key;
        Ptr next;
    }list[MaxSize];
    
    //逆转某一段含K个结点的链表
    Ptr Reverse ( Ptr head, int K ) {
        Ptr New, Old, Tmp; //New指向逆转链表表头,Old指向未逆转表头,Tmp记录Old后一个结点
        int cnt = 1;
        New = head;
        Old = list[New].next;
        while ( cnt < K ) {
            Tmp = list[Old].next; //记录Old下一个结点,防止逆转指针后丢失
            list[Old].next = New; //逆转指针
            New = Old; Old = Tmp; //New,Old向后转移一个
            cnt++;
        }
        list[head].next = Old; //原来的第一个结点逆转后变为最后一个结点,并连接剩下未逆转的元素
        return New;
    }
    
    //判断是否需要逆转
    int NeedReverse ( Ptr head , int K) {
        int i;
        for( i = 1; list[head].next != -1; head = list[head].next ) {
            i++;
            if ( i == K ) return 1; //还有K个或以上结点,需要逆转
        }
        return 0; //不足K个结点,不需要逆转
    }
    
    //逆转整个链表
    Ptr ReversingLinkedList( Ptr head , int K ) {
        Ptr UnreversedHead = head;  //未逆转的链表的第一个结点
        Ptr ListHead; //整个表的第一个结点
        Ptr TempTail; //临时表尾,用来连接下一段逆转的链表
    
        if ( NeedReverse( UnreversedHead, K ) ) { //第一次先判断是否需要逆转
            ListHead = Reverse( UnreversedHead, K ); //记住逆转后的整个链表的第一个结点
            TempTail = UnreversedHead; //记录此逆转链表的表尾
            UnreversedHead = list[TempTail].next; //记录未逆转链表的表头
        }
        else //链表结点个数小于K,无需逆转
            return head;
    
        while ( NeedReverse( UnreversedHead, K ) ) {
            list[TempTail].next = Reverse( UnreversedHead, K ); //上一个逆转链表的表尾与这个逆转链表的表头连接
            TempTail = UnreversedHead;
            UnreversedHead = list[TempTail].next;
        }
        return ListHead;
    }
    
    //输出链表
    void PrintLinkedList ( Ptr head ) {
        Ptr temp = head;
        for(; list[temp].next !=  -1;  temp = list[temp].next)
            printf("%05d %d %05d\n", temp, list[temp].key, list[temp].next);
        printf("%05d %d %d\n", temp, list[temp].key, list[temp].next);
    }
    
    int main(){
        Ptr ad, head;
        int N, K;
        scanf("%d %d %d", &head, &N, &K);
        for(int i = 0; i < N; i++){
            scanf("%d", &ad);
            scanf("%d %d", &list[ad].key, &list[ad].next);
        }
        PrintLinkedList( ReversingLinkedList( head, K ) );
        return 0;
    }
    

    答案解析

    明天再补上!!

    相关文章

      网友评论

          本文标题:2-3 Reversing Linked List

          本文链接:https://www.haomeiwen.com/subject/pdxfxqtx.html