在C中添加到列表和显示列表功能

发布于 2025-01-29 00:58:10 字数 1311 浏览 2 评论 0原文

我有我创建的链接列表。我制作了AddTostart函数,该函数具有HAS2参数,第一个参数是列表的负责人,第二个参数是要插入节点中的数据。我还有DisplayList,该列表仅显示列表中的内容。

代码:

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

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

struct node* addToStart(struct node* head, int d){
    struct node *new = malloc(sizeof(struct node));
    if(new == NULL){
        printf("out of memory");

    }
    else{
        new->data = d;
        new->next = NULL;
        new->next = head;
        head = new;
        return head;

    }
    return 0;
}
void displayList(struct node* head){
    struct node *ptr = head;
    while(ptr != NULL){
        printf("%d -> ",ptr->data);
        ptr = ptr->next;
    }
}
int main() {

    int data=0;
    struct node *head;
    head = malloc(sizeof(struct node));
    if(head == NULL){
        printf("Out of memory");
    }
    head->next = NULL;
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit.");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);


    }
    printf("Number have been recorded in the list. Here are the numbers");
    displayList(head);
    return 0;
}

列表被显示,但随后我看到了其他奇怪的数字。 IE 40274093。它的问题是什么。

I have my linked list that I created. I made addToStart function which has2 paramters, first parameter is the head of the list, second parameter is the data to be inserted in the nodes. I also have displayList which just displays the content in the list.

Code:

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

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

struct node* addToStart(struct node* head, int d){
    struct node *new = malloc(sizeof(struct node));
    if(new == NULL){
        printf("out of memory");

    }
    else{
        new->data = d;
        new->next = NULL;
        new->next = head;
        head = new;
        return head;

    }
    return 0;
}
void displayList(struct node* head){
    struct node *ptr = head;
    while(ptr != NULL){
        printf("%d -> ",ptr->data);
        ptr = ptr->next;
    }
}
int main() {

    int data=0;
    struct node *head;
    head = malloc(sizeof(struct node));
    if(head == NULL){
        printf("Out of memory");
    }
    head->next = NULL;
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit.");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);


    }
    printf("Number have been recorded in the list. Here are the numbers");
    displayList(head);
    return 0;
}

the list gets displayed but then I see other weird number(s). i.e 40274093. Whats the problem with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2025-02-05 00:58:10

您忘了初始化head-&gt; data
不过,您可以将其设置为null,而不是初始化它,然后立即开始调用addtostart。如果将null作为head参数传递到addtostart,则应将Next设置为null并按预期工作。以下内容应解决您的问题:

int main() {

    int data=0;
    struct node *head;
    head = NULL; //Just don't allocate memory for `head` and let the function do it for you
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit.");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);


    }
    printf("Number have been recorded in the list. Here are the numbers");
    displayList(head);
    return 0;
}

You forgot to initialize head->data.
Instead of initializing it though, you can just set it to NULL, and immediatly start calling addToStart on it. If you pass NULL as the head parameter to addToStart, it should just set the next to NULL and work as expected. The following should fix your problem:

int main() {

    int data=0;
    struct node *head;
    head = NULL; //Just don't allocate memory for `head` and let the function do it for you
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit.");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);


    }
    printf("Number have been recorded in the list. Here are the numbers");
    displayList(head);
    return 0;
}
野鹿林 2025-02-05 00:58:10

清理一些内联评论:

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

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

struct node* addToStart(struct node* head, int d){
    struct node *new = malloc(sizeof(struct node));
    if(new == NULL){
        printf("out of memory");
    }
    else{
        new->data = d;
//        new->next = NULL; // unnecessary, overwritten in next line
        new->next = head;
//        head = new; // does not change head, better
        return new; // return new instead of head;

    }
    return NULL; // 0; want to return a pointer
}
void displayList(struct node* head){
    struct node *ptr = head; // you could just use head as it is a call-be-value parameter
    while(ptr != NULL){
        printf("%d",ptr->data); // removed "->" here
        ptr = ptr->next;
        if (ptr != NULL) { // want " -> " between elements only
            printf(" -> ");
        }
    }
    printf("\n"); // end output with a newline, see https://stackoverflow.com/questions/27238564/getting-a-weird-percent-sign-in-printf-output-in-terminal-with-c
}
int main() {

    int data=0;
    struct node *head;
/*  
    // the following lines add nothing to the list implementation
    // it is just that all lists now end with a node containing 0 as value.  
    head = malloc(sizeof(struct node));
    if(head == NULL){
        printf("Out of memory");
    }
    head->next = NULL;
*/
    head = NULL; // initialize head to indicate list end
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit: ");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);
    }
    printf("Numbers have been recorded in the list. Here are the numbers: \n");
    displayList(head);
    return 0;
}
$ gcc -Wall list.c
$ ./a.out         
Enter a number to be added to the beginning of the list, -1 to exit: 1
Enter a number to be added to the beginning of the list, -1 to exit: 2
Enter a number to be added to the beginning of the list, -1 to exit: 3
Enter a number to be added to the beginning of the list, -1 to exit: -1
Numbers have been recorded in the list. Here are the numbers: 
3 -> 2 -> 1
$ 

Cleaned up with some inline comments:

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

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

struct node* addToStart(struct node* head, int d){
    struct node *new = malloc(sizeof(struct node));
    if(new == NULL){
        printf("out of memory");
    }
    else{
        new->data = d;
//        new->next = NULL; // unnecessary, overwritten in next line
        new->next = head;
//        head = new; // does not change head, better
        return new; // return new instead of head;

    }
    return NULL; // 0; want to return a pointer
}
void displayList(struct node* head){
    struct node *ptr = head; // you could just use head as it is a call-be-value parameter
    while(ptr != NULL){
        printf("%d",ptr->data); // removed "->" here
        ptr = ptr->next;
        if (ptr != NULL) { // want " -> " between elements only
            printf(" -> ");
        }
    }
    printf("\n"); // end output with a newline, see https://stackoverflow.com/questions/27238564/getting-a-weird-percent-sign-in-printf-output-in-terminal-with-c
}
int main() {

    int data=0;
    struct node *head;
/*  
    // the following lines add nothing to the list implementation
    // it is just that all lists now end with a node containing 0 as value.  
    head = malloc(sizeof(struct node));
    if(head == NULL){
        printf("Out of memory");
    }
    head->next = NULL;
*/
    head = NULL; // initialize head to indicate list end
    while(data != -1){
        printf("Enter a number to be added to the beginning of the list, -1 to exit: ");
        scanf("%d",&data);
        if(data == -1)
            break;
        head = addToStart(head,data);
    }
    printf("Numbers have been recorded in the list. Here are the numbers: \n");
    displayList(head);
    return 0;
}
$ gcc -Wall list.c
$ ./a.out         
Enter a number to be added to the beginning of the list, -1 to exit: 1
Enter a number to be added to the beginning of the list, -1 to exit: 2
Enter a number to be added to the beginning of the list, -1 to exit: 3
Enter a number to be added to the beginning of the list, -1 to exit: -1
Numbers have been recorded in the list. Here are the numbers: 
3 -> 2 -> 1
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文