02-Linear Structure 2 reversing Linked List

Source: Internet
Author: User

As a result of the recent study of linear structure, and the factor group need to open up too much space. So here's a list of pure linked lists that are implemented by flipping the list.

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 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 the one test case. The first line contains the address of the first node, a positive N (<= 10^5??) which of nodes, and a positive K (<= N) which is the length of the sublist-be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by-1.

then N-n lines follow, each describes a node in the format:

Address Data Next

The where Address is the position of the node, the Data was an integer, and nextis 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

1#include <stdio.h>2#include <stdlib.h>3 4typedefstructNode5 {6     intaddress;7     intdata;8     intnextaddress;9     structNode *Next;Ten }node; OnetypedefstructNode *linklist; A  - intMain () - { the     //before sorting - linklist L1, p1, Q1;  -L1 = (linklist)malloc(sizeof(Node));//To create a head pointer -L1->next =NULL; +     intfirstaddress; -     intN, K;//N is the number to be flipped for the sum of points K +scanf" %d%d%d", &firstaddress, &n, &K); AP1 =L1; at      for(inti =0; i < N; i++) { -Q1 = (linklist)malloc(sizeof(Node)); -scanf" %d%d%d", &q1->address, &q1->data, &q1->nextaddress); -P1->next =Q1; -P1 =Q1; -     } inP1->next =NULL; -      to //    //The test is fine. + //printf ("Test 1: \ n"); - //P1 = l1->next; the //While (p1) { * //printf ("%05d%d%d\n", p1->address, P1->data, p1->nextaddress); $ //P1 = p1->next;Panax Notoginseng //    } -      the     //after sorting + linklist L2, p2; AL2 = (linklist)malloc(sizeof(Node));//To create a head pointer theL2->next =NULL; +     intCount =0; -     intFindaddress =firstaddress; $P2 =L2; $      while(Findaddress! =-1) {//While  (Count < N) {There are redundant nodes that are not on the linked list.  -      -Q1 =L1; the          while(q1->next) { -             if(Q1->next->address = =findaddress) {WuyiP2->next = q1->Next; theQ1->next = q1->next->Next; -P2 = p2->Next; Wucount++; - //printf ("Count =%d\n", count); AboutFindaddress = p2->nextaddress; $ //printf ("findaddress =%d\n", findaddress); -}Else { -Q1 = q1->Next; -             } A         } +     } theP2->next =NULL; -      $ //    //The test is fine. the //printf ("Test 2: \ n"); the //P2 = l2->next; the //While (p2) { the //printf ("%05d%d%05d\n", p2->address, P2->data, p2->nextaddress); - //P2 = p2->next; in //    } the     //Reversing the linklist L3, p3, Q3, tail; AboutL3 = (linklist)malloc(sizeof(Node));//To create a head pointer theL3->next =NULL; the     //Insert the L2 into the L3 with the head interpolation method the     intn = count;//preventing unwanted nodes from affecting n=n will affect +     intK =K; -P3 =L3; theP2 =L2;Bayi      while(N >=k) { theN-=K; the          for(inti =0; I < K; i++) { -P3->next = p2->Next; -P2->next = p2->next->Next; the             if(i = =0) theTail = p3->Next; the             Else  theP3->next->next =Q3; -Q3 = p3->Next; the         } theP3 =tail; the     }94P3->next = l2->Next; the      theP3 = l3->Next; the      while(p3->next) {98printf"%05d%d%05d\n", P3->address, P3->data, p3->next->address);//less than five digits with 0 complement AboutP3 = p3->Next; -     }101printf"%05d%d-1\n", P3->address, p3->data);102     return 0;103}

02-Linear Structure 2 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.