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?(≤) which is the total number of nodes, and a positive?K?(≤) 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
#include<cstdio>
#include<algorithm>
using namespace std;const int maxn = 100010;
struct Node{int address,data,next;int flag;
}node[maxn];bool cmp(Node a,Node b){return a.flag < b.flag;
}//void print(int i,int k,int n){
//    int j;
//    for(j = i + k - 1; j >= i; j--){
//        printf("%05d %d ",node[j].address,node[j].data);
//        if(j > i){
//            printf("%05d\n",node[j-1].address);
//        }else{
//            if(i+2*k<=n){
//                printf("%05d\n",node[i+2*k].address);
//            }
//        }
//    }
//}int main(){for(int i = 0 ; i < maxn; i++){node[i].flag = maxn;}int n,k,begin;scanf("%d%d%d",&begin,&n,&k);int address;for(int i = 0; i < n; i++){scanf("%d",&address);scanf("%d%d",&node[address].data,&node[address].next);node[address].address = address;//node[address].flag = maxn;
    }int count = 0,p = begin;while(p!=-1){node[p].flag = count++;p = node[p].next;}sort(node,node+maxn,cmp);    n = count;    for(int i = 0; i < n/k; i++){for(int j = (i+1)*k-1; j > i*k; j--){printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);}printf("%05d %d ",node[i*k].address,node[i*k].data);if(i < n/k-1) printf("%05d\n",node[(i+2)*k-1].address);else{if(n%k==0) printf("-1\n");else{printf("%05d\n",node[(i+1)*k].address);for(i = n/k*k; i < n; i++){printf("%05d %d ",node[i].address,node[i].data);if(i < n-1) printf("%05d\n",node[i+1].address);else printf("-1\n");}}}}//    if(n == 1){
//        printf("%05d %d -1\n",node[0].address,node[0].data);
//        return 0;
//    }
//    bool flag = true;
//    if(count%k!=0){
//        while(count%k!=0) count--;
//        flag = false;
//    }
//    for(int i = 0; i < count; i += k){
//        print(i,k,count);
//    }
//    
//    if(flag == true){
//        printf("-1\n");
//    }else{
//        printf("%05d\n",node[count].address);
//        for(int i = count; i < n; i++){
//            printf("%05d %d ",node[i].address,node[i].data);
//            if(i < n - 1) printf("%05d\n",node[i+1].address);
//            else printf("-1\n");
//        }
//    }
//    
//    for(int i = 0; i < n; i++){
//        printf("%05d %d\n",node[i].address,node[i].data);
//    }return 0;
}

?

轉載于:https://www.cnblogs.com/wanghao-boke/p/10650794.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385084.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385084.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385084.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

03-樹1 樹的同構 (25 分)

給定兩棵樹T1和T2。如果T1可以通過若干次左右孩子互換就變成T2&#xff0c;則我們稱兩棵樹是“同構”的。例如圖1給出的兩棵樹就是同構的&#xff0c;因為我們把其中一棵樹的結點A、B、G的左右孩子互換后&#xff0c;就得到另外一棵樹。而圖2就不是同構的。 圖1 圖2 現給定兩棵…

【樹】104. 二叉樹的最大深度

題目 104. 二叉樹的最大深度 方法1 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr),…

Leetcode 206. 反轉鏈表

