Question

In: Computer Science

C++ Need to add the following functions to my templatized class linked list. (main is already...

C++

Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below)

--void destroy_list () deletes each node in the list, and resets the header to nullptr

--bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not.

--bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same key, delete_node deletes the first occurrence. Returns true if delete successful, false if the node was not found.

#ifndef headDER_H_
#define headER_H_
#include <iostream>
using namespace std;


template <class T>
class LL
{
private:
   struct LLnode
   {
       LLnode *fwdPtr;
       T theData;
   };
   LLnode* head;

public:
   void push_front(T);
   void push_back(T);
   void display_list();
   int list_length();
   void destroy_list();
   bool search_list(T);
   LL();
};

template <class T>
void LL<T>::push_front(T str)
{
LLnode *ptr = new LLnode;
ptr->theData=str;
ptr->fwdPtr=head;
head = ptr;
}

template <class T>
void LL<T>::push_back(T str)
{
LLnode *ptr=new LLnode;
ptr->theData=str;
ptr->fwdPtr= nullptr;
if(head == nullptr)
{
head = ptr;
}
else
{
LLnode *temp=head;
while(temp->fwdPtr !=nullptr)
{
temp = temp-> fwdPtr;
}
temp-> fwdPtr=ptr;
}
}

template <class T>
void LL<T>::display_list()
{
int counter = 0;
LLnode *ptr = head;

if (head == nullptr)
cout << "The list is empty" << endl;

while (ptr != nullptr)
{
cout << "node " << counter << " data -> " << ptr->theData << endl;
counter++;
ptr = ptr->fwdPtr;
}
}

template <class T>
int LL<T>::list_length()
{
int counter = 0;
LLnode *ptr = head;

while (ptr != nullptr)
{
counter++;
ptr = ptr->fwdPtr;
}
return counter;
}
template <class T>
LL<T>::LL()
{
   head=nullptr;
}

#endif /* headER_H_ */

#include <iostream>
#include "header.h"
using namespace std;

