【问题标题】:wrong output in singly linked list in cc中单链表中的错误输出
【发布时间】:2016-05-12 05:48:01
【问题描述】:

我需要帮助。我试图创建一个单链表。到目前为止一切都很好,但我得到了错误的输出。我不知道我的错误。谢谢!

我的输出是:

 create a single linked list
 list:
 aaaaa
 -1342177280 

我的代码是:

struct node{

    int key;
    int val; 
    struct node *next;  

};

struct node *create_single_link(){

    struct node *head = malloc(sizeof(struct node));

    head->next = NULL;

    return(head);

}


void insertVal( struct node *lst, int v){

    struct node *current = malloc(sizeof(struct node));
    current -> val = v;

    current->next = lst;
    lst = current;

}

void print_list(struct node * head){


    struct node *ptr = head;

    printf("list:\n");

    while(ptr != NULL ){
        printf("aaaaa\n");
        printf("%d \n ",ptr ->val );
        ptr = ptr->next;
    }


}

int  main(int argc, char const *argv[])
{

    struct node *list;
    list = create_single_link();
    if(list != NULL){
        printf("create a single linked list\n");
    }
    else
        printf("failed to create a single linked list\n");

    insertVal(list,2222);
    //insertVal(list,2);
    print_list(list);


    return 0;
}

【问题讨论】:

标签: c struct


【解决方案1】:

问题 1

你有:

void insertVal( struct node *lst, int v){

    struct node *current = malloc(sizeof(struct node));
    current -> val = v;

    current->next = lst;
    lst = current;
}

这不会改变mainlist 的值。它只会改变lst 在函数中本地指向的位置。

我建议把函数改成:

struct node* insertVal( struct node *lst, int v){

    struct node *current = malloc(sizeof(struct node));
    current -> val = v;

    current->next = lst;
    return current;
}

并将其用作:

list = insertVal(list, 2222);

问题 2

create_single_link 中创建的节点的值未初始化。你最好不要使用它。您可以将main 更改为:

int  main(int argc, char const *argv[])
{
   struct node *list = NULL;
   list = insertVal(list,2222);
   list = insertVal(list,2);
   print_list(list);

   return 0;
}

【讨论】:

    【解决方案2】:

    insertVal 修改 lst 但不返回它。

    这意味着只能添加一项​​。

    你正在打印出你初始化的头部v

    【讨论】:

      【解决方案3】:
      1. 列表应该有它的头,它指向列表的下一个元素。您做了相反的事情:列表的其余部分(第一个数据片段)指向头部(insertVal()
      2. 头部通常已经存储了第一条数据;您只能将 head 用作指向列表其余部分的指针,而不是用于存储数据。
      3. 您观察到错误行为的原因是,在print_list() 中,您假设与我上面的项目 1. 和 2. 相反:您尝试打印出与头部相关联的 val(这与项目相反) 2),并且您假设剩余的列表元素跟随 head (这与上面的第 1 项相反)。 我建议保留 print_list() 原样,但更正前两项。
      4. 顺便说一句,您可以考虑更改问题的措辞,因为您的问题不在于“错误的输出”,而在于 C 中的列表实现。有关示例实现,请参阅 http://www.cprogramming.com/tutorial/c/lesson15.html
      5. 我现在跳过你的struct node 有两个字段(keyval)但你的insertVal() 只填充vall。虽然本身不​​是错误,但迟早您会忘记为 key 分配一个有意义的值是很危险的。
        快乐的编码:-)

      【讨论】:

        【解决方案4】:

        其实你的函数插入值没有任何意义,如果你的意思是在链表的末尾插入一个值,希望下面的代码可以帮助到你。

        #include <stdio.h>
        #include <stdlib.h>
        struct node{
            int key;
            int val;
        struct node *next;
        };
        
        struct node *create_single_link() {
        struct node *head = (struct node*)malloc(sizeof(struct node));
        head->next = NULL;
        return (head);
        }
        
        void insertVal (struct node *lst, int v) {
        while(lst->next != NULL) {
            lst = lst->next;
        }
        lst->next = (struct node*)malloc(sizeof(struct node));
        lst->next->val = v;
        lst->next->next = NULL;
        }
        
        void print_list(struct node *ptr) {
        while(ptr != NULL) {
            printf("the key is %d and the value is %d\n", ptr->key, ptr->val);
            ptr = ptr->next;
        }
        }
        
        
        void main(void) {
        
        struct node *list;
        list = create_single_link();
        if(list != NULL) 
            printf("a single list has been created\n");
        else
            printf("faied to create a single list\n");
        list->val = 1111;
        insertVal(list, 2222);
        insertVal(list, 3333);
        print_list(list);
        }
        

        【讨论】:

          猜你喜欢
          • 2020-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-10
          • 1970-01-01
          • 2022-01-10
          • 1970-01-01
          相关资源
          最近更新 更多