數據結構練習 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個結點構成一條單向鏈L(單向鏈長度不一定爲n,可能有多餘結點),給定整數k,在L上每k個長度進行翻轉。按結點地址,結點數據,指向結點地址的格式輸出翻轉後的鏈。

作死用動態鏈寫,寫後發現各種指針混亂的令人頭疼,但自己作的死哭着也要改好。

簡單點的代碼就是數組存儲加algorithm裏的reverse函數,網上很多。

下面是我巨麻煩的代碼:

#include<iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

typedef struct Node *Stack;
struct Node
{
    int add;
    int dat;
    Stack Next;
};
pair<int,int> Data[100005];
pair<Stack,Stack> p;

int main()
{
    Stack push(Stack top,int k);
    pair<Stack,Stack> pop(Stack top,int k);
    int top,n,k;
//輸入
    cin>>top>>n>>k;
    for(int i=0;i<n;i++)
    {
        int b;
        cin>>b;
        cin>>Data[b].first>>Data[b].second;
    }
    int i=0,m,j,t;
    t=top;
//計算單向鏈真實長度
    while(t!=-1)
    {
        i++;
        t=Data[t].second;
    }
// m需要翻轉的段數 j不需要翻轉的結點數
    m=i/k;
    j=i%k;
//s 進行不斷翻轉的棧
    Stack s=(Stack)malloc(sizeof(Node));
    s->add=top;
    s->dat=Data[top].first;
    s->Next=NULL;
//結果鏈 h爲頭  f爲尾
    Stack f,h=(Stack)malloc(sizeof(Node));
    f=h;
    f->Next=NULL;
//m次翻轉 將每次翻轉後的段放入結果鏈後
    while(m--)
    {
        s=push(s,k);
        p=pop(s,k);
        f->Next=p.first;
        f=p.second;
        f->Next=NULL;
    }
//將不用翻轉的結點依次放入結果鏈後
    if(j)
    {
        t=s->add;
        while(t!=-1)
        {
            Stack x=(Stack)malloc(sizeof(Node));
            x->add=t;
            x->dat=Data[t].first;
            f->Next=x;
            f=x;
            f->Next=NULL;
            t=Data[t].second;
        }
    }
//打印
    h=h->Next;
    while(h)
    {
        if(h->Next)
        {
            printf("%05d %d %05d\n",h->add,h->dat,h->Next->add);
            h=h->Next;
        }
        else
        {
            printf("%05d %d -1\n",h->add,h->dat);
            h=h->Next;
        }
    }
    return 0;
}

Stack push(Stack top,int k)
{
    for(int i=0;i<k;i++)
    {
        Stack s=(Stack)malloc(sizeof(Node));
        s->add=Data[top->add].second;
        if(s->add==-1)  s->dat=-1;
        else s->dat=Data[s->add].first;
        s->Next=top;
        top=s;
    }
    return top;
}

pair<Stack,Stack> pop(Stack top,int k)
{
    Stack l=top->Next,r=top;
    for(int i=0;i<k;i++)
    {
        r=r->Next;
    }
    top->Next=NULL;
    pair<Stack,Stack> p(l,r);
    return p;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章