为什么无法实现inverseList函数?

huangapple go评论64阅读模式
英文:

Why can't inverseList function be implemented?

问题

链表通过堆栈实现反转,但在运行到inverseList时没有跳转。为什么无法实现inverseList函数?

英文:

The linked list realizes reversal through the stack, but does not jump when it runs to inverseList. Why can't inverseList function be implemented?

#include <cstdio>
#include <cstdlib>

typedef struct lnode{
    int data;
    struct lnode *next;
}lnode,*Linklist;

Linklist createList(int n){
    Linklist L,p,q;
    int i;
    L = (lnode*)malloc(sizeof(lnode));
    if(!L){
        return 0;
    }
    L->next = NULL;
    q = L;
    for(int i = 0; i < n; i++){
        p = (lnode*) malloc(sizeof(lnode));
        printf("enter:\n");
        scanf("%d",&p->data);
        p->next = NULL;
        q->next = p;
        q = p;
    }
    return L;
}

Linklist inverseList(Linklist h){
    Linklist res;
    int arr[10];
    int i=0;
    while(h->next != NULL){
        arr[i] = h->data;
        i++;
        h = h->next;
    }

    if(-1 == i){
        res->data = arr[i];
        i--;
        res = res->next;
    }
    return res;
}

void printList(Linklist h){
    Linklist p = h->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p = p->next;
    }
}

int main(){
    Linklist pHead = NULL;
    int n;

    printf("enter the number of value:\n");
    scanf("%d",&n);
    pHead = createList(n);
    printf("value in the list:\n");
    printList(pHead);
    printf("\n----------\n");
    inverseList(pHead);
    printList(pHead);
    return 0;
}

The linked list realizes reversal through the stack, but does not jump when it runs to inverseList. Why can't inverseList function be implemented?

答案1

得分: 1

你的列表可以被视为一个堆栈。

要创建一个新的列表,它是现有列表的反转版本,然后迭代原始列表,并将所有值添加到新列表的头部。新列表现在是第一个原始列表的确切反转版本。

如果你应该反转原始列表,那么请按照上述步骤进行,然后删除原始列表中的所有节点,并交换原始列表和新列表的头部。

英文:

Your list can be treated as a stack.

To create a new list which is the reverse of an existing list, then iterate over the original list and add all values to the head of the new list. The new list is now the exact reverse of the first original list.

If you should reverse the original list, then do as above, then delete all nodes from the original list, and exchange the heads of the original and new list.

答案2

得分: 0

看起来您正在将程序编译为C++程序。这些头文件

#include <cstdio>
#include <cstdlib>

是C++头文件。

在C中,您需要写成

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

但无论如何,您的代码完全错误。

例如,函数createList创建了一个具有未初始化数据成员data的虚拟首节点。因此,很难确定列表何时为空:当头节点指针等于NULL时,还是当列表只有一个虚拟节点时。而且在函数inverseList中,在此while循环中使用未初始化的数据成员next

while (h->next != NULL) {
    arr[i] = h->data;
    //...

该列表不应该有虚拟节点。

函数inverseList毫无意义。例如,为什么在函数内创建一个具有魔术元素数量的数组。或者例如在if语句中满足以下条件时:

if (-1 == i) {

又为什么在函数内使用未初始化的指针res

Linklist res;
//...
res->data = arr[i];

如果列表不包含虚拟节点(而且不应该包含),那么函数inverseList可以非常简单。例如:

Linklist inverseList(Linklist head) {
    Linklist new_head = NULL;
    
    while (head != NULL) {
        current = head;
        head = head->next;
        current->next = new_head;
        new_head = current;
    }
    
    return new_head;
}

在主函数中,您需要写成:

pHead = inverseList(pHead);
英文:

It seems you are compiling your program as a C++ program. These headers

#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;

are C++ headers.

In C you need to write

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

But in any case your code is entirely wrong.

For example the function createList creates a dummy first node with uninitialized data member data. So for example it is unclear when the list is empty: whether when the pointer to the head node is equal to NULL or when the list has a single dummy node. And moreover the uninitialized data member next is used within the function inverseList in this while loop

while(h-&gt;next != NULL){
    arr[i] = h-&gt;data;
    //...

The list shall not have a dummy node.

The function inverseList does not make sense. For example why within the function there is created an array with a magic number of elements. Or for example when this condition in the if statement

if(-1 == i){

is satisfied. Or why within the function there is used the uninitialized pointer res.

Linklist res;
//...
res-&gt;data = arr[i];

If the list does not contain a dummy node (and it shall not contain it) then the function inverseList can look very simply. For example

Linklist inverseList( Linklist head )
{
    Linklist new_head = NULL;

    while ( head != NULL )
    {
        current = head;
        head = head-&gt;next;
        current-&gt;next = new_head;
        new_head = current;
    }

    return new_head;
}

and in main you need to write

pHead = inverseList(pHead);

huangapple
  • 本文由 发表于 2023年3月7日 18:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定