【问题标题】:How to Reverse singly linked list properly in C? [duplicate]如何在C中正确反转单链表? [复制]
【发布时间】:2016-01-10 17:00:31
【问题描述】:

我从 C 开始,我被要求做一个在其节点数据中包含随机整数的链表,它们必须按升序排列,然后我必须用函数反转它们的顺序。我遇到的问题是,在我的反向输出中,我只得到第一个数字,甚至没有反转。

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

int N;

typedef struct node{
int num;
struct node *next;
}nodes;

int first_node(nodes *head){
if(head == NULL){
    printf("Error");
}
  else{
    head -> num= rand();
    head->next=NULL;
    }
}

int second_node(nodes *head){
nodes *uno=malloc(sizeof(nodes));
if(uno == NULL){
    printf("Error");

}

else{
    uno->num = rand();
    uno->next = NULL;
            if( uno->num>head->num){
               head->next=uno;

            }
            else{
                head= uno->next;
            }

}

}

int insert_node(nodes *head){
    nodes *dos=malloc(sizeof(nodes));
    if(dos == NULL){
        printf("Error");

    }

    else{
        dos->num = rand();
        dos->next = NULL;


        nodes *current = head;
        while(current!= NULL){
                if(current->num<dos->num && current->next==NULL){
                    current->next = dos;
                    return;

                }

                else if (current->num<dos->num && current->next->num>dos->num){
                dos->next=current->next;
                current->next=dos;
                return;

                }
                else if(head->num>dos->num){
                dos->next=head;
                head=dos;

        }
        current=current->next;

    }

}}

void printnodes(nodes *head){
    nodes *current = head;
    while (current != NULL){
        printf("%d\n",current->num);
        current = current ->next;
    }


}



void reverse(nodes *head)
{
 nodes *a=head->next;
 if(a!=NULL)
 {
   nodes *b=a->next;
   a->next=head;
   head->next=NULL;
   head=a;
   if(b!=NULL)
   {
     while(b!=NULL)
     {
      a=b;
      b=b->next;
      a->next=head;
      head=a;
     }
     a->next=head;
     head=a;
   }

   }
 }

int main(){
printf("Insert the number of nodes u want to create:");
scanf("%d", &N );
nodes *head =malloc(sizeof(nodes));
int i =3;

if(N==1){
    first_node(head);
}
else if (N ==2){
    first_node(head);
    second_node(head);

}
else if (N>2){

    first_node(head);
    second_node(head);
           while(i<=N){
            insert_node(head);
            i++;
           }
}


printnodes(head);
printf("\n\n Reversed \n\n");

reverse(head);
printnodes(head);


return 0;


}

创建 5 个节点的输出是: 为了: 41 3445 3890 8709 16777

反转: 41

我该如何解决这个问题?谢谢,抱歉英语不好

【问题讨论】:

  • 调试反转发现了什么?
  • 此代码无法编译。你能发布必要的代码来重现问题吗?

标签: c data-structures linked-list


【解决方案1】:

您的代码中有两个非常明显的问题:

第一个是您分配给一个局部变量,期望该分配在函数返回后反映。我说的是你在大多数功能中对head 的分配。这里的问题是,当您将参数传递给函数时。它是按值传递的,这意味着它是被复制的,而你在调用的函数中所拥有的只是一个副本或原始值。而且您应该知道,更改副本不会更改原件。

我快速浏览的第二个问题是在second_node 函数中,你首先要做的地方

uno->next = NULL;

并可能紧随其后

head= uno->next;

这会将NULL 分配给head

第一个问题很容易解决,通过模拟一种叫做引用传递的东西。我说的是模拟,因为 C 只能通过值传递。您可以在 C 中使用指针模拟通过引用传递,这意味着要在 C 中“通过引用”传递指针,您必须将指针传递给该指针,例如

int second_node(nodes **head){
    ...
}

然后你使用地址操作符调用它:

second_node(&head);

然后在second_node 中使用解引用运算符* 来访问“原始”head 指针,如

*head = uno->next;

在编写上述代码时,我注意到了一个第三个​​ 问题,那就是您将某些函数声明为返回int,但实际上您并没有从函数中返回任何内容。值得庆幸的是,您似乎没有在任何地方使用返回的值,但在应该返回值的函数中不返回值仍然是 未定义的行为。如果您不应该从函数返回值,则必须将其声明为返回 void

void second_node(nodes **head){...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 2012-10-26
    • 2012-09-08
    • 2012-01-30
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多