Halo all,
I was making a simple doubly linked list but while diplaying it I got this run time error-
Here is my code. I'm not able to get where I'm doing wrong.Quote:
Unhandled exception at 0x004115ec in example.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
I've debugged the code but to no avail.Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *right;
struct node *left;
}*head=NULL,*tail=NULL,*p=NULL;
void make_list(void)
{
int elts,count;
struct node *temp;
printf("How many elements do you want to enter\n");
scanf("%d",&elts);
for(count=0;count<elts;count++)
{
p=malloc(sizeof(struct node));
printf("Enter the %d element",count+1);
scanf("%d",&(p->info));
if(head==NULL)
{
head=p;
head->left=NULL;
head->right=NULL;
tail=head;
}
else
{
temp=tail->right;
tail->right=p->left;
p->left=temp;
p->right=NULL;
tail=p;
}
}
}
void display(void)
{
for(p=head;p!=tail;p=p->right)
printf("%d->",p->info);
}
int main(void)
{
int ch;
while(1)
{
printf("\nWhat do you wanna do\n1.Make a list\n2.display the list\n3.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:make_list();
break;
case 2:display();
break;
case 3:exit(0);
}
}
}
Thanks