日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1025 反转链表 (25 分)(c语言)

發布時間:2024/4/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1025 反转链表 (25 分)(c语言) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

給定一個常數?K?以及一個單鏈表?L,請編寫程序將?L?中每?K?個結點反轉。例如:給定?L?為 1→2→3→4→5→6,K?為 3,則輸出應該為 3→2→1→6→5→4;如果?K?為 4,則輸出應該為 4→3→2→1→5→6,即最后不到?K?個元素不反轉。

輸入格式:

每個輸入包含 1 個測試用例。每個測試用例第 1 行給出第 1 個結點的地址、結點總個數正整數?N?(≤105)、以及正整數?K?(≤N),即要求反轉的子鏈結點的個數。結點的地址是 5 位非負整數,NULL 地址用??1?表示。

接下來有?N?行,每行格式為:

Address Data Next

其中?Address?是結點地址,Data?是該結點保存的整數數據,Next?是下一結點的地址。

輸出格式:

對每個測試用例,順序輸出反轉后的鏈表,其上每個結點占一行,格式與輸入相同。

輸入樣例:

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

結尾無空行

輸出樣例:

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

結尾無空行

#include<stdio.h> #pragma warning(disable:4996) typedef struct a {int address;int data;int next; }lianbiao; lianbiao orgin[100000], sort[100000]; void nixu(lianbiao arr[], int begin, int end); int main() {int adr, N, K;scanf("%d%d%d", &adr, &N, &K);for (int cnt = 0; cnt < N; cnt++) {lianbiao temp;scanf("%d%d%d", &temp.address, &temp.data, &temp.next);orgin[temp.address] = temp;}for (int cnt = 0; cnt < N; cnt++) {sort[cnt] = orgin[adr];adr = sort[cnt].next;if (adr == -1) {N = cnt + 1;break;}}/*for (int cnt = 1; K * cnt < N; cnt++) {nixu(sort, K * (cnt - 1), K * cnt-1);}*/for (int cnt = 0; cnt < N / K; cnt++) {for (int cnt1 = 0; cnt1 < K / 2; cnt1++) {lianbiao temp = sort[K * cnt + cnt1];sort[K * cnt + cnt1] = sort[K * cnt + K - 1 - cnt1];sort[K * cnt + K - 1 - cnt1] = temp;}}//for (int i = 0; i < N / K; i++) {//反轉的次數 // for (int j = 0; j < K / 2; j++) {//反轉 // lianbiao temp;// temp = sort[j + i * K];// sort[j + i * K] = sort[K - 1 - j + i * K];//數組下標確認好 // sort[K - 1 - j + i * K] = temp;// }//}for (int cnt = 0; cnt < N; cnt++) {if (cnt + 1 < N) {sort[cnt].next = sort[cnt + 1].address;printf("%05d %d %05d\n", sort[cnt].address, sort[cnt].data, sort[cnt].next);}else {sort[cnt].next = -1;printf("%05d %d %-d", sort[cnt].address, sort[cnt].data, sort[cnt].next);}}return 0; } void nixu(lianbiao arr[], int begin, int end) {if (begin >= end) return;lianbiao temp = arr[begin];arr[begin] = arr[end];arr[end] = temp;nixu(arr, begin+1, end-1); }

我踩過的坑:

1.這道題逆序不能用遞歸,我也不知道為什么,我用我原來寫的遞歸函數進行逆序,通不過2,3測試點

2.循環過程中cnt<N/K和K*cnt<N是有區別的,例如當N=5,K=3.兩者的循環次數是不一樣的

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的1025 反转链表 (25 分)(c语言)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。