02-Linear Structure 3 reversing Linked List

Source: Internet
Author: User

Given a constantK and a singly linked listL, you is supposed to reverse the links of everyKelements onL. 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 < Span class= "Mord mathit" >k=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains the one test case. The first line contains the address of the first node, a positive N (≤) which I s the total number of nodes, and a positive K (≤) 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 N lines Follow, each describes a node in the format:

Address Data Next

Where Address is the position of the node, was an Data integer, and is the position of the Next 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 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218
Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1

1#include <stdio.h>2#include <stdlib.h>3 4typedefstructLnode *Ptrtolnode;5 typedef ptrtolnode Position;6 structlnode{7     intaddress;8     intdata;9     intNext;Ten position rear; One }; A typedef ptrtolnode List; -  - //read-in function the voidReadList (List L,intN) { -Position P =L; -      while(n--){ -List temp = (list)malloc(sizeof(structlnode)); +Temp->rear =NULL; -scanf"%d%d%d", &temp->address, &temp->data, &temp->next); +P->rear =temp; Ap = p->Rear; at     } - } -  - //sort function - intSortlist (list L, List L2,intFirst ) { -Position Now1 = L, Now2 =L2, temp; in     intFindnode = First, cnt=0;//Findnode records the next node address to look for, CNT records the number of nodes in the list -      while(findnode!=-1){ to          while(now1->rear) {//Traverse l to find the same address as the Findnode node +             if(Now1->rear->address==findnode) {//I found it. Remove it from L and insert it into the L2 -temp = now1->Rear; theNow1->rear = now1->rear->Rear;  *Temp->rear =NULL; $Now2->rear =temp;Panax NotoginsengNow2 = now2->Rear; -Findnode = temp->Next; thecnt++; +                  Break;  A             } theNow1 = now1->Rear; +         } -Now1 = L;//update Now1, every search to the beginning of the first traverse L $     } $     returnCNT; - } -  the //reversing linked list functions - voidReverselist (List L2,intCntintK) {Wuyi     //Old , New_ record two node locations for reversal, p1, P2 record two node locations to be connected to the reversed linked list thePosition old = l2->rear->rear, new_ = l2->rear, temp, p1 = L2, p2 = l2->Rear; -     if(k==1|| cnt==1)return;//K or chain list length cnt equals 1, no reversal required, direct return Wu      while(cnt>=K) { -CNT-=K; About          for(intCount=1; count<k; count++){ $temp = old->rear;//Temp Record the head node of the list that is not reversed -Old->rear = New_;//reversal -Old->next = new_->address; -New_ = old;//shift Backwards AOld = temp;//shift Backwards +         } theP1->rear =New_; -P1->next = new_->address; $P2->rear =Old ; the         if(old) P2->next = old->address;  the         Else  Break; theP1 =P2; theP2 = p2->Rear; -New_ =Old ; inOld = old->Rear; the     } the } About  the //output Function the voidprint (List L) { thePosition now = l->Rear; +      while(now) { -         if(now->rear==null) printf ("%05d%d-1\n", Now->address, now->data);  the         Elseprintf"%05d%d%05d\n", Now->address, Now->data, now->next);Bayinow = now->Rear; the     } the } -  - //Main program Framework the intMain () { the List L1, L2; theL1 = (List)malloc(sizeof(structlnode)); theL1->next =NULL; -L2 = (List)malloc(sizeof(structlnode)); theL2->rear =NULL; the     intFirst, N, K, CNT;//because there may be redundant nodes that are not on the list, the number of nodes that are recorded on the list with CNT thescanf"%d%d%d", &first, &n, &k);94 readlist (L1, N); theCNT =sortlist (L1, L2, first); the reverselist (L2, CNT, k); the print (L2);98     return 0; About}

02-Linear Structure 3 reversing Linked List

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.