Write a C++ program to create a linked list of nodes of integers. The list, at i
ID: 3904457 • Letter: W
Question
Write a C++ program to create a linked list of nodes of integers. The list, at its time of construction, contains head and tail nodes. Each new node added to the list, at a random position between the head and tail, contains a pointer to the following node and the tail node. The list has the following methods, namely
• Constructor that constructs a new list, creating head and tail nodes with the values 0 and 1, respectively
• Destructor that destroys the list, with the exception of the head and tail nodes.
• Function to add a new node to the list •
Function to print the list, printing the integer of each node, with each node between the head and tail also printing the integer in the tail node using its pointer to the tail node
The main function is to contain the following sequence of operations, namely
• Create a list L • Add a node with the value 3 to L • Print L
• Add a node with the value -7 to L
• Print L
• Add a node with the value 12 to L
• Print L
• Add a node with the value 9 to L
• Print L
• Add a node with the value -5 to L
• Print L
Explanation / Answer
ScreenShot
----------------------------------------------------
Program
/*Thisprogram to set head and tail of a linked list 0 an1
add new values in between
Then print list after each addition */
//Header file for I/O
#include <iostream>
using namespace std;
//Node creating struct
struct node
{
int data;
node *next;
};
//Linkedlist class
class linked_list
{
//head and tail nodes declared as private to keep safe
private:
node * head, *tail;
//Member methods
public:
//Constructor set head as 0 value and tail as one value
linked_list()
{
head = new node;
tail = new node;
head->data = 0;
head->next = tail;
tail->data = 1;
tail->next = NULL;
}
//Add new nodes in the list
void add_node(int n)
{
//node to link last and new
node *last = new node;
last->data = n;
last->next = tail;
node *tmp = new node;
//if the node is first node other than head and tail add it after head and before tail
if (head->next == tail)
{
head->next = tmp;
tmp->data = n;
tmp->next = tail;
}
//Otherwise add after the previous node and before tail node
else
{
int randPos = rand();
tmp = head;
// Uses tmp to find the last node
while (tmp->next != tail) {
tmp = tmp->next;
}
// Appends the last node with last
tmp->next = last;
}
}
//Print the linked list values
void print()
{
node *tmp;
tmp = head;
while (tmp != NULL)
{
cout << tmp->data;
tmp = tmp->next;
if (tmp != NULL)
cout << "-->";
}
cout << endl;
}
//Destructor
~linked_list(){
node *current = head;
node *next;
while (current != head ||current != tail) {
next = current->next;
delete current;
current = next;
}
}
};
//Main program
int main()
{
//Create object of linkedlist array
linked_list a ;
//Insert and print linked list value
a.add_node(3);
a.print();
a.add_node(-7);
a.print();
a.add_node(12);
a.print();
a.add_node(9);
a.print();
a.add_node(-5);
a.print();
return 0;
}
--------------------------------
Output
0-->3-->1
0-->3-->-7-->1
0-->3-->-7-->12-->1
0-->3-->-7-->12-->9-->1
0-->3-->-7-->12-->9-->-5-->1
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.