0

I was going through implementation of single linked list, and I came across whenever I try to add a node at beginning with a function call addatbegin() my program ends abruptly with return value 3221225477. I am attaching whole code here ,all functions working fine am just problem with addatbegin() function .kindly help me out in finding the error.

#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node* link;
};
struct node* root;
int len;
void append();
void addatbegin();
void addafter();
int length();
void Delete();
void display();
int main()
{
    int ch;
    while(1)
    {
        printf("Single Linked List Operations :\n");
        printf("1.Append\n");
        printf("2.Addatbegin\n");
        printf("3.Addafter\n");
        printf("4.Length\n");
        printf("5.Delete\n");
        printf("6.Display\n");
        printf("7.Quit\n");
        printf("Enter your choice :");
        scanf("%d",&ch);
        switch(ch)
     {
        case 1 : append();
                 printf("\n\n");
                 break;
        case 2 : addatbegin();
                 break;
        case 3 : addafter();
                 break;
        case 4 : len=length();
                 printf("Length is :%d\n\n",len);
                 break;
        case 5 : Delete();
                 break;
        case 6 : display();
                 break;
        case 7 : exit(0);
        default : printf("Invalid Input\n\n");
     }
    }
}
void append()
{
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
    printf("Enter node data :");
    scanf("%d",&temp->data);
    temp->link=NULL;
    if(root==NULL)
    root=temp;
    else{
        struct node *p;
        p=root;
        while(p->link!=NULL)
        {
            p=p->link;
        }
        p->link=temp;
    }
}
void  display()
{
    struct node* temp;
    temp=root;
    if(temp==NULL)
    printf("List is empty\n\n");
    else{
        while(temp!=NULL)
        {
            printf("%d-->",temp->data);
            temp=temp->link;
        }
        printf("\n\n");
    }
}
int length()
{
    struct node* temp;
    int count=0;
    temp=root;
    while(temp!=NULL)
    {
        count++;        
        temp=temp->link;        
    }
    return count;
}
void addatbegin()
{
    struct node* temp;
    printf("Enter node data :");
    scanf("%d",&temp->data);
    if(root==NULL)
    root=temp;
    else
    {
     temp->link=root;
     root=temp;
     }
     printf("Node inserted sucessfully\n\n");
}
void Delete()
{
    struct node* temp;
    int loc;
    printf("Enter the location : ");
    scanf("%d",&loc);
    if(loc>length())
    {
        printf("Invalid Location\n\n");
    }else if(loc==1)
    {
        temp=root;
        root=temp->link;
        temp->link=NULL;
        free(temp);
    }else{
        struct node *p;
        temp=root;
        int i=1;;
        while(i<loc-3)
        {
            temp=temp->link;
            i++;
        }
        p=temp->link;
        temp->link=p->link;
        p->link=NULL;
        free(p);
        printf("Node deleted sucessfully\n\n");
    }
}
void addafter()
{
    struct node* temp;
    int loc;
    printf("Enter the location : ");
    scanf("%d",&loc);
    if(loc>length())
    printf("Invalid location\nCurrently list is having %d nodes\n",length());
    else{
        int i=1;
        struct node* p=root;
        while(i<loc)
        {
            p=p->link;
            i++;
        }
    temp=(struct node*)malloc(sizeof(struct node*));
    printf("Enter node data : ");
    scanf("%d",&temp->data);
    temp->link=NULL;
    temp->link=p->link;
    p->link=temp;
    printf("Node inserted successfully\n\n");
    }
}

I am sharing output screen here too:

Yogesh Satyam
  • 33
  • 1
  • 7
  • 1
    In the `addatbegin` function, you have to allocate a node and assign its pointer to `temp` before doing `scanf("%d",&temp->data);`. Also don't forget to initialize `temp->link` even if `root == NULL`. – MikeCAT Jun 21 '21 at 15:59
  • `temp=(struct node*)malloc(sizeof(struct node*));` in the `addafter` is also wrong for wrong allocation size and [casting](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It shouldbe `temp = malloc(sizeof(*temp));` or `temp = malloc(sizeof(struct node));`. – MikeCAT Jun 21 '21 at 16:00
  • thanks, yeah by mistake I put '*' but in case of `temp` without type casting complier gives error message(in case of temp) – Yogesh Satyam Jun 21 '21 at 16:06
  • 1
    Ah, so this code is actually C++, not C? Please don't put `c` and `c++` tag together and use only one of them according to the language you are using. – MikeCAT Jun 21 '21 at 16:10
  • @MikeCAT I am facing one more issue if use `addatbegin()`then `append()` then it output screen stuck ,it takes input then gets stuck, just cursor keeps blinking.can u help me out with this – Yogesh Satyam Jun 21 '21 at 16:17
  • BTW, in C++, you don't need the `struct` keyword when declaring variables, parameters or return values. In C++ you don't need to write your own linked list, there is `std::list`. You tagged both C and C++ are you mixing them? – Thomas Matthews Jun 21 '21 at 16:34
  • Since you tagged as C++, prefer to use `operator new` instead of `malloc`. The `malloc` function doesn't call struct or class constructors. – Thomas Matthews Jun 21 '21 at 16:35
  • Since you tagged as C++, the common implementation is to have a templated container class (e.g. List) that uses a templated node class. The methods and members, would be *encapsulated* into the appropriate class. For example, the `root` pointer would be a member in the `List` class. – Thomas Matthews Jun 21 '21 at 16:38
  • Since you tagged as C++, the length of the list should be a member variable. It should initialized in the constructor, and updated by the insert and delete methods. This saves time by not having to traverse the list each time to find the length. – Thomas Matthews Jun 21 '21 at 16:40

0 Answers0