Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:反转链接列表的问题_C - Fatal编程技术网

C:反转链接列表的问题

C:反转链接列表的问题,c,C,我正在编写一个程序来创建一个链表(节点),然后将其反转。链接列表包含数据和下一个链接的地址 typedef struct node{ int data; struct node *next; }node; 首先,我创建了链表 struct node *Insert_value(int dataInput,node* head) { node *new_node=NULL; new_node = malloc(sizeof(node)); new_node

我正在编写一个程序来创建一个链表(节点),然后将其反转。链接列表包含数据和下一个链接的地址

typedef struct node{
    int data;
    struct node *next;
}node;
首先,我创建了链表

struct node *Insert_value(int dataInput,node* head)
{
    node *new_node=NULL;
    new_node = malloc(sizeof(node));
    new_node -> next = head;
    new_node -> data = dataInput;
    head = new_node;
    return head;
}
struct node* Reversing(node **head)
{
    node *current, *previous, *first;
    current = previous = first = *head;

    first = first->next->next;
    current = current->next;
    previous ->next = NULL;
    current->next = previous;

    while(first != NULL)
    {
        previous = current;
        current = first;
        first = first -> next;
        previous->next = current;
    }

    return current;
}
之后,我创建了一个函数来打印这些数据。(我称之为PrintNode)

最后,创建一个函数来反转链表

struct node *Insert_value(int dataInput,node* head)
{
    node *new_node=NULL;
    new_node = malloc(sizeof(node));
    new_node -> next = head;
    new_node -> data = dataInput;
    head = new_node;
    return head;
}
struct node* Reversing(node **head)
{
    node *current, *previous, *first;
    current = previous = first = *head;

    first = first->next->next;
    current = current->next;
    previous ->next = NULL;
    current->next = previous;

    while(first != NULL)
    {
        previous = current;
        current = first;
        first = first -> next;
        previous->next = current;
    }

    return current;
}
这是我的全部计划

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

typedef struct node{
    int data;
    struct node *next;
}node;

struct node *Insert_value(int dataInput,node* head);
struct node * Reversing(node **head);
void PrintNode(node *head);

main()
{
    node *head = NULL;
    int i=0,dataInput;
    while(i!=5)
    {
        printf("input your elements: ");
        scanf("%d",&dataInput);
        head = Insert_value(dataInput,head);
        i++;
    }
    PrintNode(head);
    head = Reversing(&head); 
    PrintNode(head);


}               

struct node *Insert_value(int dataInput,node* head)
{
    node *new_node=NULL;
    new_node = malloc(sizeof(node));
    new_node -> next = head;
    new_node -> data = dataInput;
    head = new_node;
    return head;
}

struct node* Reversing(node **head)
{
    node *current, *previous, *first;
    current = previous = first = *head;

    first = first->next->next;
    current = current->next;
    previous ->next = NULL;
    current->next = previous;

    while(first != NULL)
    {
        previous = current;
        current = first;
        first = first -> next;
        previous->next = current;
    }

    return current;
}

void PrintNode(node* head)
{
    while(head!= NULL)
        {
            printf("%d\t",head->data);
            head= head->next;
        }
        printf("\n");
}
#包括
#包括
类型定义结构节点{
int数据;
结构节点*下一步;
}节点;
结构节点*插入_值(int数据输入,节点*头);
结构节点*反向(节点**头部);
无效打印节点(节点*头部);
main()
{
node*head=NULL;
int i=0,数据输入;
而(i!=5)
{
printf(“输入您的元素:”);
scanf(“%d”和数据输入);
head=插入_值(数据输入,head);
i++;
}
打印节点(头);
压头=反向(压头和压头);
打印节点(头);
}               
结构节点*插入值(int数据输入,节点*头)
{
node*new_node=NULL;
新节点=malloc(sizeof(node));
新建节点->下一步=头部;
新建_节点->数据=数据输入;
head=新的_节点;
回流头;
}
结构节点*反向(节点**头部)
{
节点*当前,*上一个,*第一个;
当前=上一个=第一个=*头部;
第一个=第一个->下一个->下一个;
当前=当前->下一步;
上一个->下一个=空;
当前->下一步=上一步;
while(第一个!=NULL)
{
先前=当前;
电流=第一;
第一个=第一个->下一个;
上一个->下一个=当前;
}
回流;
}
无效打印节点(节点*头)
{
while(head!=NULL)
{
printf(“%d\t”,头部->数据);
头部=头部->下一步;
}
printf(“\n”);
}

经过多次调试,我知道这些函数都很好。但是,在反向函数之后,
head
变量的下一个节点的地址为空。您能解释一下并给我一些建议吗?

解决您问题的一行更改是(您认为它有点错误)

代替

previous->next = current;

您的代码将为单元素链表放大。在函数
Reversing()
中添加相应的检查。如果存在单个元素,
first->next
将为
NULL
。但是您编写了
first->next->next
,如果
first->next
NULL
,那么这将是未定义的行为



在前面的例子中,您只是在
Reversing()
中创建了一个链接列表,链接保持不变,但是
指向最后一个节点。因此,它的
下一个
NULL
修改
反转
,以便在末尾追加新节点。浏览列表时,需要提前保存下一个节点(
node*next=current->next


别忘了
释放你的列表,例如
while(head!=NULL){node*受害者=head;head=head->next;释放(受害者);}
struct node* Reversing(node **head)
{
    node *current = *head;
    node *reverse = NULL;

    while(current)
    {
        node *next = current->next;

        if(!reverse)
        {
            reverse = current;
            reverse->next = NULL;
        }
        else
        {
            current->next = reverse;
        }
        reverse = current;

        current = next;
    }

    return reverse;
}