[pta]02-Linear structure 3 reversing Linked List (25 points)

Source: Internet
Author: User
02-Linear structure 3 reversing Linked List (25 min)

Given a constant K and a singly linked list L, you is supposed to reverse the links of every K Elements on L. For example, given LLL 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 the one test case. For each case, the first line contains the address of the first node, a positive N (≤10^5) which are the total number of nodes, and a positive K (≤n\le n≤n) which are 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 NNN lines follow, each describes a node in the format:

Address Data Next

The where Address is the position of the node, Data are 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 was 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

The following my method is an ordered array, in order to occupy too much time, the main rollover time is actually not much.
Time-to-space time complexity in n^2. Causes the run to time out. See other people's methods, cancel the sequence of steps, using an array to simulate memory, array ordinal represents the address. The following code is used, but it is not understood when there are redundant nodes that are not on the list.

#include <stdio.h> #include <stdlib.h> struct lnode{int Address;
    int Data;
int Next;

}lnode;
void Lread (int l,struct lnode *lhead);
void Lprint (struct lnode *lhead,int L);
void Lsort (struct lnode *lhead,int l,int address1st);
    int main () {int l,k,address1st;
    int t;
    T=l;
    scanf ("%d%d%d", &address1st,&l,&k);
    struct Lnode lhead[l];
    Lread (L,&lhead);
    Lsort (&lhead,l,address1st);
    Lprint (&lhead,l);
    System ("pause");
return 0;
    } void Lread (int l,struct lnode *lhead) {int t = L; while (t!=0) {scanf ("%d%d%d", &lhead[l-t]. ADDRESS,&AMP;LHEAD[L-T]. DATA,&AMP;LHEAD[L-T].
        Next);
    t--;
} return;
    } lsort (struct Lnode *lhead,int l,int address1st) {struct Lnode tmplist[l];
    int i,j; for (i=0;i<l;i++) {if (Lhead[i].
        address==address1st) {Tmplist[0]=lhead[i]; }} for (i=0;i<l;i++) {if (Tmplist[i].
        Next==-1) break; for (j=0;j<l;j++) {if (Lhead[j]. Address==tmplist[i].
                Next) {//printf ("tmplist[%d+1]=lhead[%d]\n", i,j);
                TMPLIST[I+1]=LHEAD[J];
            Break
    }}} for (j=0;j<l;j++) {lhead[j]=tmplist[j];
    }} void Lprint (struct lnode *lhead,int l) {int i = l; while (i!=0) {if (Lhead[l-1]. next<0) {printf ("%05d%d%d\n", Lhead[l-i]. Address,lhead[l-i]. Data,lhead[l-i].
        Next); }else{printf ("%05d%d%05d\n", Lhead[l-i]. Address,lhead[l-i]. Data,lhead[l-i].
        Next);
    } i--;
} return; }

Test Examples

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

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

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

After modification

#include <stdio.h> #include <stdlib.h> #define MAXN 100001 struct node{int Data;  
int Next;  
};
void Read (struct Node *space,int N);
void Lprint (struct Node *space,int address1st);


int Reverse (struct Node *space,int address1st,int k,int N); 
    int main () {int address1st, N, K; 
    struct Node SPACE[MAXN];
    printf ("&address1st, &n, &k\n");
    scanf ("%d%d%d", &address1st, &n, &k);
    Read (&space,n);
    Lprint (&space,address1st);
    Address1st=reverse (&space,address1st,k,n);
    Lprint (&space,address1st);
    System ("pause");  
return 0;
    } void Read (struct Node *space,int N) {int Address;
        while (n!=0) {scanf ("%d", &address); scanf ("%d%d", &space[address]. Data,&space[address].
        Next);
    n--;
    }//printf ("read end\n");
return;
    } void Lprint (struct Node *space,int address1st) {int Address = address1st; while (Space[address].
      Next!=-1) {  printf ("%05d%d%05d\n", address,space[address]. Data,space[address].
        Next); Address=space[address].
    Next; } printf ("%05d%d%d\n", address,space[address]. Data,space[address].

    Next);
Return
    } int Reverse (struct Node *space,int address1st,int k,int N) {//printf ("Reverse start\n");
    int address=address1st,tempaddress,celladdress;
    int n=n/k,i,j,x;
    int Arr[n*k];
    printf ("address1st:%d,k:%d,n:%d,n:%d\n", address1st,k,n,n);
            for (j=0;j<n*k;j++) {arr[j]=address; Address=space[address].
        Next;
        } for (i=0;i<n;i++) {//printf ("i:%d\n", I);
            for (j=0;j<k;j++) {////printf ("j:%d\n", j);
            x = i*k+j; if (j==0) {if (n-i==1) {space[arr[x]]. Next=space[arr[x+k-1]].
                Next; }else{Space[arr[x]].
                NEXT=ARR[X+2*K-1]; }}else{Space[arr[x]].

    NEXT=ARR[X-1];        }//printf ("%05d\t%d\t%d\n", Arr[x],space[arr[x]]. DATA,SPACE[ARR[X]].
            Next);
        System ("pause");
    }}//printf ("Reverse end\n");
    ADDRESS1ST=ARR[K-1];
return address1st;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.