0% found this document useful (0 votes)
3 views

DLL

This document provides an overview of a C program that implements a doubly linked list, detailing its structure, key concepts, and various operations such as insertion and deletion. It includes code examples with explanations for each function, demonstrating how to manage nodes dynamically. The conclusion emphasizes the program's effectiveness in showcasing the doubly linked list and its relevance for further exploration of data structures in C programming.

Uploaded by

marfanikulsum
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)
3 views

DLL

This document provides an overview of a C program that implements a doubly linked list, detailing its structure, key concepts, and various operations such as insertion and deletion. It includes code examples with explanations for each function, demonstrating how to manage nodes dynamically. The conclusion emphasizes the program's effectiveness in showcasing the doubly linked list and its relevance for further exploration of data structures in C programming.

Uploaded by

marfanikulsum
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
You are on page 1/ 9

ZZZ Code AI

Edit Prompt New Prompt

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!

Implementation of a Doubly Linked


List in C

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:

Node Definition: The struct node defines the structure of each


node.
Function Declarations: Functions for displaying the list,
inserting, and deleting nodes.
Main Function: The entry point of the program that handles
user interaction and invokes the appropriate functions based
on user input.

Code Examples
Below is the provided code with a detailed line-by-line
explanation.

language-c Copy code

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

These lines include the standard input-output and standard


library headers, which are necessary for using functions like
printf, scanf, and malloc.

language-c Copy code

struct node {
int data;
struct node *next;
struct node *prev;
} *head, *tail, *newnode, *temp;

This defines a structure named node that contains an integer


data, a pointer to the next node (next), and a pointer to the
previous node (prev). The pointers head, tail, newnode, and
temp are declared globally to manage the list.

language-c Copy code

void display();
void doublyLL();
void newval();
void insertbegin();
void insertend();
void insertpos();
void deletebegin();
void deleteend();
void deletepos();

These are function prototypes for the various operations that


can be performed on the doubly linked list.

language-c Copy code

int i, pos, newvalue;

These variables are declared globally to be used across


different functions for indexing and storing new values.

language-c Copy code

int main() {
int option;
doublyLL();

The main function begins execution, and the doublyLL function


is called to initialize the list.

language-c Copy code

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);

A loop is initiated to allow the user to perform multiple


operations. The user is prompted to select an operation.

language-c Copy code

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;
}

A switch statement is used to call the appropriate function


based on the user's choice.

language-c Copy code

printf("Do you want to continue? (Y/N): ");


scanf(" %c", &cont);
} while (cont == 'Y' || cont == 'y');

The user is asked if they wish to continue, and the loop


continues if the answer is 'Y' or 'y'.
language-c Copy code

return 0;
}

The main function returns 0, indicating successful execution.

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;

The doublyLL function initializes the list by allowing the


user to enter values. A new node is created for each value
entered.

language-c Copy code

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

printf("Do you want to continue? (Y/N): ");


scanf(" %c", &ch);
} while (ch == 'Y' || ch == 'y');

The user is prompted to continue adding values.

language-c Copy code

display();
}

After the user finishes entering values, the display function


is called to show the current list.

language-c Copy code

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");
}

The display function traverses the list from head to tail,


printing each node's data.

language-c Copy code

void newval() {
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter a value \n");
scanf("%d", &newvalue);
}

The newval function allocates memory for a new node and


prompts the user for a value.

language-c Copy code

void insertbegin() {
newval();
newnode->data = newvalue;
newnode->next = head;
newnode->prev = NULL;
if(head != NULL) head->prev = newnode;
head = newnode;
display();
}

The insertbegin function inserts a new node at the beginning


of the list.

language-c Copy code

void insertend() {
newval();
newnode->data = newvalue;
newnode->next = NULL;
newnode->prev = tail;
if(tail != NULL) tail->next = newnode;
tail = newnode;
display();
}

The insertend function appends a new node to the end of the


list.

language-c Copy code

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();
}

The insertpos function allows the user to insert a new node at


a specified position.

language-c Copy code

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.

language-c Copy code

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;

for(i = 0; i < (pos - 1) && temp != NULL; i++) temp = temp->next;


if(temp != NULL) {
if(temp->prev != NULL) temp->prev->next = temp->next;
if(temp->next != NULL) temp->next->prev = temp->prev;
if(temp == head) head = temp->next;
}
free(temp);
display();
}

The deletepos function allows the user to delete a node at a


specified position.

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.

Please help us by rating this answer:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy