0% found this document useful (0 votes)
12 views8 pages

Doubly Linked List Operations in C

Uploaded by

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

Doubly Linked List Operations in C

Uploaded by

Pruthviraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INPUT:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

void insertFront(struct Node** head_ref, int data) {

struct Node* newNode = createNode(data);

newNode->next = *head_ref;

if (*head_ref != NULL) {

(*head_ref)->prev = newNode;

*head_ref = newNode;

void insertBack(struct Node** head_ref, int data) {


struct Node* newNode = createNode(data);

if (*head_ref == NULL) {

*head_ref = newNode;

return;

struct Node* temp = *head_ref;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void deleteFront(struct Node** head_ref) {

if (*head_ref == NULL) {

printf("List is empty\n");

return;

struct Node* temp = *head_ref;

*head_ref = temp->next;

if (*head_ref != NULL) {

(*head_ref)->prev = NULL;

free(temp);

void deleteBack(struct Node** head_ref) {


if (*head_ref == NULL) {

printf("List is empty\n");

return;

struct Node* temp = *head_ref;

if (temp->next == NULL) {

free(temp);

*head_ref = NULL;

return;

while (temp->next != NULL) {

temp = temp->next;

temp->prev->next = NULL;

free(temp);

void display(struct Node* node) {

while (node != NULL) {

printf("%d ", node->data);

node = node->next;

printf("\n");

int main() {

struct Node* head = NULL;

int choice, data;


while (1) {

printf("\nDoubly Linked List Menu:\n");

printf("1. Insert at Front\n");

printf("2. Insert at Back\n");

printf("3. Delete from Front\n");

printf("4. Delete from Back\n");

printf("5. Display List\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data to insert at front: ");

scanf("%d", &data);

insertFront(&head, data);

break;

case 2:

printf("Enter data to insert at back: ");

scanf("%d", &data);

insertBack(&head, data);

break;

case 3:

deleteFront(&head);

printf("Deleted from front.\n");

break;

case 4:

deleteBack(&head);

printf("Deleted from back.\n");

break;
case 5:

printf("List: ");

display(head);

break;

case 6:

printf("Exiting...\n");

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

OUTPUT:

You might also like