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

Exp 4

Experiments code

Uploaded by

jiyadubla
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)
18 views

Exp 4

Experiments code

Uploaded by

jiyadubla
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/ 17

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Name Dandekar Ganesh Gangadhar

UID no. 2023300037

Division. A

Batch. C

Submission Date. 10/09/2024

Experiment No. 4

AIM: perform following operations on doubly linked lists


1. Create
2. find the length of list using recursion
3. delete a node
4. display

Program 1.

THEORY: What is a Linked List?


A linked list is a linear data structure where elements are stored in nodes.
Each node contains two parts:
• Data: The value or data stored in the node.
• Pointer (or Reference): A reference to the next node in the
sequence.
Types of Linked Lists
1. Singly Linked List:
o Structure: Each node points to the next node.
o Termination: The last node points to NULL.
2. Doubly Linked List:
A doubly linked list is a data structure that consists of a set of nodes, each
of which contains a value and two pointers, one pointing to the previous
node in the list and one pointing to the next node in the list. This allows for
efficient traversal of the list in both directions, making it suitable for
applications where frequent insertions and deletions are required.
o Structure: Each node has two pointers:
▪ One to the next node.
▪ One to the previous node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

o Termination: The first node's previous pointer and the last


node's next pointer are NULL.
3. Circular Linked List:
o Structure: The last node points back to the first node.
o Traversal: Can be done starting from any node and
wrapping around to the beginning.
4. Circular Doubly Linked List:
o Structure: Each node has two pointers:
▪ One to the next node.
▪ One to the previous node.
o Circularity: The last node's next pointer points to the first
node, and the first node's previous pointer points to the last
node.

Doubly Linked List:

Doubly Linked List


Representation of Doubly Linked List in Data Structure
In a data structure, a doubly linked list is represented using nodes that have
three fields:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)

Basic Operations in Doubly Linked List:-


BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

• Traversal in Doubly Linked List:


To Traverse the doubly list, we can use the following steps:
a. Forward Traversal:
• Initialize a pointer to the head of the linked list.
• While the pointer is not null:
o Visit the data at the current node.
o Move the pointer to the next node.
b. Backward Traversal:
• Initialize a pointer to the tail of the linked list.
• While the pointer is not null:
o Visit the data at the current node.
o Move the pointer to the previous node.
o
• Finding Length of Doubly Linked List:
To find the length of doubly list, we can use the following steps:
• Start at the head of the list.
• Traverse through the list, counting each node visited.
• Return the total count of nodes as the length of the list.

• Insertion :

o Insertion at the beginning of Doubly Linked List.

To insert a new node at the beginning of the doubly list, we can use the
following steps:
• Create a new node, say new_node with the given data and set its
previous pointer to null, new_node->prev = NULL.
• Set the next pointer of new_node to current head, new_node->next
= head.
• If the linked list is not empty, update the previous pointer of the
current head to new_node, head->prev = new_node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

• Return new_node as the head of the updated linked list.

o Insertion at the end of the Doubly Linked List

To insert a new node at the end of the doubly linked list, we can use the
following steps:
• Allocate memory for a new node and assign the provided value to its
data field.
• Initialize the next pointer of the new node to nullptr.
• If the list is empty:
o Set the previous pointer of the new node to nullptr.
o Update the head pointer to point to the new node.
• If the list is not empty:
o Traverse the list starting from the head to reach the last node.
o Set the next pointer of the last node to point to the new node.
o Set the previous pointer of the new node to point to the last
node.

o Insertion at a specific position in Doubly Linked List

• If position = 1, create a new node and make it the head of the linked
list and return it.
• Otherwise, traverse the list to reach the node at position – 1,
say curr.
• If the position is valid, create a new node with given data,
say new_node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

• Update the next pointer of new node to the next of current node and
prev pointer of new node to current node, new_node->next = curr-
>next and new_node->prev = curr.
• Similarly, update next pointer of current node to the new
node, curr->next = new_node.
• If the new node is not the last node, update prev pointer of new
node’s next to the new node, new_node->next->prev = new_node

• Deletion :
o Deletion of a node at the beginning of Doubly Linked List

To delete a node at the beginning in doubly linked list, we can use


the following steps:
• Check if the list is empty, there is nothing to delete. Return.
• Store the head pointer in a variable, say temp.
• Update the head of linked list to the node next to the current
head, head = head->next.
• If the new head is not NULL, update the previous pointer of new
head to NULL, head->prev = NULL

o Deletion of a node at the end of Doubly Linked List

To delete a node at the end in doubly linked list, we can use the
following steps:
• Check if the doubly linked list is empty. If it is empty, then there is
nothing to delete.
• If the list is not empty, then move to the last node of the doubly
linked list, say curr.
• Update the second-to-last node’s next pointer to NULL, curr-
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

>prev->next = NULL.
• Free the memory allocated for the node that was deleted.

o Deletion of a node at a specific position in Doubly Linked List

To delete a node at a specific position in doubly linked list, we can


use the following steps:
• Traverse to the node at the specified position, say curr.
• If the position is valid, adjust the pointers to skip the node to be
deleted.
o If curr is not the head of the linked list, update the next
pointer of the node before curr to point to the node after
curr, curr->prev->next = curr-next.
o If curr is not the last node of the linked list, update the
previous pointer of the node after curr to the node before
curr, curr->next->prev = curr->prev.
• Free the memory allocated for the deleted node.

Applications of Doubly Linked List:

1. Browser History: Used to navigate forward and backward through


web pages.
2. Music Playlists: Enables navigation to the previous or next song.
3. Undo/Redo Functionality: Provides efficient navigation between
different states in software like text editors.
4. Navigation Systems: Used in scenarios where traversal in both
directions is required.
5. Memory Management: In some systems, used for managing
memory blocks where both forward and backward traversals are
required.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Advantages of Doubly Linked List:

1. Bidirectional Traversal: Can be traversed in both directions


(forward and backward).
2. Efficient Insertion/Deletion: Inserting or deleting a node is more
efficient as pointers to both previous and next nodes are available.
3. No Need for Extra Traversal: Can directly access the previous
node, unlike singly linked lists where you need to traverse from the
head to reach a node's predecessor.

Disadvantages of Doubly Linked List:

1. More Memory: Requires extra memory for storing the previous


pointer in each node.
2. Complex Implementation: Requires handling additional pointers,
making the implementation more complex than a singly linked list.
3. Slower Operations: Extra pointers can make certain operations
slower due to the need to update multiple pointers during insertions
or deletions.

ALGORITHM: Step 1: Define the Node Structure


• Create a structure Node that contains:
o data: To hold the integer value.
o next: A pointer to the next node.
o prev: A pointer to the previous node.
o position: To track the position of the node (though unused in
this code).
Step 2: Define Function doublyLinkedListTraversal
1. Input: Pointer to the head node of the doubly linked list.
2. Process:
o Start from the head and print each node's data value while
moving to the next node.
3. Output: The data values of the nodes in the list.
Step 3: Define Function insertAtFirst
1. Input:
o head: The pointer to the head node.
o data: The integer to be inserted.
2. Process:
o Create a new node with data.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

o Set the prev pointer of the new node to NULL.


o Link the new node to the old head by setting its next to the
current head.
o If the head is not NULL, set the old head’s prev pointer to
the new node.
o Update the head to point to the new node.
3. Output: The updated head pointer.
Step 4: Define Function getLength
1. Input: Pointer to the head of the doubly linked list.
2. Process:
o Use recursion to count the number of nodes in the list.
3. Output: Return the length of the linked list.
Step 5: Define Function deleteAtStart
1. Input: Pointer to the head of the doubly linked list.
2. Process:
o If the list is empty, return NULL.
o Set the head to the next node.
o If the new head is not NULL, set its prev pointer to NULL.
o Free the memory of the old head node.
3. Output: Return the updated head pointer.
Step 6: Define Function deleteright
1. Input:
o head: The pointer to the head node.
o position: The position of the node to be deleted.
2. Process:
o If the position is 0, call deleteAtStart.
o Traverse the list until the node at the given position is found.
o If the position is invalid (out of range), print an error
message.
o Unlink the node by adjusting the prev and next pointers of its
neighbors.
o Free the memory of the node.
3. Output: Return the updated head pointer.
Step 7: Define Function menu
• Display the menu options:
1. Insert at node.
2. Find the Length.
3. Delete the start node.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

4. Delete node at a specific position.


5. Display the list.
6. Exit.
Step 8: Main Program Execution
1. Initialize the head of the doubly linked list as NULL.
2. Display the menu.
3. Use a loop to handle user input for the following operations:
o Insert at Node: Call insertAtFirst.
o Find the Length: Call getLength and print the result.
o Delete: Call deleteAtStart to delete the head node.
o Delete Right Node: Call deleteright to delete a node at a
specific position.
o Display List: Call doublyLinkedListTraversal to display the
list.
o Exit: Terminate the program when the user selects 6.
4. Continue until the user chooses to exit.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

PROBLEM
SOLVING ON THE
CONCEPT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

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

struct Node{
int data;
struct Node* next;
struct Node* prev;
int position;
};

void doublyLinkedListTraversal(struct Node* head) {


struct Node *ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

struct Node* insertAtFirst(struct Node *head,int data){


struct Node* ptr=(struct Node*)malloc(sizeof(struct Node));
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

ptr->data = data;
ptr->prev = NULL;
ptr->next = head;
if (head != NULL) {
head->prev = ptr;
}
return ptr;
}
int getLength(struct Node* head) {
if (head == NULL)
return 0;
return 1 + getLength(head->next);
}

struct Node* deleteAtStart(struct Node* head) {


if (head == NULL) {
return NULL;
}

struct Node* ptr = head;


head = head->next;

if (head != NULL) {
head->prev = NULL;
}

free(ptr);
return head;
}

struct Node* deleteright(struct Node* head, int position) {


if (head == NULL) {
return NULL;
}
if (position == 0){
return deleteAtStart(head);
}
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

struct Node* temp = head;


int index = 0;
while (temp != NULL && index < position) {
temp = temp->next;
index++;
}
if (temp == NULL) {
printf("Position out of range\n");
return head;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}

free(temp);
return head;
}

void menu(){
printf("1,Insert at node\n");
printf("2.Find the Length\n");
printf("3.delete\n");
printf("4.Delete right node\n");
printf("5.display\n");
printf("6.Exit\n");
}
int main(){
struct Node* head=NULL;
int choice;
int data;
int position;

menu();

do{
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

printf("\nEnter your choice:");


scanf("%d",&choice);

switch(choice){

case 1:
printf("Enter Node:");
scanf("%d",&data);
head = insertAtFirst(head,data);
break;

case 2:
printf("Length of Node is:");
printf("%d\n",getLength(head));
break;

case 3:
printf("Deleted");
head=deleteAtStart(head);
break;

case 4:
printf("Enter position:");
scanf("%d",&position);
printf("Right Deleted");
head=deleteright(head,position);
break;

case 5:
printf("Linked List:");
doublyLinkedListTraversal(head);
break;

case 6:
printf("Exiting...");
break;

default:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

printf("Invalid choice\n");
}

}while(choice != 6);

return 0;
}

OUTPUT SCREENSHOT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

CONCLUSION: In this experiment, we successfully implemented and worked with a doubly


linked list. The key operations performed were inserting nodes at the
beginning, deleting nodes from the start and from specific positions, and
traversing the list to display the data. We also calculated the length of the
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

linked list dynamically as nodes were added or removed.


The doubly linked list provided flexibility for efficient bidirectional
traversal, which simplified the deletion process from any position compared
to singly linked lists. However, the use of extra memory for the prev
pointers and handling multiple pointers during insertions and deletions
added some complexity to the implementation.
Overall, this experiment demonstrated the practical benefits of a doubly
linked list in situations where both forward and backward traversal is
needed, along with dynamic memory management.

You might also like