首页 文章

链接列表和指针

提问于
浏览
-1

我正在尝试将指针传递给main中链接列表的地址,将其传递给函数以向其分配内存并遍历到下一个节点,同时保持下一个节点的位置而不破坏头节点 .

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

typedef struct {
    struct node_list *head;
}list;

void insert_list(node_list **c, int num);

void main()
{
    int num;
    list *list_odd = (list*)calloc(1, sizeof(list));
    node_list *c = &list_odd->head;

    while (num != -1)
    {
        if (num % 2)
            insert_list(c, num);
    }
}

void insert_list(node_list **c, int num)
{
    if (*c == NULL)
    {
        *c = (node_list*)malloc(sizeof(node_list)); // it allocates the memory in the right place.
        (*c)->data = num;
        (*c) = (*c)->next; // but this step breaks the starting list pointer
    }
    else
    {
        (*c)->next = (node_list*)malloc(sizeof(node_list));
        (*c)->data = num;
        (*c) = (*c)->next;
    }
}

编辑:我可能没有解释自己,澄清:如果我的列表指向链表的开头,我分配内存然后做(* c)=(* c) - >接下来,我的头不再指向乞讨 . 我想要实现的是拥有列表的开头并保存下一个节点的位置 .

2 回答

  • 0

    我想建议一个双面的单链表 .

    这是一个示范计划 .

    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct node
    {
        int data;
        struct node *next;
    } node;
    
    typedef struct list
    {
        node *head;
        node *tail;
    } list;
    
    int push_back( list *lst, int data )
    {
        node *new_node = malloc( sizeof( node ) );
        int success = new_node != NULL;
    
        if ( success )
        {
            new_node->data = data;
            new_node->next = NULL;
    
            if ( lst->tail == NULL )
            {
                lst->tail = lst->head = new_node;
            }
            else
            {
                lst->tail = lst->tail->next = new_node;
            }            
        }
    
         return success;
     }
    
    void display( list *lst )
    {
        for ( node *current = lst->head; current != NULL; current = current->next )
        {
            printf( "%d ", current->data );
        }
        printf( "\n" );
    }
    
    int main( void )
    {
        list lst = { NULL, NULL };
    
        int data;
    
        while ( scanf( "%d", &data ) == 1 && data != -1 )
        {
            if ( data % 2 != 0 ) push_back( &lst, data );
        }
    
        display( &lst );
    
        return 0;
    }
    

    如果要输入这个数字序列

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

    然后输出将是

    1 3 5 7 9
    

    将新节点添加到列表末尾的复杂性是O(1) .

  • 0

    我想要实现的是拥有列表的开头并保存下一个节点的位置 .

    我不完全确定你要做什么,所以我做了一个等效的程序 . 如果您发现这种添加到列表的方式太慢,则需要将程序更改为保留{head,tail}对或将项添加到列表的前面 . 从你的文字听起来你正试图保持头部相同 - 所以{head,tail}对可能是最好的 .

    #include <stdlib.h>  //added
    #include <stdio.h>  //added
    #include <assert.h>
    
    typedef struct node_list_t {   //changed
        int data;
        struct node_list_t *next;   //changed
    } node_list;
    
    typedef struct list_t { //gave the struct a tag
        node_list *head;    //use the typedef name, not the struct name
    }list;
    
    void insert_list(list *my_list, int num);
    
    void main()
    {
        int num = 0; // initialised
        list my_list = {0}; // changed to be on stack. Could be calloc'd if you like
        node_list* printer;
    
        while (num != 50) //changed limit
        {
            if (num % 2)
            {
                // we're just passing in the list
                insert_list(&my_list, num);
            }
    
            num += 1;  //actually incrementing number. :)
        }
    
    
        for (printer = my_list.head;
                printer;
                    printer = printer->next)
        {
            printf("%d\n", printer->data);
        }
    }
    
    void insert_list(list *my_list, int num)
    {
        node_list *c = (node_list*) calloc(1, sizeof(node_list));
        c->data = num;
    
        assert(!c->next);
    
        if (!my_list->head)
        {
            // if the head is not initialised, then make C the head
            my_list->head = c;
        }
        else
        {
            // otherwise stick it on the end of the list.
            node_list *p;
            for (p = my_list->head;
                    p->next;
                        p = p->next)
            {
                //do nothing
            }
            p->next = c;
        }
    }
    

相关问题