8.18 Programming 8: LinkedList n this programming assignment you will reuse code
ID: 3571688 • Letter: 8
Question
8.18 Programming 8: LinkedList n this programming assignment you will reuse code from lab 9 for working with a Linked List. Node class represents a single element of the linked list. class Node public ode Node* prev; string key ode ext LinkedList class represent one entire linked list. class LinkedList f public Linked List(); void Insert (string key); Adds new node to head of list Prints list with "List: on first line void Print(); Node* Find(string key); Returns pointer to node with key equal to parameter key or 0 if not found Node head Code to insert strings into linked list and print list. while (true getline (cin, line); if (line empty break 1. Insert (line l Print() With input Test1 Test 2 Test 3Explanation / Answer
below is the code :
#include <iostream>
#include <string>
using namespace std;
#include "node.cpp"
class LinkedList
{
public:
Node* head;
LinkedList()
{
head = NULL;
}
void Insert(string key)
{
Node* new_node;
new_node->key = key;
new_node->next = head;
new_node->prev = NULL;
head = new_node;
}
void Print()
{
Node* new_node = head;
while (new_node != NULL)
{
cout << " %s"<<new_node->key;
new_node = new_node->next;
}
}
Node* Find(string key)
{
Node* current = head;
while (current != NULL)
{
if (current->key == key)
{
return current;
}
current = current->next;
}
return 0;
}
};
int main()
{
LinkedList head;
string line;
cout << "Enter linked list's strings :";
while (true)
{
getline(cin,line);
if(line.empty())
{
break;
}
head.Insert(line);
}
cout << " The Linked list is : ";
head.Print();
Node* nTest;
nTest = head.Find("Test2");
if (nTest == 0)
{
cout << " Error! node not found!";
}
else if(nTest->key != "Test2")
{
cout << " Error! node key doesnot equal Test2";
}
else
{
cout << " Node is found successfully";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.