• 02-线性结构2 Reversing Linked List


    由于最近学的是线性结构,且因数组需开辟的空间太大。因此这里用的是纯链表实现的这个链表翻转。

    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 NN 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 Nextis 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

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct Node
      5 {
      6     int address;
      7     int data;
      8     int nextAddress;
      9     struct Node *next;
     10 }Node;
     11 typedef struct Node *LinkList;
     12 
     13 int main()
     14 {
     15     //排序前 
     16     LinkList L1, p1, q1;            
     17     L1 = (LinkList)malloc(sizeof(Node));    //创建头指针 
     18     L1->next = NULL;
     19     int firstAddress;
     20     int N, K;//N为总结点数 K为需翻转的数 
     21     scanf("%d %d %d", &firstAddress, &N, &K);
     22     p1 = L1;
     23     for(int i = 0; i < N; i++) {
     24         q1 =  (LinkList)malloc(sizeof(Node));
     25         scanf("%d %d %d",&q1->address, &q1->data, &q1->nextAddress);
     26         p1->next = q1;
     27         p1 = q1;
     28     }
     29     p1->next = NULL;
     30     
     31 //    //测试没问题 
     32 //    printf("测试1 :
    ");
     33 //    p1 = L1->next;
     34 //    while(p1){
     35 //        printf("%05d %d %d
    ", p1->address, p1->data, p1->nextAddress);
     36 //        p1 = p1->next;
     37 //    }
     38     
     39     //排序后 
     40     LinkList L2, p2;
     41     L2 = (LinkList)malloc(sizeof(Node));    //创建头指针 
     42     L2->next = NULL;
     43     int count = 0;
     44     int findAddress = firstAddress;
     45     p2 = L2;
     46     while(findAddress != -1) {            //while(count < N) {有多余结点不在链表上没通过 
     47     
     48         q1 = L1;
     49         while(q1->next) {
     50             if(q1->next->address == findAddress) {
     51                 p2->next = q1->next;
     52                 q1->next = q1->next->next;
     53                 p2 = p2->next;
     54                 count++;
     55 //                printf("count = %d
    ",count);
     56                 findAddress = p2->nextAddress;
     57 //                printf("findAddress = %d
    ",findAddress);
     58             }else {
     59                 q1 = q1->next;
     60             }
     61         }
     62     }
     63     p2->next = NULL;
     64     
     65 //    //测试没问题 
     66 //    printf("测试2 :
    ");
     67 //    p2 = L2->next;
     68 //    while(p2){
     69 //        printf("%05d %d %05d
    ", p2->address, p2->data, p2->nextAddress);
     70 //        p2 = p2->next;
     71 //    }
     72     //Reversing
     73     LinkList L3, p3, q3, tail;
     74     L3 = (LinkList)malloc(sizeof(Node));    //创建头指针 
     75     L3->next = NULL;
     76     //将L2以头插法插入L3
     77     int n = count;                //防止有多余结点影响 n=N 会影响
     78     int k = K;
     79     p3 = L3;
     80     p2 = L2;
     81     while(n >= k) {
     82         n -= k;
     83         for(int i = 0; i < k; i++) {
     84             p3->next = p2->next;
     85             p2->next = p2->next->next;
     86             if(i == 0)
     87                 tail = p3->next;
     88             else 
     89                 p3->next->next = q3;
     90             q3 = p3->next;
     91         }
     92         p3 = tail;
     93     }
     94     p3->next = L2->next;
     95     
     96     p3 = L3->next;
     97     while(p3->next) {
     98         printf("%05d %d %05d
    ",p3->address, p3->data, p3->next->address);//不到五位数用0补全 
     99         p3 = p3->next;
    100     }
    101     printf("%05d %d -1
    ",p3->address, p3->data);
    102     return 0;
    103 }

     ----------------------------------------陈越老师讲解----------------------------------

    单链表逆转模板代码

     1 LinkList Reverse (LinkList head, int K)    //单链表逆转K个结点 且仅一次 
     2 {
     3     int cnt = 1;        //用于计数已逆转的结点数 
     4     LinkList new_ = head->next;
     5     LinkList old = new_->next;
     6     LinkList tmp;        
     7     while(cnt < K) {     
     8         tmp = old->next;    //用于old结点逆转后 记录未逆转链表的头结点 
     9         old->next = new_;    //逆转 
    10         new_ = old;            //向后移位 
    11         old = tmp;            //向后移位 
    12         cnt++;                //逆转节点+1 
    13     }
    14     head->next->next = old;
    15     return new_;        //新的头结点 
    16 } 
  • 相关阅读:
    Git批量删除的方法
    第五课 森さんは 七時に 起きます
    第一课 李さんは  中国人です
    如何设置上传文件控件 input type="file" 的 默认值
    如何用程序提交一个上传文件的请求
    MVC Beta 做的网站实践总结(上)
    泛型单一模式
    提高web站点性能的最佳实践
    扩展GridView实现的一个自定义无刷新分页,排序,支持多种数据源的控件TwfGridView
    [导入]将字符串中连续的空格转换为一个空格
  • 原文地址:https://www.cnblogs.com/kuotian/p/5269434.html
Copyright © 2020-2023  润新知