Home > database >  Adding to list and displaying list functions in C
Adding to list and displaying list functions in C

Time:05-16

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.

CodePudding user response:

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;
}

CodePudding user response:

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
$ 
  • Related