Question: Write a linked list template class in C++ called LinkedList. The class has to implement the following public member functions, where T is the template

Write a linked list template class in C++ called LinkedList. The class has to implement the following public member functions, where T is the template parameter.

LinkedList(); // creates an empty list

~LinkedList(); // deletes all allocated memory

void insert(const T value); // adds a value to the list

bool remove(const T value); // removes a value from the list

bool contains(const T value) const; // tests if value is in the list

size_t length() const; // returns the number of list elements

void print_forwards() const; // prints the list from head to tail

void print_backwards() const; // prints the list from tail to head

T getLast();// find last element recursively

This is the test.cpp:

#include

#include

#include

#include "linkedlist.h"

int main(){

LinkedList ll;

for (int i = 0; i < 10; i++) {

ll.insert(i);

}

cout << ll.length() << endl;

ll.print_forwards();

ll.print_backwards();

 cout << "rm 5 " << ll.remove(5) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); 
 cout << "rm 0 " << ll.remove(0) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "rm 9 " << ll.remove(9) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); for (int i = 0; i < 10; i++) { cout << "rm " << i << " " << ll.remove(i) << endl; } cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); for (int i = 9; i >= 0; i--) { ll.insert(i); } cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "add -1 " << endl; ll.insert(-1); cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "add 100 " << endl; ll.insert(100); cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "add 4 " << endl; ll.insert(4); cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "rm 4 " << ll.remove(4) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "rm 4 " << ll.remove(4) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "rm 4 " << ll.remove(4) << endl; cout << ll.length() << endl; ll.print_forwards(); ll.print_backwards(); cout << "last elem "<< ll.getLast() < *pll = new LinkedList; for (int i = 0; i < 10000; i++) { pll->insert(i*17.4); } delete pll; pll = NULL; }

THIS is what I have so far:

#ifndef LINKEDLIST_H #define LINKEDLIST_H #include  #include  #include  using namespace std; template  class LinkedList { public: LinkedList();// creates an empty list ~LinkedList(); // deletes all allocated memory void insert(const T value); // adds a value to the list bool remove(const T value); // removes a value from the list bool contains(const T value) const; // tests if value is in the list size_t length() const; // returns the number of list elements void print_forwards() const; // prints the list from head to tail void print_backwards() const; // prints the list from tail to head T getLast();// find last element recursively private: struct Node { T data; Node *prev; Node *next; }; Node *head; Node *tail; }; template LinkedList::~LinkedList() { } template LinkedList::LinkedList() { } template void LinkedList::insert(const T value) { } template bool LinkedList::remove(const T value) { return false; } template bool LinkedList::contains(const T value) const { return false; } template size_t LinkedList::length() const { return 0; } template void LinkedList::print_forwards() const { } template void LinkedList::print_backwards() const { } template T LinkedList::getLast() { return nullptr; } #endif //LINKEDLIST_H 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!