1. 程式人生 > >02-線性結構3 Reversing Linked List (25 分)

02-線性結構3 Reversing Linked List (25 分)

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

此題不應當使用動態連結串列,因為無法儲存,也不應當使用struct節點的vector容器,雖然可以進行排序但不好輸出,應當使用靜態結構體陣列,在用一個int的陣列來依次存放節點的位置(可以看成指向節點的指標,因為給出了節點的地址),反轉時只需要反轉int陣列,輸出時使用反轉後的節點位置來輸出


#include<cstdio>
#include<algorithm>
using namespace std;
#define maxsize 1000010


struct node                                                     // 定義一個結構體陣列
{
    int data;
    int next;
}node[maxsize];      //結構體陣列

int List[maxsize];
int main() {
    int Adr, Data, Next; //地址 資料 下一個地址
    int first, N, K, i;// 第一個地址 總的節點數 每k反轉節點
    scanf("%d%d%d", &first, &N, &K); //輸入
    for (i = 0; i < N; i++)                                            //輸入資料
    {
        scanf("%d%d%d", &Adr, &Data, &Next);
        node[Adr].data = Data;  //會出現為零的
        node[Adr].next = Next;
    }
    int p = first; //第一個節點的地址
    int j = 0;
    while (p != -1)                                      //將所有在連結串列上的結點存放在一個數組中,
    {//如果指標沒有指向末尾
        List[j++] = p; //用int 陣列依次儲存節點的地址
        p = node[p].next;
    }
    i = 0;
    while (i + K <= j)    //假設k為4,j為9                                              // 每k個進行反轉並迴圈
    {                  //i+k=4
        reverse(&List[i], &List[i + K]); //反轉
        i = i + K;//i = 4
    }
    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;
}
//
// Created by wangzhiyong on 18-9-16.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#define Max_Node_Num 100002
using namespace std;


typedef struct Node *NextNode;
struct Node
        {
    int Address;
    int Data;
    int Next;
        };
void OutPut(vector<Node> List);
bool SortByM1( const Node &v1, const Node &v2);

int main()
{
    vector<struct Node> List,List1;

    int Address,Data,Next,N,K;
    cin>>Address>>N>>K;
    Node temp;
    for(int i = 0;i<N;i++)
    {
        cin>>Address>>Data>>Next;

        temp.Address = Address;
        temp.Data = Data;
        temp.Next = Next;
        List.push_back(temp);
    }
//    OutPut(List);
//    cout<<"************************"<<endl;
//    cout<<"************************"<<endl;
    sort(List.begin(),List.end(),SortByM1);
//    OutPut(List);
//    cout<<"************************"<<endl;
//    cout<<"************************"<<endl;
    int m = 0;
    while(m+K <= N)
    {
        reverse(&List[m],&List[m+K]);
        m = m+K;
    }
    OutPut(List);
    return 0;
}

void OutPut(vector<Node> List)
{

    for (int i = 0; i < List.size() - 1; i++) {
        printf("%05d %d %05d\n", List[i].Address, List[i].Data, List[i+1].Address);   //  按格式輸出
    }
    printf("%05d %d -1\n", List[List.size()-1].Address, List[List.size()-1].Data);//最後一個節點的輸出格式
}
bool SortByM1( const Node &v1, const Node &v2)//注意:本函式的引數的型別一定要與vector中元素的型別一致
{
    return v1.Data < v2.Data;//升序排列
}

此程式碼無法通過後兩個測試節點

最大N,最後剩K-1不反轉 答案錯誤 264 ms 4596KB
6 有多餘結點不在連結串列上 答案錯誤