int main() {

   LL <string> ll1;
   cout << "main: length of empty list - " << ll1.list_length() << endl;
   cout << "main: trying to display empty list 1" << endl;
   ll1.display_list();
   cout << "main: trying to display initial size of ll1 - " << ll1.list_length() << endl;
   ll1.push_front("aaaaa");
   ll1.push_back("bbbbb");
   ll1.push_front("before aaaaa");
   ll1.push_back("after bbbbb");
   cout << "main: length of ll1 after 4 pushes - " << ll1.list_length() << endl;
   cout << "main: now trying to display ll1 after 4 push's" << endl;
   ll1.display_list();
   cout << "main: displaying final size of ll1 - " << ll1.list_length() << endl;
   ll1.destroy_list();
   cout << "main: displaying size of list 1 after destroy - " << ll1.list_length() << endl;
   cout << endl;

   LL <string> ll2;
   ll2.push_front("33333");
   ll2.push_front("22222");
   ll2.push_front("11111");
   ll2.push_back("44444");
   ll2.push_back("55555");
   ll2.push_back("66666");
   cout << "main: now trying to display ll2 after 6 push's" << endl;
   ll2.display_list();
   cout << "main: now searching for node 44444" << endl;
   if (ll2.search_list("44444"))
   {
       cout <<"main: found node 44444" << endl;
   }
   else
   {
       cout << "main: did not find node 44444" << endl;
   }
   cout << "main: now searching for node 44445" << endl;
   if (ll2.search_list("44445"))
   {
       cout <<"main: found node 44445" << endl;
   }
   else
   {
       cout << "main: did not find node 44445" << endl;
   }
   cout << "main: now trying to delete node 44444" << endl;
   if (ll2.delete_node("44444"))
   {
       cout <<"main: node 44444 deleted" << endl;
   }
   else
   {
       cout << "main: did not find 44444 for delete" << endl;
   }
   if (ll2.search_list("44444"))
   {
       cout <<"main: searched for 44444 after delete, found" << endl;
   }
   else
   {
       cout << "main: searched for 44444 after delete, not found" << endl;
   }
   cout << "main: displaying whole list after delete 44444" << endl;
   ll2.display_list();
   cout << "main: now trying to delete node 11111" << endl;
   if (ll2.delete_node("11111"))
   {
       cout <<"main: node 11111 deleted" << endl;
   }
   else
   {
       cout << "main: did not find node 11111 for delete" << endl;
   }
   cout << "main displaying whole list after delete 11111" << endl;
   ll2.display_list();
   ll2.destroy_list();

   return 0;
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Only header.h is attached since it is the only file that is modified.

//updated code for header.h file

#ifndef headDER_H_

#define headER_H_

#include <iostream>

using namespace std;

template <class T>

class LL {

private:

    struct LLnode {

        LLnode* fwdPtr;

        T theData;

    };

    LLnode* head;

public:

    void push_front(T);

    void push_back(T);

    void display_list();

    int list_length();

    LL();

    //prototypes of newly implemented methods

    void destroy_list();

    bool search_list(T);

    bool delete_node (T);

   

};

template <class T>

void LL<T>::push_front(T str)

{

    LLnode* ptr = new LLnode;

    ptr->theData = str;

    ptr->fwdPtr = head;

    head = ptr;

}

template <class T>

void LL<T>::push_back(T str)

{

    LLnode* ptr = new LLnode;

    ptr->theData = str;

    ptr->fwdPtr = nullptr;

    if (head == nullptr) {

        head = ptr;

    }

    else {

        LLnode* temp = head;

        while (temp->fwdPtr != nullptr) {

            temp = temp->fwdPtr;

        }

        temp->fwdPtr = ptr;

    }

}

template <class T>

void LL<T>::display_list()

{

    int counter = 0;

    LLnode* ptr = head;

    if (head == nullptr)

        cout << "The list is empty" << endl;

    while (ptr != nullptr) {

        cout << "node " << counter << " data -> " << ptr->theData << endl;

        counter++;

        ptr = ptr->fwdPtr;

    }

}

template <class T>

int LL<T>::list_length()

{

    int counter = 0;

    LLnode* ptr = head;

    while (ptr != nullptr) {

        counter++;

        ptr = ptr->fwdPtr;

    }

    return counter;

}

template <class T>

LL<T>::LL()

{

    head = nullptr;

}

//implementations of new methods

template <class T>

void LL<T>::destroy_list()

{

                //looping as long as head is not nullptr

    while(head!=nullptr){

                //taking a reference to next of head node

                LLnode *temp=head->fwdPtr;

                //deleting head node

                delete head;

                //assigning next node to head

                head=temp;

                }

}

template <class T>

bool LL<T>::search_list(T key)

{

                //using a for loop, looping through each node in linked list

    for(LLnode* temp=head;temp!=nullptr;temp=temp->fwdPtr){

                //comparing data field with key

                if(temp->theData==key){

                                //found

                                return true;

                                }

                }

                //not found

                return false;

}

template <class T>

bool LL<T>::delete_node(T key)

{

                //if head node is empty, returning false

    if(head==nullptr){

                return false;

                }

                // checking if head node contains the value to be deleted,

                if(head->theData==key){

                                //storing next of head node

                                LLnode* temp=head->fwdPtr;

                                //deleting head node

                                delete head;

                                //assigning next to head node

                                head=temp;

                                //deletion is successful

                                return true;

                }

                //taking a reference of head

                LLnode* temp=head;

                //looping as long as next of temp is not nullptr

                while(temp->fwdPtr!=nullptr){

                                //checking if next node is the node to be deleted

                                if(temp->fwdPtr->theData==key){

                                               //storing next of next node

                                               LLnode* next=temp->fwdPtr->fwdPtr;

                                               //deleting next node

                                               delete temp->fwdPtr;

                                               //storing previously saved next of next node as the next of temp

                                               temp->fwdPtr=next;

                                               return true; //success

                                }

                                //advancing temp

                                temp=temp->fwdPtr;

                }

                return false; //not found

}

#endif /* headER_H_ */

/*OUTPUT of given test code*/

main: length of empty list - 0

main: trying to display empty list 1

The list is empty

main: trying to display initial size of ll1 - 0

main: length of ll1 after 4 pushes - 4

main: now trying to display ll1 after 4 push's

node 0 data -> before aaaaa

node 1 data -> aaaaa

node 2 data -> bbbbb

node 3 data -> after bbbbb

main: displaying final size of ll1 - 4

main: displaying size of list 1 after destroy - 0

main: now trying to display ll2 after 6 push's

node 0 data -> 11111

node 1 data -> 22222

node 2 data -> 33333

node 3 data -> 44444

node 4 data -> 55555

node 5 data -> 66666

main: now searching for node 44444

main: found node 44444

main: now searching for node 44445

main: did not find node 44445

main: now trying to delete node 44444

main: node 44444 deleted

main: searched for 44444 after delete, not found

main: displaying whole list after delete 44444

node 0 data -> 11111

node 1 data -> 22222

node 2 data -> 33333

node 3 data -> 55555

node 4 data -> 66666

main: now trying to delete node 11111

main: node 11111 deleted

main displaying whole list after delete 11111

node 0 data -> 22222

node 1 data -> 33333

node 2 data -> 55555

node 3 data -> 66666


Related Solutions

C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void remove_node(struct linked_list* list, int rm_node_value) (the function to make) This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable number_of_nodes) means the number of nodes that will be removed. When...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Assume the following functions have already been defined, write a main() using c++ that uses them...
Assume the following functions have already been defined, write a main() using c++ that uses them to fill a vector with random integers, remove the smallest and largest integers from the vector, save the result in a file called "trimmed.txt" void fillRandom(vector & nums, int howMany); // fill with specified number of integers int minValue(vector nums); // return smallest value in vector int maxValue(vector <;int> nums); // return largest value in vector void writeFile(string outFileName, vector nums ); // writes...
need to add these functions to my code: • show only important tasks • show all...
need to add these functions to my code: • show only important tasks • show all completed tasks My code in java: import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[]...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT