Question: Hello, I would like some help in completing my code, most of the work is done and all that is left is a rotate function
Hello, I would like some help in completing my code, most of the work is done and all that is left is a rotate function in linearLinkedList.h, deleteNode function in circularLinkedList.h, and another rotate function in circularLinkedList.h.
The problem is:

my code:
linkedNode.h
#ifndef LINKEDNODE_H #define LINKEDNODE_H
template class nodeType{ public: Type info; nodeType *next; nodeType *prev; };
#endif /* LINKEDNODE_H */
----------------------------------------------------------
----------------------------------------------------------
linkedListADT.h
#ifndef LINKEDLISTADT_H #define LINKEDLISTADT_H #include "linkedNode.h"
template class linkedListADT{ public: virtual void initializeList() = 0; virtual void destroyList() = 0; virtual bool isEmptyList() const = 0; virtual void print() const = 0; virtual int length() const = 0; virtual Type front() const = 0; virtual Type back() const = 0; virtual bool search(const Type &seachItem) const = 0; virtual void insertFirst(const Type &newItem) = 0; virtual void insertLast(const Type &newItem) = 0; virtual void deleteNode(const Type &deleteItem) = 0; virtual void rotate() = 0; protected: int count; nodeType *first; nodeType *last; };
#endif /* LINKEDLISTADT_H */
-----------------------------------------------------------
-----------------------------------------------------------
linearLinkedList.h
#ifndef LINEARLINKEDLIST_H #define LINEARLINKEDLIST_H #include #include "linkedListADT.h" using namespace std;
template class linearLinkedList:public linkedListADT{ public: const linearLinkedList &operator= (const linearLinkedList &); //overload = operator
void initializeList(); //initialize list to empty
bool isEmptyList(); //function to see if list is empty
void print() const; //function to print data from each node
int length() const; //function to return # of nodes in list
void destroyList(); //function to delete all nodes
Type front() const; //function to return first element of list
Type back() const; //function to return last element of list bool search(const Type &searchItem) const; //function to search list void insertFirst(const Type &newItem); //function to insert a node beginning of list void insertLast(const Type &newItem); //function to insert a node at end of list void deleteNode(const Type &deleteItem); //function to delete a node from list void rotate() = 0; //function to rotate nodes in list linearLinkedList(); //default constructor linearLinkedList(const linearLinkedList &otherList); //copy constructor
~linearLinkedList(); //destructor private: void copyLinearList(const linearLinkedList *otherList); //function to make a copy of otherList };
template linearLinkedList::linearLinkedList(){ //default constructor this->first = nullptr; this->last = nullptr; this->count = 0; }
template void linearLinkedList::destroyList(){ nodeType *temp; while(this->first != nullptr){ temp = this->first; //assign temp to first this->first = this->first->next; //move first to next node delete temp; //delete temp from memory } this->last = nullptr; this->count = 0; }
template void linearLinkedList::initializeList(){ destroyList(); //if list has nodes, delete them }
template void linearLinkedList::print() const{ nodeType *current; //pointer to move through list current = this->first; while(current != nullptr){ cout info next; } }
template int linearLinkedList::length() const{ return this->count; //end of length }
template Type linearLinkedList::front() const{ assert(this->first != nullptr); return this->first->info; //return info of first node }
template Type linearLinkedList::back() const{ assert(this->last != nullptr); return this->last->info; //return info of last }
template linearLinkedList::~linearLinkedList(){ destroyList(); }//end destructor
//overload assignment operator template const linearLinkedList& linearLinkedList::operator= (const linearLinkedList &otherList){ if(this != &otherList){ //to avoid self-copy copyLinearList(&otherList); } return *this; }
template linearLinkedList::linearLinkedList (const linearLinkedList &otherList){ this->first = nullptr; copyLinearList(otherList); }//end of copy constructor
//create functions for rotate. template bool linearLinkedList::search(const Type &searchItem) const{ nodeType *current; //pointer to move through list bool found = false; current = this->first; //set current to first node in list while(current != nullptr && !found){ //search the list if(current->info == searchItem){ found = true; //found searchItem } else{ current = current->next; //move to next node } }//end of while return found; } //end of search
template void linearLinkedList::insertFirst(const Type &newItem){ nodeType *newNode; //pointer created to new node newNode = new nodeType; //create new node newNode->info = newItem; //store newItem info to newNode newNode->next = this->first; //have newNode as first node this->first = newNode; //move first pointer to first node this->count++; //increment count //if list is empty, newNode is also last node if(this->last == nullptr){ this->last = newNode; } }//end of insertFirst
template void linearLinkedList::insertLast(const Type &newItem){ nodeType *newNode; //pointer created to new node newNode = new nodeType; //create new node newNode->info = newItem; //store item info in node newNode->next = nullptr; //assign next to null if(this->first == nullptr){ //if list is empty this->first = newNode; this->last = newNode; this->count++; //increment } else{ //if list is not empty this->last->next = newNode; //connect last to newNode this->last = newNode; //move last pointer to newNode this->count++; //increment } }// end of insertLast
template void linearLinkedList::deleteNode(const Type &deleteItem){ nodeType *current;//pointer to move through list nodeType *trailCurrent;//a temp current before original current bool found; if(this->first == nullptr){ //1st case: list is empty cout first->info == deleteItem){ //2nd case: one node only current = this->first; this->first = this->first->next; this->count--; if(this->first == nullptr){ this->last = nullptr; } delete current; } else{ //searching list with more than one node found = false; trailCurrent = this->first; //have trailCurrent where current was current = this->first->next; //move current to next node while(current != nullptr && !found){ if(current->info != deleteItem){ trailCurrent = current; current = current->next; } else{ found = true; } }//end while if(found){ //third case: delete it, if found. trailCurrent->next = current->next;//move trailCurrent //ahead of current this->count--; //if the last node should be deleted if(this->last == current){ this->last = trailCurrent; //move last delete current; //delete node } else{ cout template void linearLinkedList::rotate(){ //add function }//end rotate
#endif /* LINEARLINKEDLIST_H */
-------------------------------------------------------------------
-------------------------------------------------------------------
circularLinkedList.h
#ifndef CIRCULARLINKEDLIST_H #define CIRCULARLINKEDLIST_H #include #include "linkedListADT.h"
template class circularLinkedList:public linkedListADT{ public: const circularLinkedList &operator= (const circularLinkedList &); //overload = operator
void initializeList(); //initialize list to empty
bool isEmptyList(); //function to see if list is empty
void print() const; //function to print data from each node
int length() const; //function to return # of nodes in list
void destroyList(); //function to delete all nodes
Type front() const; //function to return first element of list
Type back() const; //function to return last element of list bool search(const Type &searchItem) const; //function to search list void insertFirst(const Type &newItem); //function to insert a node beginning of list void insertLast(const Type &newItem); //function to insert a node at end of list void deleteNode(const Type &deleteItem); //function to delete a node from list void rotate() = 0; //function to rotate nodes in list circularLinkedList(); //default constructor circularLinkedList(const circularLinkedList &otherList); //copy constructor
~circularLinkedList(); //destructor protected: nodeType* &tail = (this->last); //Tail pointer for circular linked list private: void copyCircularList(const circularLinkedList *otherList); //function to make a copy of otherList };
template circularLinkedList::circularLinkedList(){ //default constructor this->first = nullptr; this->last = nullptr; this->count = 0; }
template void circularLinkedList::destroyList(){ nodeType *temp; while(this->first != nullptr){ temp = this->first; //assign temp to first this->first = this->first->next; //move first to next node delete temp; //delete temp from memory } this->last = nullptr; this->count = 0; }
template void circularLinkedList::initializeList(){ destroyList(); //if list has nodes, delete them }
template void circularLinkedList::print() const{ nodeType *current; //pointer to move through list current = this->first; while(current != nullptr){ cout info next; } }
template int circularLinkedList::length() const{ return this->count; //end of length }
template Type circularLinkedList::front() const{ assert(this->first != nullptr); return this->first->info; //return info of first node }
template Type circularLinkedList::back() const{ assert(this->last != nullptr); return this->last->info; //return info of last }
template circularLinkedList::~circularLinkedList(){ destroyList(); }//end destructor
//overload assignment operator template const circularLinkedList& circularLinkedList::operator= (const circularLinkedList &otherList){ if(this != &otherList){ //to avoid self-copy copyCircularList(&otherList); } return *this; }
template circularLinkedList::circularLinkedList (const circularLinkedList &otherList){ this->first = nullptr; copyCircularList(otherList); }//end of copy constructor
//create functions for insertFirst, insertLast, and rotate. template bool circularLinkedList::search(const Type &searchItem) const{ nodeType *current; //pointer to move through list bool found = false; current = this->first;//set current to first node in list while(current != nullptr && !found){ //search the list if(current->info == searchItem){ found = true; //found searchItem } else{ current = current->next; //move to next node } }//end of while return found; } //end of search
template void circularLinkedList::insertFirst(const Type &newItem){ nodeType *newNode; //pointer created to new node newNode = new nodeType; //create new node newNode->info = newItem; //store newItem info to newNode newNode->next = this->first; //have newNode as first node this->first = newNode; //move first pointer to first node this->count++; //increment count }//end insertFirst
template void circularLinkedList::insertLast(const Type &newItem){ nodeType *newNode; //pointer created to new node newNode = new nodeType; //create new node newNode->info = newItem; //store newItem info to newNode newNode->next = tail; //have newNode as last node tail = newNode; //move last pointer to tail node this->count++; //increment count }//end insertLast
template void circularLinkedList::deleteNode(const Type &deleteItem){ //add function }//end deleteNode
template void circularLinkedList::rotate(){ //add function }//end rotate
#endif /* CIRCULARLINKEDLIST_H */
----------------------------------------------------------------
----------------------------------------------------------------
main.cpp
#include #include #include "circularLinkedList.h" #include "linearLinkedList.h"
using namespace std;
int main() {
exit(EXIT_SUCCESS); }
Musical Chairs Simulation Use a circular linked list to run a simple simulation of the musical chairs game. In the game, the players walk around a circle listening to music until the music randomly stops, whereby each player must find a seat as quickly as possible. However, there are always one fewer chairs than people, meaning the last person standing in each round is out of the game. As a new round begins, an additional chair is removed. When only one chair and two people are left in the game, the first person to sit down after the music stops wins. In the simulation, assume that 10 people play the game, represented by integers 1-10 randomly arranged in a circular linked list. Each round, one of the nodes of the circular linked list is randomly removed, representing a player who lost. After 10 rounds, the remaining node in the circular list wins. To keep a running tally of when other players drop out, place each of the losing nodes into a linear linked list. Initial seating Chart: 10 8 71642 35 9 Round 1: Player 10 Lost Players Still in the Game: 8 716423 59 Current Losers: 10 Round 2: Player 5 Lost Players Still in the Game: 871 64 23 9 Current Losers: 10 5 Round 9: Player 9 Lost Players Still in the Game: 8 Current Losers: 10 5431672 9 Round 10: Player 8 WINS! To solve the problem, you will use a number of template classes based off the linkedListType.h and unorderedLinkedList.h classes previously developed in class. Musical Chairs Simulation Use a circular linked list to run a simple simulation of the musical chairs game. In the game, the players walk around a circle listening to music until the music randomly stops, whereby each player must find a seat as quickly as possible. However, there are always one fewer chairs than people, meaning the last person standing in each round is out of the game. As a new round begins, an additional chair is removed. When only one chair and two people are left in the game, the first person to sit down after the music stops wins. In the simulation, assume that 10 people play the game, represented by integers 1-10 randomly arranged in a circular linked list. Each round, one of the nodes of the circular linked list is randomly removed, representing a player who lost. After 10 rounds, the remaining node in the circular list wins. To keep a running tally of when other players drop out, place each of the losing nodes into a linear linked list. Initial seating Chart: 10 8 71642 35 9 Round 1: Player 10 Lost Players Still in the Game: 8 716423 59 Current Losers: 10 Round 2: Player 5 Lost Players Still in the Game: 871 64 23 9 Current Losers: 10 5 Round 9: Player 9 Lost Players Still in the Game: 8 Current Losers: 10 5431672 9 Round 10: Player 8 WINS! To solve the problem, you will use a number of template classes based off the linkedListType.h and unorderedLinkedList.h classes previously developed in class