i was creating a double linked and it was showing unexpected behaiviour ,it is not printing the data stored in linked list.enter image description here
is something wrong in my logic? also do i need to allocate dyamic memory for temp , head and tail node? because since when i was creating a single linked list , i just used malloc() on new_node only . and it worked wonderfully.
and in double linked list code. if i dont use malloc() on head ,tail segmentation error occurs enter image description here
#include <stdio.h>
#include<stdlib.h>
int main()
{
int value,ch;
struct node{
int data;
struct node *next;
struct node *prev;
}*new_node,*head,*tail,*temp;
head=(struct node*)malloc(sizeof(struct
node*));
tail=(struct node*)malloc(sizeof(struct
node*));
temp=(struct node*)malloc(sizeof(struct
node*));
do{
new_node=(struct node*)malloc(sizeof(struct
node*));
printf("Hello user , enter data for your
node\n");
scanf("%d" , &value);
new_node->data=value;
if(head==NULL)
{
new_node->next=NULL;
new_node->prev=NULL;
head=new_node;
tail=new_node;
}
else
{
new_node->prev=tail;
new_node->next=NULL;
tail->next=new_node;
tail=new_node;
}
printf("\ndo you want to add extra node?
\nenter 0 to continiue or 1 for exit\n");
scanf("%d",&ch);
}while(ch==0);
temp=head;
while(temp!=NULL)
{
printf("%d", temp->data);
temp=temp->next;
}
printf("\nprogram executed");
return 0;
}