I have a program for creating a singly linked list,adding a node at the beginning and end of the list. The program is producing the intended output,but it is crashing after producing the last output. I have created the linked list in main(),i have 2 functions for adding a node at the beginning and at the end respectively.
#include<stdio.h>
struct book
{
char bname[20];
char aname[20];
int pages;
struct book *next;
};
struct book *first;
struct book *current;
struct book *previous;
int main()
{
int count=0,temp=0;
for(int i=1;i<4;i++)
{
current=(struct book*)malloc(sizeof(struct book));//memory assigned to only the current structure
if(current==NULL)
{
break;
}
if(first==NULL)
{
first=current; //if the first node value is null then it hadn't yet been processed,so current node is now the first;.
}
if(previous!=NULL)
{
previous->next=current;//stores the current structure address to the next member(pointer) of the previous structure address
}
printf("\nBook Name:: ");
scanf("%s",current->bname);
printf("\nAuthor Name:: ");
scanf("%s",current->aname);
printf("\nPages::");
scanf("%d",¤t->pages);
current->next=NULL;//if this is the last node
//will again be filled up if there is a next structure
previous=current;//the current node is the previous node for the next iteration
}
current=first;
while(current!=NULL)
{
count++;
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates
}
addnodebeginning(first);
}
/*****************************inserting at the BEGINNING*********************************/
void addnodebeginning(struct book *first)
{
int count=0,temp=0;
current=(struct book*)malloc(sizeof(struct book));
current->next=first;
first=current;
printf("\nBook Name:: ");
scanf("%s",current->bname);
printf("\nAuthor Name:: ");
scanf("%s",current->aname);
printf("\nPages::");
scanf("%d",¤t->pages);
current=first;
while(current!=NULL)
{
count++;
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates
}
addnode_end(first);
}
/*****************************inserting at the end*********************************/
void addnode_end(struct node *first)
{
current=first;
while(1)
{
if(current->next==NULL)
{
struct book *newnode=(struct book*)malloc(sizeof(struct book));
printf("\nBook Name:: ");
scanf("%s",newnode->bname);
printf("\nAuthor Name:: ");
scanf("%s",newnode->aname);
printf("\nPages::");
scanf("%d",&newnode->pages);
current->next=newnode;
//newnode->next=NULL;
break;
}
current=current->next;
}
current=first;
while(current!=NULL)
{
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;
free(previous);
}
}
Why is this happening?