C++ Linked List Exercises: Create and display a doubly linked list
C++ Linked List: Exercise-15 with Solution
Write a C++ program to create and display a doubly linked list.
Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Sample Solution:
C++ Code:
#include <iostream>
using namespace std;
// A doubly linked list node
struct Node {
string data;
struct Node* next;
struct Node* prev;
};
//Append node at the front of the list
void append_data(Node** head, string new_data)
{
//Create a new node and allocate memory.
struct Node* newNode = new Node;
//assign data to new node
newNode->data = new_data;
// A new node has been added with the name head and the previous node
// set to null, since it is being added at the front.
newNode->next = (*head);
newNode->prev = NULL;
//previous head is new node
if ((*head) != NULL)
(*head)->prev = newNode;
//head points to new node
(*head) = newNode;
}
// Following function display contents of the doubly linked list
void displayDlList(Node* head)
{
Node* last_node;
cout << "\n\nTraversal in Forward direction:\n";
while (head != NULL) {
cout << " " << head->data << " ";
last_node = head;
head = head->next;
}
cout << "\n\nTraversal in Reverse direction:\n";
while (last_node != NULL) {
cout << " " << last_node->data << " ";
last_node = last_node->prev;
}
}
//main program
int main() {
/* Start with the empty list */
struct Node* head = NULL;
append_data(&head, "Red");
append_data(&head, "Green");
append_data(&head, "White");
append_data(&head, "Orange");
cout<<"Doubly linked list is as follows: ";
displayDlList(head);
return 0;
}
Sample Output:
Doubly linked list is as follows: Traversal in Forward direction: Orange White Green Red Traversal in Reverse direction: Red Green White Orange
Flowchart:


CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Kth node from the Middle towards Head of a Linked List.
Next C++ Exercise: Reverse doubly linked list.
What is the difficulty level of this exercise?
C++ Programming: Tips of the Day
In C++, if throw is an expression, what is its type?
According to the standard, 5.16 paragraph 2 first point, "The second or the third operand (but not both) is a throw-expression (15.1); the result is of the type of the other and is an rvalue." Therefore, the conditional operator doesn't care what type a throw-expression is, but will just use the other type.
In fact, 15.1, paragraph 1 says explicitly "A throw-expression is of type void."
Ref: https://bit.ly/3e2l8FL
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook