C++: Implement the unfinished methods of SLL class. Modify the methods in SLL.cp
ID: 3709946 • Letter: C
Question
C++:
Implement the unfinished methods of SLL class. Modify the methods in SLL.cpp, do NOT change anything in node.h file. SLL must be class TEMPLATE. Use placeholder wherever you do not want to use fixed data type.
SLL.h:
#include <iostream>
#include "node.h"
using namespace std;
template <class U>
class SLL {
Node<U> * headPtr;
int size;
public:
// default constructor
SLL(){
//implement this method
}
// destructor
~SLL(){
// implement this method
}
Node<U>* getHeadPtr(){
return headPtr;
}
// insert (item1, item2) to the list
void insert(U item1, U item2){
//implement this method
}
// if find the item1 value, return the pointer to the node
// otherwise, return nullptr
Node<U>* search(U item1){
//implement this method
}
// remove the node with key value: item1
bool remove(U item1){
//implement this method
}
int getSize(){
return size;
}
// display the SSN values of each node in the linked list
void display(){
Node<U>* temp;
temp = headPtr;
while (temp!= nullptr) {
cout << temp->SSN << endl;
temp = temp->next;
}
}
};
node.h:
#include <iostream>
using namespace std;
template <class T>
struct Node{
T SSN;
T name;
Node<T>* next;
};
Example of one of the files:
i 586412373 NICOLA EVANGELISTA
i 177228167 MEAGAN LEKBERG
i 586412373 JEFF DUTTER
i 760846483 KITTY MANZANERO
i 061899135 CATHERIN MCCREIGHT
i 087300880 CARMA KULHANEK
i 177264549 VALERY KOSAKOWSKI
i 210044984 SHEILAH MONGES
d 760846483 KITTY MANZANERO
r 760846483 KITTY MANZANERO
r 007980295 DELPHIA SIMISON
i 493515916 VERONIKA TADENA
d 401991909 MCKINLEY WESTERFELD
i 793267575 TEMIKA MESHEW
i 319373939 MARGIT EBLIN
Explanation / Answer
Given below is the completed code for the question. Please use it with your driver program.
Please do rate the answer if it was helpful. Thank you
SLL.h
--------
#include <iostream>
#include "node.h"
using namespace std;
template <class U>
class SLL {
Node<U> * headPtr;
int size;
public:
// default constructor
SLL(){
headPtr = nullptr;
size = 0;
}
// destructor
~SLL(){
Node<U> *next;
while(headPtr != nullptr)
{
next = headPtr->next;
delete headPtr;
headPtr = next;
}
}
Node<U>* getHeadPtr(){
return headPtr;
}
// insert (item1, item2) to the list
void insert(U item1, U item2){
Node<U> *n = new Node<U>();
n->SSN = item1;
n->name = item2;
n->next = nullptr;
if(headPtr == nullptr)
headPtr = n;
else
{
Node<U> last = headPtr;
while(last->next != nullptr)
last = last->next;
last->next = n;
}
size++;
}
// if find the item1 value, return the pointer to the node
// otherwise, return nullptr
Node<U>* search(U item1){
Node<U> *curr = headPtr;
while(curr != nullptr)
{
if(curr->item1 == SSN)
return curr;
curr = curr->next;
}
return nullptr;//not found
}
// remove the node with key value: item1
bool remove(U item1){
Node<U> *prev = nullptr, *curr = headPtr;
while(curr != nullptr)
{
if(curr->SSN == item1)
break;
prev = curr;
curr = curr->next;
}
if(curr == nullptr) //not found
return false;
if(curr == headPtr) //remove head node?
headPtr = curr->next;
else
prev->next = curr->next;
delete curr;
size--;
return true;
}
int getSize(){
return size;
}
// display the SSN values of each node in the linked list
void display(){
Node<U>* temp;
temp = headPtr;
while (temp!= nullptr) {
cout << temp->SSN << endl;
temp = temp->next;
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.