DoublyLinkedList
DoublyLinkedList
h>
#include <stdlib.h>
struct node {
struct node *prev;
struct node *next;
int data;
};
void insertion();
void deletion();
void display();
int main() {
int choice = 0;
while(choice != 4) {
printf("\n*********Main Menu*********\n");
printf("1. Insert\n2. Delete\n3. Show\n4. Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Please enter valid choice..\n");
}
}
return 0;
}
void insertion() {
struct node *ptr;
int item;
if(head == NULL) {
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
} else {
ptr->prev = NULL;
ptr->next = head;
head->prev = ptr;
head = ptr;
}
printf("\nNode inserted\n");
}
void deletion() {
struct node *ptr;
if(head == NULL) {
printf("\nUNDERFLOW\n");
} else if(head->next == NULL) {
free(head);
head = NULL;
printf("\nNode deleted\n");
} else {
ptr = head;
head = head->next;
head->prev = NULL;
free(ptr);
printf("\nNode deleted\n");
}
}
void display() {
struct node *ptr;
ptr = head;
if(ptr == NULL) {
printf("\nList is empty\n");
return;
}
printf("\nPrinting values...\n");
while(ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}