user12239061
user12239061

Reputation:

Add element linked list

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

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

void create_list(node ** head, int n);
void display_list(node * head);
void add_e(node ** head);

int
main(void)
{
    int n;
    node *head;

    head = NULL;
    printf("Insert size of list: ");
    scanf("%d",&n);

    create_list(&head, n);
    display_list(head);
    add_e(&head);
    display_list(head);

    return 0;
}

void
display_list(node *head)
{
    if (head == NULL) {
        printf("Empty list.");
    }
    else {
        while (head != NULL) {
            printf("DATA: %d\n", head->data);
            head = head->next;
        }
        puts("null");
    }
}




void create_list(node **head,int n){
    node *new,*tmp;
    int num,i;
    *head = malloc(sizeof(node));
    if(*head == NULL){
        printf("Memory can not be allocated.");
    }
    else{
        printf("Insert element 1: ");
        scanf("%d",&num);
        (*head)->data = num;
        (*head)->next = NULL;
        tmp = *head;

        for(i=2;i<=n;i++){
            new = malloc(sizeof(node));
            if(new == NULL){
                printf("Memory can not be allocated.");
                break;
            }
                else{
                printf("Insert element %d: ",i);
                scanf("%d",&num);
                new->data = num;
                new->next = NULL;
                tmp->next = new;
                tmp = tmp->next;
            }
        }
    }
}


void
add_e(node **head)
{
    node *new;
    int num;

    new = malloc(sizeof(node));
    if (new == NULL) {
        printf("Memory can not be allocated.");
    }
    else {
        printf("Insert element at the beginnig: ");
        scanf("%d", &num);
        new->data = num;
        new->next = NULL;
        while ((*head)->next != NULL) {
            *head = (*head)->next;
        }
        (*head)->next = new;
    }
}

I don't understand why after using the add_e() function, the display_list() function gives to me only the last two number of the list. The add_e() fucntion should be add an element at the end of the list. What am i doing wrong?

Edit: Added create_list() function so you can understand better but now it says to me to add more details so I'm writing something.

Upvotes: 0

Views: 60

Answers (1)

Craig Estey
Craig Estey

Reputation: 33601

In main, n is unitialized, so you'll get random/bad results.

The add_e should not use *head in the while or even do a while. The printf says "insert at beginning", which is different/simpler. This is what I've currently coded up/fixed.

You'd want to use a loop, if you [really] wanted to insert/append to the end of the list. But, the loop would still be incorrect, because you don't want to advance head when finding the end.

I've also fixed the printf for prompts and scanf

Here's a refactored/fixed version of your code with the bugs annotated:

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

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

void create_list(node **head, int n);
void display_list(node *head);
void add_e(node ** head);

int
main(void)
{
    int n;
    node *head;

    head = NULL;

// NOTE/BUG: n is unitialized
#if 1
    n = 5;
#endif
    create_list(&head, n);
    display_list(head);

    add_e(&head);
    display_list(head);

    return 0;
}

void
display_list(node *head)
{
    if (head == NULL) {
        printf("Empty list.");
    }
    else {
        while (head != NULL) {
            printf("DATA: %d\n", head->data);
            head = head->next;
        }
        puts("null");
    }
}

void
create_list(node **head, int n)
{
    node *new,
    *tmp;
    int num,
     i;

    *head = malloc(sizeof(node));
    if (*head == NULL) {
        printf("Memory can not be allocated.");
    }
    else {
        printf("Insert element 1: ");
#if 1
        fflush(stdout);
#endif
#if 0
        scanf("%d", &num);
#else
        scanf(" %d", &num);
#endif

        (*head)->data = num;
        (*head)->next = NULL;
        tmp = *head;

        for (i = 2; i <= n; i++) {
            new = malloc(sizeof(node));
            if (new == NULL) {
                printf("Memory can not be allocated.");
                break;
            }
            else {
                printf("Insert element %d: ", i);
#if 1
                fflush(stdout);
#endif
#if 0
                scanf("%d", &num);
#else
                scanf(" %d", &num);
#endif
                new->data = num;
                new->next = NULL;
                tmp->next = new;
                tmp = tmp->next;
            }
        }
    }
}

void
add_e(node **head)
{
    node *new;
    int num;

    new = malloc(sizeof(node));
    if (new == NULL) {
        printf("Memory can not be allocated.");
    }
    else {
        printf("Insert element at the beginnig: ");
        fflush(stdout);
        scanf(" %d", &num);

        new->data = num;
        new->next = NULL;

#if 0
        while ((*head)->next != NULL) {
            *head = (*head)->next;
        }
        (*head)->next = new;
#else
        if (*head == NULL)
            *head = new;
        else {
            new->next = *head;
            *head = new;
        }
#endif
    }
}

UPDATE:

In add_e, because I couldn't be sure if you wanted to insert at beginning of list [based on the printf] or at the end [based on the code], I created a version that is cleaned up a bit more and demonstrates both types:

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

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

void create_list(node **head, int n);
void display_list(node *head);
void add_begin(node **head);
void add_end(node **head);

int
main(void)
{
    int n;
    node *head;

    setbuf(stdout,NULL);

    head = NULL;

    printf("Enter initial number of list elements: ");
    scanf(" %d",&n);

    create_list(&head, n);
    display_list(head);

    add_begin(&head);
    display_list(head);

    add_end(&head);
    display_list(head);

    return 0;
}

void
display_list(node *head)
{
    node *cur;

    if (head == NULL) {
        printf("Empty list.\n");
    }

    for (cur = head;  cur != NULL;  cur = cur->next)
        printf("DATA: %d\n", cur->data);
}

void
create_list(node **head, int n)
{
    node *new, *tmp;
    int num, i;

    tmp = *head;

    for (i = 1; i <= n; i++) {
        new = malloc(sizeof(node));
        if (new == NULL) {
            printf("Memory can not be allocated.");
            break;
        }

        printf("Insert element %d: ", i);
        scanf(" %d", &num);

        new->data = num;
        new->next = NULL;

        if (*head == NULL)
            *head = new;
        else
            tmp->next = new;

        tmp = new;
    }
}

// add_begin -- insert at before head of list
void
add_begin(node **head)
{
    node *new;
    int num;

    new = malloc(sizeof(node));
    if (new == NULL) {
        printf("Memory can not be allocated.");
        exit(1);
    }

    printf("Insert element at the beginning: ");
    scanf(" %d", &num);

    new->data = num;
    new->next = *head;

    *head = new;
}

// add_end -- add to tail/end of list
void
add_end(node **head)
{
    node *new;
    node *tmp;
    node *tail;
    int num;

    new = malloc(sizeof(node));
    if (new == NULL) {
        printf("Memory can not be allocated.");
        exit(1);
    }

    printf("Append element at the end: ");
    scanf(" %d", &num);

    new->data = num;
    new->next = NULL;

    // find the tail
    tail = NULL;
    for (tmp = *head;  tmp != NULL;  tmp = tmp->next)
        tail = tmp;

    if (tail != NULL)
        tail->next = new;
    else
        *head = new;
}

Upvotes: 1

Related Questions