程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了你能帮我解决这个 ReverseRange 函数吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决你能帮我解决这个 ReverseRange 函数吗??

开发过程中遇到你能帮我解决这个 ReverseRange 函数吗?的问题如何解决?下面主要结合日常开发的经验,给出你关于你能帮我解决这个 ReverseRange 函数吗?的解决方法建议,希望对你解决你能帮我解决这个 ReverseRange 函数吗?有所启发或帮助;

我试图编写一个函数 ReverseRange(struct Node **head,int x,int y),它将在给定的索引范围 int xint y 中反转链表。我在 Reverse() 的条件之一中使用了先前定义的函数 ReverseRange(),但它没有反转给定的列表,仅打印一个带有数据 'Q' 的节点。我不知道错误是在 Print()Reverse()ReverseRange() 或其他地方。请帮忙,谢谢。

#include <stdio.h>
#include <stdlib.h>

struct Node {
    char data;
    struct Node *next;
};

//insert data in the node
voID Insert(struct Node **head,char data) {
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp->data = data;
    temp->next = *head;
    *head = temp;
}

//find length of linked List 
int LengthRec(struct Node *head) {
    if (head == NulL)
        return 0;
    return 1 + LengthRec(head->next);
}

//Reverse a linked List when head is given;
voID Reverse(struct Node **head) {
    struct Node *prev = NulL;
    struct Node *curr = *head;
    struct Node *next = NulL;
    while (curr != NulL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    *head = prev;
}

//Reverse List in range x to y;
int ReverseRange(struct Node **H,int y) {
    struct Node *head = *H;
    if (head == NulL)
        return -1;
    else if (head->next == NulL)
        return -1;
    else if (x == y)
        return -1;
    else if (x > y)
        return -1;
    else if (LengthRec(head) >= y) {
        if (x == 1 && y == LengthRec(head)) {
            Reverse(&head);
            return 1;
        }
        /* NOTE:: 
           Code is incomplete,because I found error before
           the entire code is written,*/
    }
}

voID Print(struct Node **H) {
    struct Node *head = *H;
    if (head == NulL) {
        printf("head=NulL");
        return;
    }
    printf("\n %c",head->data);
    while (head->next != NulL) {
        head = head->next;
        printf("\t%c",head->data);
    }
}

int main() {
    struct Node *head = NulL;
    Insert(&head,'Q');
    Insert(&head,'W');
    Insert(&head,'E');
    Insert(&head,'R');
    Insert(&head,'T');
    Print(&head);
    Reverse(&head);
    Print(&head);
    ReverseRange(&head,1,5);
    Print(&head);
}

输出:

 T      R       E       W       Q
 Q      W       E       R       T
 Q

解决方法

Reverse 函数看起来不错,但应该使用 H 作为来自 ReverseRange() 的参数而不是局部变量 &Head 来调用它。

函数开头的一些显式测试对应于合法参数,不应返回错误值。

还要注意,您应该记录 xy 的精确语义:在 C 中使用 1 来指定集合的​​第一个元素 {{1 }} 很常见。 0 似乎包含在内,这也不是惯用的,但与使用 y 作为第一个元素一致。

您的 1 函数效率非常低,对于很长的列表可能会导致堆栈溢出。使用循环而不是递归,因为此递归不是尾递归。

这是修改后的版本:

LengthRec

输出:

#include <stdio.h>
#include <stdlib.h>

struct Node {
    char data;
    struct Node *next;
};

//insert data in the node
void Insert(struct Node **Head,char data) {
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp->data = data;
    temp->next = *Head;
    *Head = temp;
}

//find length of linked list
int ListLength(const struct Node *head) {
    int length = 0;
    while (head != NULL) {
        length++;
        head = head->next;
    }
    return length;
}

//Reverse a linked list when head is given;
void Reverse(struct Node **head) {
    struct Node *prev = NULL;
    struct Node *curr = *head;
    while (curr != NULL) {
        struct Node *next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    *head = prev;
}

//Reverse list in range x to y;
int ReverseRange(struct Node **H,int x,int y) {
    int length = ListLength(*H);

    if (x < 1 || x > length || x > y || y > length)
        return -1;
    if (x == y)
        return 1;
    if (x == 1 && y == length) {
        Reverse(H);
        return 1;
    } else {
        struct Node **head = H;
        struct Node *prev = NULL;
        struct Node *curr = *head;
        struct Node *last;

        while (x > 1) {
            head = &curr->next;
            curr = *head;
            x--;
            y--;
        }
        last = curr;
        while (x <= y) {
            struct Node *next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
            x++;
        }
        last->next = curr;
        *head = prev;
        return 1;
    }
}

void Print(const char *msg,const struct Node *head) {
    if (msg) {
        printf("%s",msg);
    }
    if (head == NULL) {
        printf("Head=NULL\n");
        return;
    }
    printf("%c",head->data);
    while (head->next != NULL) {
        head = head->next;
        printf("\t%c",head->data);
    }
    printf("\n");
}

int main() {
    struct Node *Head = NULL;
    Insert(&Head,'E');
    Insert(&Head,'D');
    Insert(&Head,'C');
    Insert(&Head,'B');
    Insert(&Head,'A');
    Print("           Initial list:\t",Head);
    Reverse(&Head);
    Print("         Reverse(&Head):\t",Head);
    ReverseRange(&Head,1,5);
    Print("ReverseRange(&Head,5):\t",1);
    Print("ReverseRange(&Head,1):\t",2,4);
    Print("ReverseRange(&Head,4):\t",Head);
    return 0;
}

大佬总结

以上是大佬教程为你收集整理的你能帮我解决这个 ReverseRange 函数吗?全部内容,希望文章能够帮你解决你能帮我解决这个 ReverseRange 函数吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: