ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Data structure tutorial 4: Doubly Linked List [DLL] with explanation with implementation in C++

prodevelopertutorial May 19, 2019

In the previous chapter we learnt about single linked list. In this chapter we shall learn about Doubly Linked List.

Doubly Linked List is a special data structure, which is a collection of zero or more nodes.

Each node is made up of 3 parts, “prev_link + data + next_link”. As it is a Doubly linked list, we need to store the data for the previous node and the next node.

prev_link: It is used to hold the information of the previous node.

Data: It is used to store some value. It can be an Integer variable or a structure.

next_link: It is used to hold the information of the next node.

Together with “prev_link + data + next_link” is called as a node.

Below is how we represent a doubly LL node:

Doubly Linked List [DLL] with explanation with implementation in C++

A DLL with multiple nodes can be shown below:

Doubly Linked List [DLL] with explanation with implementation in C++

 

From the above image we can infer following below points:

  1. First node prev_link will always be null
  2. Last node next_link will always be null
  3. Data section holds the data
  4. Head pointer always point to the first node

Below are the operations that we will be learning in DLL

  1. Insert element in rear.
  2. Delete element with given key value.
  3. Search an element.
  4. Display all the data.

1. Insert element in rear.

While inserting element at the rear, we need to check 2 conditions.

  1. If the head node is NULL

If the head node is NULL, it means that the element we are trying to insert is the first element. Hence we do below steps:

  1. Copy the value to the data section
  2. Make prev_link and next_link pointers point to NULL.

Doubly Linked List [DLL] with explanation with implementation in C++

2. If the head node is not NULL

 

If the head element is not NULL, it means that there are already elements present. Hence we do the following steps to insert at the rear.

  1. Take a new node and copy the value.
  2. Take a temp pointer, traverse till the end of the list.
  3. Make the prev pointer of new node to point to temp node
  4. Make the next pointer of the new node to point to NULL.

Doubly Linked List [DLL] with explanation with implementation in C++

2. Delete element with given key value.

To delete we follow below steps:

  1. Take a temp pointer move to the point till you find the element equal to the key.
  2. If the element is the lest element, then make the previous element’s next pointer to NULL.
  3. Else, do the following operations:
    1. temp->prev->next = temp->next;
    2. temp->next->prev = temp->prev;
  4. Delete temp node.

3. Search an element.

To search an element, we do below steps:

  1. Take a temp pointer pointing to head
  2. Check if the data is same as key
  3. If it is not, move the temp pointer to next node
  4. Do step 2 and 3 till you find the key.

4. Display all the data.

To Display, we do below steps:

  1. Take a temp pointer pointing to head
  2. Display the data
  3. Move the temp pointer to next node
  4. Do step 2 and 3 till you reach the end of LL.

 

Doubly Linked List implementation using C++

 #include<iostream>
using namespace std;

struct Node
{
	int val;
	Node *prev;
	Node *next;
};

Node *head;

void insert_rear(int value)
{
	Node *temp = head;
	if (head != NULL)
	{
		// if head is not null
		while(temp->next != NULL)
		{
			temp = temp ->next;
		}
		Node *newNode = new Node;
		temp->next = newNode;
		newNode->prev = temp;
		newNode->val = value;
		newNode->next = NULL;
	}
	else
	{
		//if head is nul
		Node *newNode = new Node;
		newNode->val = value;
		newNode->prev = NULL;
		newNode->next = NULL;
		head = newNode;
	}
}

void remove(int x)
{
	Node *temp = head;
	while(temp->val != x)
	{
		temp = temp->next;
	}
	if(temp -> next == NULL)
	{
		temp->prev->next = NULL;
	}
	else
	{
		temp->prev->next = temp->next;
		temp->next->prev = temp->prev;
	}	
	delete temp;
}

void search(int x)
{
	Node *temp = head;
	int found = 0;
	while(temp != NULL)
	{
		if(temp->val == x)
		{
			cout<<"\nFound";
			found = 1;
			break;
		}
		temp = temp->next;
	}
	if(found==0)
	{
		cout<<"\nNot Found";
	}
}

void display()
{
	Node *temp =head;
	while(temp !=NULL)
	{
		cout<< temp->val<<"\t";
		temp = temp->next;
	}

}


int main()
{
	int choice, x;
	do
	{
		cout<<"\n1. Insert";
		cout<<"\n2. Delete";
		cout<<"\n3. Search";
		cout<<"\n4. Display";
		cout<<"\n5. Exit";
		cout<<"\n\nEnter you choice : ";
		cin>>choice; 
		switch (choice)
		{
			case 1 : 	cout<<"\nEnter the element to be inserted at rear : ";
					 	cin>>x;;
					 	insert_rear(x);	
					 	break;

			case 2 : 	cout<<"\nEnter the element to be removed : ";
						cin>>x;
						remove(x); 	
						break;

			case 3 : 	cout<<"\nEnter the element to be searched : ";
						cin>>x;
						search(x); 	
						break;

			case 4 : 	display();		
						break;

		}
	}
	while(choice != 5);

	return 0;
}

Output:

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 1

Enter the element to be inserted at rear : 1

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 1

Enter the element to be inserted at rear : 2

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 1

Enter the element to be inserted at rear : 3

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 1

Enter the element to be inserted at rear : 4

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 4
1	2	3	4
1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 3

Enter the element to be searched : 3

Found
1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 2

Enter the element to be removed : 3

1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 4
1	2	4
1. Insert
2. Delete
3. Search
4. Display
5. Exit

Enter you choice : 5

 

Further Reading:

AJ’s definitive guide for DS and Algorithms. Click here to study the complete list of algorithm and data structure tutorial. 85+ chapters to study from.

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT