0% found this document useful (0 votes)
2 views2 pages

DoublyLinkedList

This document contains a C program that implements a doubly linked list with functionalities to insert, delete, and display nodes. The main menu allows users to choose between these operations until they decide to exit. Memory management is handled using dynamic allocation for nodes, and appropriate messages are displayed for overflow and underflow conditions.

Uploaded by

Pramod Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

DoublyLinkedList

This document contains a C program that implements a doubly linked list with functionalities to insert, delete, and display nodes. The main menu allows users to choose between these operations until they decide to exit. Memory management is handled using dynamic allocation for nodes, and appropriate messages are displayed for overflow and underflow conditions.

Uploaded by

Pramod Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

include <stdio.

h>
#include <stdlib.h>

struct node {
struct node *prev;
struct node *next;
int data;
};

struct node *head = NULL;

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;

ptr = (struct node *)malloc(sizeof(struct node));


if(ptr == NULL) {
printf("\nOVERFLOW\n");
return;
}

printf("\nEnter Item value: ");


scanf("%d", &item);
ptr->data = 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;
}
}

You might also like