Class Definition in C++

In this sample assignment solution; a C++ class definition is given for an abstract data type. It is Linked List of string objects. Here the expert is demonstrating to students how to implement each member function in the class given. Any private data members or private member functions can be added to this class.

SOLUTION : –

#include “LinkedList.h”

#include <assert.h>

int main(){

LinkedList ls;

ls.insertToFront(“Steve”);

ls.insertToFront(“Judy”);

ls.insertToFront(“Laura”);

ls.insertToFront(“Eddie”);

ls.insertToFront(“Hariette”);

ls.insertToFront(“Carl”);

for (int k = 0; k < ls.size(); k++){

string x;

ls.get(k, x);

cout << x << endl;

}

LinkedList ls1;

ls1.insertToFront(“Eric”);

ls1.insertToFront(“Shawn”);

ls1.insertToFront(“Topanga”);

ls1.insertToFront(“Cory”);

ls1.printList();

ls1.printReverse();

LinkedList e1;

e1.insertToFront(“bell”);

e1.insertToFront(“biv”);

e1.insertToFront(“devoe”);

LinkedList e2;

e2.insertToFront(“Andre”);

e2.insertToFront(“Big Boi”);

e1.printList(); e2.printList();

e1.append(e2); // adds contents of e2 to the end of e1

e1.printList();

string s;

assert(e1.size() == 5 && e1.get(3, s) && s == “Big Boi”);

assert(e2.size() == 2 && e2.get(1, s) && s == “Andre”);

// reverseList function

LinkedList e3;

e3.insertToFront(“Sam”);

e3.insertToFront(“Carla”);

e3.insertToFront(“Cliff”);

e3.insertToFront(“Norm”);

e3.printList();

e3.reverseList(); // reverses the contents of e1

e3.printList();

assert(e3.size() == 4 && e3.get(0, s) && s == “Sam”);

// Swap function

LinkedList e4;

e4.insertToFront(“A”);

e4.insertToFront(“B”);

e4.insertToFront(“C”);

e4.insertToFront(“D”);

LinkedList e5;

e5.insertToFront(“X”);

e5.insertToFront(“Y”);

e5.insertToFront(“Z”);

e4.printList(); e5.printList();

e4.swap(e5); // exchange contents of e4 and e5

e4.printList(); e5.printList();

assert(e4.size() == 3 && e4.get(0, s) && s == “Z”);

assert(e5.size() == 4 && e5.get(2, s) && s == “B”);

// Copy onstructor

LinkedList e6 = ls;

// assignment operator

e4 = e5;

e4.printList();

e5.printList();

return 0;

}

#include “LinkedList.h”

/**

* Function to insert the given val at the beginning of the linkedlist

* idea: the val would be present at the head on insertion

* Dynamically allocate a node(tmp here) and make it head

* the next of new head should point to old head( tmp->next = head; head = tmp;)

*/

void LinkedList::insertToFront(const ItemType &val){

Node *tmp = new Node();

tmp->next = this->head;

tmp->value = val;

this->head = tmp;

this->length++;

}

/**

* Function to print the Linkedlist from head to end

* travese the linked list nodes one by one and print

* tmp is the pointer used to traverse the linkedlist

*/

void LinkedList::printList() const{

Node *tmp = this->head;

while(tmp != NULL){

cout << tmp->value << ” “;

tmp = tmp->next;

}

cout << endl;

}

/**

* Function that copies value at node at node i(0-indexing) to item

* to get value at node i, traverse i nodes in the linkedlist

* Copy value to item, return true.

* returns false if node i is not present

*/

bool LinkedList::get(int i, ItemType& item) const{

Node *tmp = this->head;

if (i >= this->length)

return false;

while(i > 0){

tmp = tmp->next;

i–;

}

item = tmp->value;

return true;

}

/**

* see https://www.geeksforgeeks.org/reverse-a-linked-list/ for clear explantion

* We basically traverse the linked list while reversing pointers

*/

void LinkedList::reverseList(){

Node *next, *prev, *curr;

prev = NULL;

next = NULL;

curr = this->head;

while(curr != NULL){

next = curr->next;

curr->next = prev;

prev = curr;

curr = next;

}

this->head = prev;

}

LinkedList::~LinkedList(){

Node *curr, *next;

curr = this->head;

next = NULL;

while(curr != NULL){

next = curr->next;

delete curr;

curr = next;

}

this->head = NULL;

}

/**

* Use append here to create list

* Append rhs to empy list to create copy

*/

LinkedList::LinkedList(const LinkedList& rhs){

head = NULL;

length = 0;

Node *node = rhs.head;

append(rhs);

}

const LinkedList& LinkedList::operator=(const LinkedList& rhs){

LinkedList temp(rhs);           // Use copy constructor to create a dummy object

std::swap(temp.head, head);     // Swap head and length

std::swap(temp.length, length);

return *this;

}

/**

* dummy function to facilitate recursion to print linkedlist in reverse

* This function calls itself on the next node until the last node calls on NULL

* then it returns, then every call returns after printing its value

* This results in  reverse printing

* eg : 1->2->3 is the LinkedList

* calls would be printReverseRec(1)->pRR(2)->pRR(3)->pRR(NULL)

* Last call returns then print(3) happens and pRR(3) returns

* then print(2) happens and pRR(2) returns

* then print(1) and pRR(1) returns

* Result – 3 2 1

* Hope this is clear.

*/

void LinkedList::printReverseRec(const Node *t) const{

if(t == NULL)             // Return if t is NULL

return;

printReverseRec(t->next); // Call on next node

cout << t->value  << ” “; // Print before exiting

}

/**

* Function that prints Linkedlist in reverse

* Uses a dummy printReverseRec function to do this

*/

void LinkedList::printReverse() const{

printReverseRec(this->head);   // Starts reverse printing from head

cout << endl;

}

/**

* Function that appends LL other to our LL

*/

void LinkedList::append(const LinkedList &other){

Node *tmp = this->head;

while(tmp != NULL and tmp->next != NULL)

tmp = tmp->next;            // points to the last non-NULL node, null iff LL is empty

Node *tmp2 = other.head;        // tmp2 points to head of second LL

if(tmp2 == NULL)

return;

if(tmp == NULL){                // if LL is empty, copy first node of second list and advance tmp2

tmp = new Node();          // This step is to handle when first LL is null

this->head = tmp;           // Not required otherwise

tmp->next = NULL;

tmp->value = tmp2->value;

tmp2 = tmp2->next;

this->length++;

}

while(tmp2 != NULL){            // Starting from node pointed by tmp2

Node *t = new Node();

t->next = NULL;

t->value = tmp2->value;

tmp->next = t;     // Copy each node one by one to first LL

tmp = tmp->next;            // We have to dynamically allocate each node since we are appending

tmp2 = tmp2->next;

this->length++;

}

}

/**

* Function that swaps two linked list

* To swap a LL object just swap their heads and other private data(Here lenngth that we added)

*/

void LinkedList::swap(LinkedList &other){

// Node *head1 = this->head;

// this->head = other.head;

// other.head = head1;

// int t = this->length;

// this->length = other.length;

// other.length = t;

// Alternate code using copy constructors

LinkedList temp = other;

other = *this;

*this = temp;

}

/**

* Function that returns length of LL

* Just returns length variable we stored

*/

int LinkedList::size() const{

return this->length;

}