C++ Program to create and display doubly linked list (DLL)
Introduction
From this article we are going to implement doubly linked list using C++ programming language.
In this article we are going to Create and Display the doubly linked list.
To create doubly linked list , the steps are as follows :
Graphical representation of DLL:
Step 1: Declare a structure named node. In this structure declare one integer data and two pointers : next and prev.
Graphical representation of DLL:
Step 1: Declare a structure named node. In this structure declare one integer data and two pointers : next and prev.
struct node {
int data;
node *next;
node *prev;
};
int data;
node *next;
node *prev;
};
Step 2 : Declare a pointer that stores the address of first node of linked list. Name it as head .
Initialize it as head = NULL;
Step 3: Create a new node by using new keyword as follows :
node *newNode = new node;
newNode->data = 10;
newNode->next = NULL;
newNode->prev = NULL;
newNode->data = 10;
newNode->next = NULL;
newNode->prev = NULL;
Step 4: If head ==NULL , assign address of newNode to head .
head = newNode;
Step 5 : Else if head is not NULL , then traverse the linked list till end. Link the next pointer of last node to the newNode and prev pointer of newNode to the last node of list.
node *temp = head;
while( temp->next !=NULL )
{
temp = temp->next;
}
newNode ->prev = temp;
Step 6: By repeating steps through 3 to 5 , you can add as much node as you can.
Display the Doubly Linked List
Steps to display the dll in forward direction .
Step 1 : Create a copy of head pointer , temp and initialize it as temp = head.
Step 2 : Till reach to the end of linked list , make temp = temp->next and print the output.
node *temp = head;
while ( temp !=NULL )
{
cout << temp ->data ;
temp = temp ->next;
} C++ program to Create and Display the linked list
Download the Code here
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
node *head = NULL;
void create( int val);
void display();
int main()
{
int choise,input,n;
do
{
cout<<"\n\t\t________________Menu_____________________\n\n";
cout<<"\t\t 1.Create Doubly Linked List \n";
cout<<"\t\t 2.Display Doubly Linked List\n";
cout<<"\t\t 3.Exit \n";
cout<<" Your Choise : ";
cin>>choise;
switch(choise)
{
case 1:
{
cout<<"How many nodes do you want to insert ? ";
cin>>n;
for (int i = 0; i < n; i++)
{
cout<<"Enter the data : ";
cin>>input;
create(input);
}
}
break;
case 2:
{
cout<<"Displaying the Doubly Linked List \n";
display();
}
break;
}
}while(choise<3);
}
void create(int val)
{
node *newNode = new node;
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
if (head ==NULL)
{
head = newNode;
}
else
{
node *temp = head;
while(temp->next!=NULL)
temp = temp->next;
temp ->next = newNode;
newNode->prev = temp;
}
}
void display()
{
node *temp = head;
while(temp!=NULL)
{
cout<<"\t"<<temp->data<<endl;
temp = temp->next;
}
}
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
node *head = NULL;
void create( int val);
void display();
int main()
{
int choise,input,n;
do
{
cout<<"\n\t\t________________Menu_____________________\n\n";
cout<<"\t\t 1.Create Doubly Linked List \n";
cout<<"\t\t 2.Display Doubly Linked List\n";
cout<<"\t\t 3.Exit \n";
cout<<" Your Choise : ";
cin>>choise;
switch(choise)
{
case 1:
{
cout<<"How many nodes do you want to insert ? ";
cin>>n;
for (int i = 0; i < n; i++)
{
cout<<"Enter the data : ";
cin>>input;
create(input);
}
}
break;
case 2:
{
cout<<"Displaying the Doubly Linked List \n";
display();
}
break;
}
}while(choise<3);
}
void create(int val)
{
node *newNode = new node;
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
if (head ==NULL)
{
head = newNode;
}
else
{
node *temp = head;
while(temp->next!=NULL)
temp = temp->next;
temp ->next = newNode;
newNode->prev = temp;
}
}
void display()
{
node *temp = head;
while(temp!=NULL)
{
cout<<"\t"<<temp->data<<endl;
temp = temp->next;
}
}
Note : This code is successfully compiled and run under Linux distribution Ubuntu 18.04 LTS environment .
Output :
________________Menu_____________________
1.Create Doubly Linked List
2.Display Doubly Linked List
3.Exit
Your Choise : 1
How many nodes do you want to insert ? 4
Enter the data : 10
Enter the data : 20
Enter the data : 30
Enter the data : 40
________________Menu_____________________
1.Create Doubly Linked List
2.Display Doubly Linked List
3.Exit
Your Choise : 2
Displaying the Doubly Linked List
10
20
30
40
________________Menu_____________________
1.Create Doubly Linked List
2.Display Doubly Linked List
3.Exit
Your Choise : 3
Conclusion
Doubly Linked List is successfully created and displayed using C++.
0 Comments