Gyllenhaal718 2024-09-30 10:54 采纳率: 0%
浏览 2

02-线性结构3 Reversing Linked List 为啥我的狗屎代码不对?

正常输入题目给的例子也能输出和答案一样的output,那不给分是哪里有问题呢?

题目

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 (≤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 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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB

img


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node *PtrToNode;
struct Node{
    char vorads[5];
    int data;
    char nachads[5];
    PtrToNode Next;
};
typedef PtrToNode List;

List order(List L, char X[5]);
List sort(List L, int K);
void print(List L);

int main()
{
    List L, LL, r;
    PtrToNode Rear;
    int N, K;
    char X[5];
    L = (List)malloc(sizeof(struct Node));
    L->Next = NULL;
    Rear = L;
    scanf("%s", X);//起始地址 
    scanf("%d", &N);//Node个数 
    scanf("%d", &K);//RE节点 
    int i;          //读入并建立乱序表 
    for( i = 0; i < N; i++ ){
        r = (List)malloc(sizeof(struct Node));
        scanf("%s %d %s", (r->vorads), &(r->data), (r->nachads));    
        Rear->Next = r;
        Rear = r;     
    }
    Rear->Next = NULL;
    L = order(L, X);    //排正序 
    LL = sort(L, K);    //排RE序 
    print(LL);
     
    return 0;
} 

List order(List L, char X[5]) 
{
    List L1;
    L1 = (List)malloc(sizeof(struct Node));
    PtrToNode front, Rear, P, tmp;
    P = L1;
    for( Rear = L; Rear && strcmp((Rear->Next->vorads), X); Rear = Rear->Next );
    if(!Rear) return NULL;
    tmp = Rear->Next;
    Rear->Next = tmp->Next;
    P->Next = tmp;
    front = tmp;
    P = P->Next;

    while( strcmp((front->nachads), "-1") ){
        for( Rear = L; Rear && strcmp((Rear->Next->vorads), (front->nachads)); Rear = Rear->Next );
        if(!Rear) return NULL;
        tmp = Rear->Next;
        Rear->Next = tmp->Next;
        P->Next = tmp;
        front = tmp;
        P = P->Next;
    }
    P->Next = NULL;
    return L1->Next;
}

List sort(List L, int K)
{
    PtrToNode Rear, tag;
    List head = L;
    Rear = L;

    while( Rear->Next && (Rear->Next->data) <= K ){
        tag = Rear->Next;
        Rear->Next = tag->Next;
        tag->Next = head;
        strcpy(tag->nachads, head->vorads);
        head = tag; //更新head结点 
    }
    strcpy(Rear->nachads, Rear->Next->vorads);
    if( !(Rear->Next) ) return NULL;

    return head;
}

void print(List L)
{
    PtrToNode Rear;
    
    if( L ){
        for( Rear = L; Rear; Rear = Rear->Next ){
            printf("%s %d %s", Rear->vorads, Rear->data, Rear->nachads);
            if( Rear->Next ) printf("\n");
        }
    }
    else{
        printf("-1\n");
    }
    
    return;
}
  • 写回答

1条回答 默认 最新

  • 嵌入式小企鹅 2024-09-30 13:16
    关注

    对于你的order函数,你是想把它按连地址一样变成有序的。但是逻辑有问题。

    你换一种思路,来一个节点你就找位置“插入到那个位置”,就像链表的插入排序一样。
    对于你的sort,链表倒置就使用”断头头插法“,这样对你整体的编程会有一定的帮助。

    评论

报告相同问题?

问题事件

  • 创建了问题 今天

悬赏问题

  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?
  • ¥15 nasm x86 变量归零
  • ¥65 Tree 树形控件实现单选功能,可以使用element也可以手写一个,实现全选为全选状态
  • ¥60 寻抓云闪付tn组成网页付款链接
  • ¥16 寻字节跳动内部人员帮推简历