//*************************SERIOUS PROBLEM, NOT WORKING******************
//DOUBLY LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct dll
{
int info;
struct dll *next;
struct dll *prev;
};
struct dll *head = NULL, *tail = NULL, *temp;
void append();
void insafter();
void delete();
void display();
void main()
{
int choice;
do
{
printf("Enter your choice:\n");
printf("1.Append Node\n2.Insert After a Node\n3.Delete the First Node\n4.Display\n5.Exit");
scanf("%d", &choice);
switch(choice)
{
case 1:
append();
break;
case 2:
insafter();
break;
case 3:
delete();
break;
case 4:
display();
case 5:
exit(1);
default:
printf("Invalid choice!\n");
}
}while(choice!=4);
}
void append()
{
int x;
printf("Enter element you wish to push\n");
scanf("%d", &x);
temp->info = x;
if(head==tail && head==NULL)
{
temp = head;
temp->next = temp;
temp->prev = NULL;
}
else
{
tail->next = temp;
temp->prev = tail;
}
tail = temp;
temp->next = NULL;
}
void insafter() //I assume user inputs this option after he has made a list, no condition check for empty list
{
int x, added_item;
printf("Enter the information of the node after which you want to insert your new node\n");
scanf("%d", &x);
temp = head;
while(temp!=NULL)
{
while(temp->info!=x)
temp= temp->next;
}//now we have the node temp after which we insert our node
printf("Enter item to be added\n");
scanf("%d", &added_item);
struct dll *p;
p->info = added_item;
p->next = temp->next;
p->prev = temp;
temp->next = p;
}
void delete() //allows deleting only from head
{
temp = head;
if(head==NULL)
printf("List empty, nothing to delete\n");
else
printf("Deleted element: %d", temp->info);
head = head->next;
free(temp);
}
void display()
{
temp = head;
if(head==NULL)
printf("Doubly Linked List empty\n");
else
while(temp!=NULL)
{
printf("%d", temp->info);
temp = temp->next;
}
}
Can anyone tell me why my code is not working? Its a doubly linked list...