[go: up one dir, main page]

0% found this document useful (0 votes)
29 views7 pages

Datastructures Lab Double Linkedlist

The document contains a C program that implements a doubly linked list with functionalities to insert, delete, and display elements. It defines a structure for nodes and provides a menu-driven interface for user interaction. The program handles memory allocation and deallocation for nodes and manages the linked list operations accordingly.

Uploaded by

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

Datastructures Lab Double Linkedlist

The document contains a C program that implements a doubly linked list with functionalities to insert, delete, and display elements. It defines a structure for nodes and provides a menu-driven interface for user interaction. The program handles memory allocation and deallocation for nodes and manages the linked list operations accordingly.

Uploaded by

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

Data Structures Lab

Double Linked list


PROGRAM:

#include <stdio.h>
#include <stdlib.h>

struct node {
int value;
struct node *next;
struct node *prev;
};
void insert();
void display();
void delete();

typedef struct node DATA_NODE;

DATA_NODE *head_node = NULL, *tail_node = NULL, *temp_node = NULL;


int data;
int main() {
int option = 0;
printf("Doubly Linked List - Insert, Delete, Display\n");
while (option < 4) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option: ");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List: ");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof(DATA_NODE));
temp_node->value = data;
temp_node->next = NULL;
temp_node->prev = NULL;

if (head_node == NULL) {
head_node = temp_node;
tail_node = temp_node;
} else {
tail_node->next = temp_node;
temp_node->prev = tail_node;
tail_node = temp_node;
}
printf("Inserted %d into the linked list.\n", data);
}
void delete() {
int pos, i = 1;
temp_node = head_node;
printf("\nEnter Position for Delete Element: ");
scanf("%d", &pos);
if (temp_node == NULL) {
printf("List is empty, nothing to delete.\n");
return;
}
if (pos == 1) {
head_node = head_node->next;
if (head_node != NULL) head_node->prev = NULL;
free(temp_node);
printf("Deleted the element at position %d.\n", pos);
} else {
while (i < pos && temp_node != NULL) {
temp_node = temp_node->next;
i++;
}
if (temp_node != NULL) {
if (temp_node == tail_node) {
tail_node = tail_node->prev;
tail_node->next = NULL;
} else {
temp_node->prev->next = temp_node->next;
if (temp_node->next != NULL) {
temp_node->next->prev = temp_node->prev;
}
}
free(temp_node);
printf("Deleted the element at position %d.\n", pos);
} else {
printf("Invalid position!\n");
}
}
}
void display() {
temp_node = head_node;
if (temp_node == NULL) {
printf("List is empty.\n");
return;
}
printf("\nDisplay Linked List: \n");
while (temp_node != NULL) {
printf("# %d # ", temp_node->value);
temp_node = temp_node->next;
}
printf("\n");
}
Thank You

You might also like