DLL
DLL
This website helped you? Help us back by sharing your answer Copy
and make it viral! url
https://zzzcode.ai/code-explain?id=460accac-d3f0-4b9d-a818-
b8fcaa885a8e
View Count: 1
Do you know this website cost us over 50$ per day to stay free?
Contributing to this website take 10 seconds. Help us this week
with:
Step 1: Click on EF Core Bulk Insert Optimized
Step 2: And share/follow it!
Introduction
This document provides a comprehensive overview of a C program
that implements a doubly linked list. A doubly linked list is a
data structure that consists of nodes, where each node contains
a data field and two pointers: one pointing to the next node and
another pointing to the previous node. This structure allows for
efficient insertion and deletion of nodes from both ends and at
specific positions.
Key Concepts
1. Doubly Linked List: A linked list where each node contains
pointers to both the next and previous nodes, allowing
traversal in both directions.
2. Node Structure: Each node is defined by a structure that
contains an integer data field and pointers to the next and
previous nodes.
3. Dynamic Memory Allocation: The program utilizes malloc to
allocate memory for new nodes dynamically.
4. Basic Operations: The program supports various operations
such as insertion and deletion at the beginning, end, and
specific positions within the list.
Code Structure
The code is structured into several functions, each responsible
for a specific operation on the doubly linked list. The main
components include:
Code Examples
Below is the provided code with a detailed line-by-line
explanation.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
struct node *prev;
} *head, *tail, *newnode, *temp;
void display();
void doublyLL();
void newval();
void insertbegin();
void insertend();
void insertpos();
void deletebegin();
void deleteend();
void deletepos();
int main() {
int option;
doublyLL();
char cont;
do {
printf(" \nWhat operation do you want to perform? \n");
printf("1-Insert at the beginning \n2-Insert at the end \n3-Insert at a
scanf("%d", &option);
switch (option) {
case 1: insertbegin(); break;
case 2: insertend(); break;
case 3: insertpos(); break;
case 4: deletebegin(); break;
case 5: deleteend(); break;
case 6: deletepos(); break;
default: printf("Option not valid"); break;
}
return 0;
}
Function Definitions
language-c Copy code
void doublyLL() {
char ch;
do {
int value;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter a value: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = NULL;
newnode->prev = NULL;
if(head == NULL) {
head = newnode;
tail = newnode;
} else {
tail->next = newnode;
newnode->prev = tail;
tail = newnode;
}
If the list is empty, the new node becomes both the head and
tail. Otherwise, it is added to the end of the list.
language-c Copy code
display();
}
void display() {
struct node *temp = head;
printf("The digits in linked list are ");
while(temp != NULL) {
printf(" %d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void newval() {
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter a value \n");
scanf("%d", &newvalue);
}
void insertbegin() {
newval();
newnode->data = newvalue;
newnode->next = head;
newnode->prev = NULL;
if(head != NULL) head->prev = newnode;
head = newnode;
display();
}
void insertend() {
newval();
newnode->data = newvalue;
newnode->next = NULL;
newnode->prev = tail;
if(tail != NULL) tail->next = newnode;
tail = newnode;
display();
}
void insertpos() {
printf("Enter position: ");
scanf("%d", &pos);
newval();
newnode->data = newvalue;
newnode->next = NULL;
temp = head;
for(i = 0; i < (pos - 1) && temp != NULL; i++) temp = temp->next;
if(temp != NULL) {
newnode->next = temp;
newnode->prev = temp->prev;
if(temp->prev != NULL) temp->prev->next = newnode;
else head = newnode;
temp->prev = newnode;
}
display();
}
void deletebegin() {
temp = head;
head = head->next;
if(head != NULL) head->prev = NULL;
free(temp);
display();
}
The deletebegin function removes the first node from the list.
void deleteend() {
temp = tail;
tail = tail->prev;
if(tail != NULL) tail->next = NULL;
else head = NULL;
free(temp);
display();
}
The deleteend function removes the last node from the list.
language-c Copy code
void deletepos() {
printf("Enter position: ");
scanf("%d", &pos);
temp = head;
Conclusion
The provided C program effectively demonstrates the
implementation of a doubly linked list, showcasing various
operations such as insertion and deletion at different
positions. The use of dynamic memory allocation allows for
flexible management of nodes, making this data structure
suitable for applications requiring efficient data manipulation.
Understanding this implementation provides a solid foundation
for further exploration of more complex data structures and
algorithms in C programming.