w3resource

C++ Linked List Exercises: Insert a new node at the end of a Doubly Linked List

C++ Linked List: Exercise-19 with Solution

Write a C++ program to insert a new node at the end of 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
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Orange White Green Red Pink
Traversal in Reverse direction: Pink 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;
}      
void insert_Node_at_end(Node** head, string color)
{
    Node* new_node = new Node();
    Node* last_node = *head;  
    new_node->data = color;
    new_node->next = NULL;
    if (*head == NULL) {
        new_node->prev = NULL;
        *head = new_node;
        return;
    }
    while (last_node->next != NULL)
        last_node = last_node->next;
    last_node->next = new_node;
    new_node->prev = last_node;
    return;
}
 
// Following function display contents of the doubly linked list
void displayDlList(Node* head)
{
    Node* last_node;
    cout << "\nTraversal in Forward direction: ";
    while (head != NULL) {
        cout << " " << head->data << " ";
        last_node = head;
        head = head->next;
    }
    cout << "\nTraversal in Reverse direction: ";
    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: ";  
   cout<<"\n------------------------------";
   displayDlList(head);
   cout<<"\n\nInsert a new node at the end of a Doubly Linked List:";
   cout<<"\n-------------------------------------------------------------";
   insert_Node_at_end(&head, "Pink");
   displayDlList(head);
   cout<<endl;
   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

Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction:  Orange  White  Green  Red  Pink
Traversal in Reverse direction:  Pink  Red  Green  White  Orange

Flowchart:

Flowchart: Insert a new node at the end of a Doubly Linked List.
Flowchart: Insert a new node at the end of a Doubly Linked List.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Insert a node at the beginning of a Doubly Linked List.
Next C++ Exercise: Middle element of a Doubly Linked List.

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter

C++ Programming: Tips of the Day

How to replace all occurrences of a character in string?

std::string doesn't contain such function but you could use stand-alone replace function from algorithm header.

#include <algorithm>
#include <string>

void some_func() {
  std::string s = "example string";
  std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
}

Ref: https://bit.ly/3niU71x