0

It's not working when I try to insert at beginning first. But working when I try to insert at end first.

Liked lists should work both ways I think. It's singly linked lists.

I am learning so please explain thoroughly and in easy language.

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

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

struct node * insrt_frnt(struct node *head){
    struct node *temp = (struct node *) malloc(sizeof(struct node *));
    printf("\n Enter data:");
    scanf("%d", &temp->data);

    if(head == NULL){
        head = temp;
    } else {
        temp->next = head;
    }
    return temp;
}

struct node * insrt_end(struct node *head){
    struct node *ptrr = head;
    struct node *neww = (struct node *) malloc(sizeof(struct node *));

    printf("\n Enter data:");
    scanf("%d", &neww->data);
    neww->next = NULL;

    if(head == NULL){
        head = neww;
    } else {
        while(ptrr != NULL){
            ptrr = ptrr->next;
        }
        ptrr->next = neww;
    }
    return head;
}

void display(struct node *head){
    while(head != NULL){
        printf("%d\n", head->data);
        head = head->next;
    }
}


int main(){
    struct node *head = NULL;

    head = insrt_frnt(head);

    head = insrt_end(head);

    display(head);

    return 0;
}
  • Error: `malloc(sizeof(struct node *))`. You want to create a node here, not a pointer, so you need to indicate a size of a node: `malloc(sizeof(struct node))`. – CiaPan Jun 06 '21 at 13:50
  • Error: after `malloc`-ing a new node you do not initialize its `next` member. – CiaPan Jun 06 '21 at 13:53
  • Error: the `while(ptrr != NULL)` loop iterates until `ptrr` becomes `NULL` (unless the program crashes prematurely due to the error no. 1). Then the `ptrr->next = neww;` instruction dereferences a NULL pointer which is almost a guarantee of a crash. – CiaPan Jun 06 '21 at 13:57
  • Can you explain, how functions `insrt_frnt` and `insrt_end` access the `head` variable? – CiaPan Jun 06 '21 at 13:59
  • @CiaPan All three errors are taken care of. But still not working properly. You can check my code here https://ideone.com/CdvzIb What about accessing the head variable? – Raghav Sharma Jun 06 '21 at 16:11
  • As far as I can see the third error is not fixed. You replaced the `ptrr` variable in a `while()` loop with `head` but the problem remains: the loop ends when `head == NULL` then an assignment to `head->next` causes a crash. – CiaPan Jun 06 '21 at 19:14
  • Also: [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – CiaPan Jun 06 '21 at 19:37
  • Thanks @CiaPan . You can see here https://ideone.com/mc65iK It is working perfectly for the moment but I checked it is not working when i am calling insrt_end function more than once. – Raghav Sharma Jun 11 '21 at 06:08
  • @CiaPan I figured what was wrong with my code. Now feeling confident. You can see my code here https://ideone.com/iuCdYK . Thanks – Raghav Sharma Jun 13 '21 at 16:05

0 Answers0