206. 反轉鏈表 解法1 class Solution { public:ListNode *reverseList(ListNode *head){if(!head || !head->next)return head;ListNode *p;p reverseList(head->next);head->next->next head;head->next nullptr;return p;} };解法2 /*** Definition for…

05-樹7 堆中的路徑 (25 分)

將一系列給定數字插入一個初始為空的小頂堆H[]。隨后對任意給定的下標i&#xff0c;打印從H[i]到根結點的路徑。 輸入格式: 每組測試第1行包含2個正整數N和M(≤)&#xff0c;分別是插入元素的個數、以及需要打印的路徑條數。下一行給出區間[-10000, 10000]內的N個要被插入一個初…

Leetcode 124.二叉樹中的最大路徑

解法1 解法 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* …

《UNIX環境高級編程》目錄

第一章&#xff1a;UNIX標準及實現 01 函數perror、strerror 第三章&#xff1a;文件I/O 01 C庫函數 02 文件描述符、函數open和openat 03 函數read、write、lseek 04 函數dup和dup2 第四章&#xff1a;文件和目錄 01 函數stat、fstat、fstatat和lstat 02 函數umask 03 函…

06-圖1 列出連通集 (25 分)

給定一個有N個頂點和E條邊的無向圖&#xff0c;請用DFS和BFS分別列出其所有的連通集。假設頂點從0到N?1編號。進行搜索時&#xff0c;假設我們總是從編號最小的頂點出發&#xff0c;按編號遞增的順序訪問鄰接點。 輸入格式: 輸入第1行給出2個整數N(0)和E&#xff0c;分別是圖的…

牛客網算法題題解

序號題目語言標記1 C解題報告 2 3 4字符串歸一化C解題報告

06-圖3 六度空間 (30 分)

“六度空間”理論又稱作“六度分隔&#xff08;Six Degrees of Separation&#xff09;”理論。這個理論可以通俗地闡述為&#xff1a;“你和任何一個陌生人之間所間隔的人不會超過六個&#xff0c;也就是說&#xff0c;最多通過五個人你就能夠認識任何一個陌生人。”如圖1所示…

Linux網絡編程目錄

UNIX網絡編程目錄 1. TCP三次握手的第三次的 ack包丟失會怎樣&#xff1f; 2. inux網絡編程“驚群”問題總結

Linux高性能服務器編程

一、文件IO、標準IO 1. 2. 函數dup和dup2 三、進程 1. fork、vfork、clone 2. 函數wait、waitpid、孤兒進程、僵尸進程 3. 進程組 4. 會話 四、信號 1. 函數signal、sigaction 2. 函信號SIGCHLD 3. 函數kill、raise、abort、alarm 4. 信號集、sigprocmask、sigpending 五、…

07-圖4 哈利·波特的考試 (25 分)

哈利波特要考試了&#xff0c;他需要你的幫助。這門課學的是用魔咒將一種動物變成另一種動物的本事。例如將貓變成老鼠的魔咒是haha&#xff0c;將老鼠變成魚的魔咒是hehe等等。反方向變化的魔咒就是簡單地將原來的魔咒倒過來念&#xff0c;例如ahah可以將老鼠變成貓。另外&…

C++ Primer (二)目錄

第十五章&#xff1a;面向對象程序設計 1. 虛函數/2. 虛函數表剖析&#xff08;一&#xff09;3. 虛函數表剖析&#xff08;二&#xff09;4. 虛函數表剖析&#xff08;三&#xff09;

07-圖6 旅游規劃 (25 分)

有了一張自駕旅游路線圖&#xff0c;你會知道城市間的高速公路長度、以及該公路要收取的過路費。現在需要你寫一個程序&#xff0c;幫助前來咨詢的游客找一條出發地和目的地之間的最短路徑。如果有若干條路徑都是最短的&#xff0c;那么需要輸出最便宜的一條路徑。 輸入格式: 輸…

08-圖7 公路村村通 (30 分)

現有村落間道路的統計數據表中&#xff0c;列出了有可能建設成標準公路的若干條道路的成本&#xff0c;求使每個村落都有公路連通所需要的最低成本。 輸入格式: 輸入數據包括城鎮數目正整數N&#xff08;≤&#xff09;和候選道路數目M&#xff08;≤&#xff09;&#xff1b;隨…

《回溯算法》目錄

序號題目標記1 46. 全排列 2 47. 全排列 II 3 39. 組合總和 4 40. 組合總和 II 5 6

08-圖9 關鍵活動 (30 分)

假定一個工程項目由一組子任務構成&#xff0c;子任務之間有的可以并行執行&#xff0c;有的必須在完成了其它一些子任務后才能執行。“任務調度”包括一組子任務、以及每個子任務可以執行所依賴的子任務集。 比如完成一個專業的所有課程學習和畢業設計可以看成一個本科生要完成…

C++ Primer

C Primer目錄索引 虛函數表剖析&#xff08;一&#xff09;虛函數表剖析&#xff08;二&#xff09;static關鍵字用法 volatile、const的用法???????

10-排序4 統計工齡 (20 分)

給定公司N名員工的工齡&#xff0c;要求按工齡增序輸出每個工齡段有多少員工。 輸入格式: 輸入首先給出正整數N&#xff08;≤&#xff09;&#xff0c;即員工總人數&#xff1b;隨后給出N個整數&#xff0c;即每個員工的工齡&#xff0c;范圍在[0, 50]。 輸出格式: 按工齡的遞…

10-排序6 Sort with Swap(0, i) (25 分)

Given any permutation of the numbers {0, 1, 2,..., N?1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the follow…