修复反向链接 [英] fix for a reversed link

查看:69
本文介绍了修复反向链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,由于某种原因,例如,如果我输入的是2-> 4-> 6,则我的链表将以反序显示. 我的输出是6-> 4-> 2

hey for some reason my linked list is printing in the reversed ordear for example if my input is 2->4->6 my output is 6->4->2

list* add_int_list(list* a,int b)
{
    list *temp;
    temp = (list*)malloc(sizeof(list*));
    temp->next = NULL;
    if (a->next == NULL)//insert to the first node
    {
        temp->data = b;
        temp->next = a;
        a = temp;

    }
    else 
    {
        temp->data = b;
        temp->next = a;
        a = temp;//I think the problem is here, couldnt find how to fix 
}

推荐答案

此语句中的初学者

temp = (list*)malloc(sizeof(list*));
                            ^^^^^

分配的内存大小等于指针的大小,而不是节点的大小.您必须要么写

there is allocated memory of the size equal to the size of pointer instead of the size of the node. You have to write either

temp = (list*)malloc(sizeof(list));

temp = (list*)malloc(sizeof( *temp));

此if语句

if (a->next == NULL)

可以调用未定义的行为,因为最初列表可以为空.因此,指针a可以等于NULL.那就是使用空指针访问内存.

can invoke undefined behavior because initially the list can be empty. So the pointer a can be equal to NULL. That is there is used a null pointer to access memory.

if-else语句的if和else部分之后,这两个代码块之间没有区别

There is no difference between these two code blocks after the if and else parts of the if-else statement

if (a->next == NULL)//insert to the first node
{
    temp->data = b;
    temp->next = a;
    a = temp;
}
else 
{
    temp->data = b;
    temp->next = a;
    a = temp;//
}

这是两个代码片段,都尝试在列表的开头插入一个新节点.

That is the both code snippet try insert a new-node in the beginning of the list.

将新节点插入到单链单边列表的开始是一种通用方法.将节点附加到此类列表的末尾效率不高,因为必须遍历整个列表.

It is a general approach to insert a new node in a singly-linked one-sided list in its beginning. To append a node to such a list to its end is inefficient because the whole list must be traversed.

如果要将节点附加到单链列表的末尾,则将其设为双面.

If you want to append a node to the end of a singly linked list then make it two-sided.

这是一个演示程序.

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

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

typedef struct List
{
    Node *head;
    Node *tail;
} List;

int push_front( List *list, int data )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = list->head;

        list->head = new_node;

        if ( list->tail == NULL ) list->tail = list->head;
    }

    return success;
}

int push_back( List *list, int data )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = NULL;

        if ( list->tail == NULL )
        {
            list->head = list->tail = new_node;
        }
        else
        {
            list->tail = list->tail->next = new_node;
        }
    }   

    return success;
}

void output( const List *list )
{
    for ( const Node *current = list->head; current != NULL; current = current->next )
    {
        printf( "%d -> ", current->data );
    }

    puts( "null" );
}

int main(void) 
{
    List list = { .head = NULL, .tail = NULL };

    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        if ( i % 2 != 0 )
        {
            push_front( &list, i );
        }
        else
        {
            push_back( &list, i );
        }

        output( &list );
    }

    return 0;
}

其输出为

0 -> null
1 -> 0 -> null
1 -> 0 -> 2 -> null
3 -> 1 -> 0 -> 2 -> null
3 -> 1 -> 0 -> 2 -> 4 -> null
5 -> 3 -> 1 -> 0 -> 2 -> 4 -> null
5 -> 3 -> 1 -> 0 -> 2 -> 4 -> 6 -> null
7 -> 5 -> 3 -> 1 -> 0 -> 2 -> 4 -> 6 -> null
7 -> 5 -> 3 -> 1 -> 0 -> 2 -> 4 -> 6 -> 8 -> null
9 -> 7 -> 5 -> 3 -> 1 -> 0 -> 2 -> 4 -> 6 -> 8 -> null

在此演示程序中,使用功能push_back将偶数插入列表的末尾,使用功能push_front将奇数插入列表的开头.

In this demonstrative program even numbers are inserted in the end of the list using the function push_back and odd numbers are inserted in the beginning of the list using the function push_front.

如果您的C编译器不支持指定的初始化程序,则此声明

If you C compiler does not support designated initializers then this declaration

List list = { .head = NULL, .tail = NULL };

可以通过以下方式更改

List list = { NULL, NULL };

这篇关于修复反向链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