f01.justanswer.com · web viewjan 09, 2014 · the remaining problems for the assignment must be...

26
Troy University, eTroy, CS 3330 Data Structures and Algorithms. Copyright (c) 2014 by Dr. Jack Davault. Problem Set 1: Lists, Stacks, and Queues Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In order to receive credit, your programs must compile and run; and you must provide all source code files so that I can compile and run your program. Examples on how to import existing files into your compiler are provided in the file called Importing Source Code.pdf. The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include your name and course number within all files that you submit, including source code files as a comment at the top of each file that you create or modify. 1. [6 points] Arrays and Linked Lists Read the assigned chapters and notes for Module 1 located in the Learning Activities area, and then provide solutions to the following: (a) [4 points] Download the file linkedList.zip. Modify the linkedList.h file by implementing details for the class method called isEqual(). The prototype for this function is as follows: bool isEqual(const linkedListType<Type>& givenList); The method will take a given linked list as input and compares it to the current list. The method will return a Boolean (true or false, which equates to 1 or 0) to indicate whether or not the given list is equal to the current list. Two lists are equal if they contain the same number of elements and the elements that are in the current list are also in the given list – order of the items in the list does not matter. Hint: You only need to modify the areas of the code in the linkedList.h file that are marked with the TODO comments. Everything else, including the driver.cpp file should remain that same. This method will require some thought. You can implement this method anyway you like, but you must use the provided lists and class method prototype. Recommendations: Remember to check that the lengths of the list are equal. If they are not you can return false right away. Consider using a temporary linked list iterator and a while loop to walk

Upload: phamcong

Post on 26-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

Troy University, eTroy, CS 3330 Data Structures and Algorithms. Copyright (c) 2014 by Dr. Jack Davault.

Problem Set 1: Lists, Stacks, and Queues Overview: For problems 1(a) and 2(a) of this assignment, you will need a C++ compiler. In order to receive credit, your programs must compile and run; and you must provide all source code files so that I can compile and run your program. Examples on how to import existing files into your compiler are provided in the file called Importing Source Code.pdf. The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include your name and course number within all files that you submit, including source code files as a comment at the top of each file that you create or modify. 1. [6 points] Arrays and Linked Lists

Read the assigned chapters and notes for Module 1 located in the Learning Activities area, and then provide solutions to the following: (a) [4 points] Download the file linkedList.zip. Modify the linkedList.h file by implementing details for the class method called isEqual(). The prototype for this function is as follows:

bool isEqual(const linkedListType<Type>& givenList); The method will take a given linked list as input and compares it to the current list. The method will return a Boolean (true or false, which equates to 1 or 0) to indicate whether or not the given list is equal to the current list. Two lists are equal if they contain the same number of elements and the elements that are in the current list are also in the given list – order of the items in the list does not matter. Hint: You only need to modify the areas of the code in the linkedList.h file that are marked with the TODO comments. Everything else, including the driver.cpp file should remain that same. This method will require some thought. You can implement this method anyway you like, but you must use the provided lists and class method prototype. Recommendations: Remember to check that the lengths of the list are equal. If they are not you can return false right away. Consider using a temporary linked list iterator and a while loop to walk through the items in the current list. For example, at the top of the function declare an iterator: linkedListIterator<int> itr = this->begin(); Create a while-loop that checks if the item itr is in the given list. You can do this by calling the search() method on the given list and passing in *itr. If the item is not in the list you can return false. At the end of the loop increment the pointer: ++itr 2

Page 2: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

Output: The output of the program after the isEqual() class method is implement is as follows: List 1: 1 2 3 4 List 2: 10 20 30 40 List 3: 20 30 40 10 List 4: 1 2 3 List 5: 3 4 1 2 List 1 equals List 2: 0 List 2 equals List 3: 1 List 3 equals List 4: 0 List 1 equals List 4: 0 List 4 equals List 1: 0 List 1 equals List 5: 1 ** Press any key to continue ** (b) [2 points] Linked lists can be implemented using either arrays or pointers. Briefly discuss one advantage and one disadvantage between an array-based list verses a pointer-based linked list implementation and vice versa.

2. [6 points] Arrays and Linked Lists

Read the assigned chapters and notes for Module 2 located in the Learning Activities area, then provide solutions to the following: (a) [4 points] Download the file linkedQueue.zip. Modify the linkedQueue.h file by implementing the details for the class method called highestPriority(int position), which will take the item at the given position in the queue and move it to the front of the queue. If the given position is greater than the length of the queue, then the original queue will remain unchanged. Note that once the item is moved, all of the remaining items in the queue will stay in their original order.

Hint: Again, you only need to modify the linkedQueue.h file by adding the necessary code to implement the TODO areas as noted in the comments. The prototype for the function and everything else in the program will remain unchanged (include the driver.cpp file). Consider using two while-loops, two temporary queues, and two temporary item variables for this implementation. Remember that the main queue will be in the this pointer variable. You can create the temporary queues with the original contents of the main queue as follows: linkedQueueType<Type> tmpQueue1 = *this; linkedQueueType<Type> tmpQueue2 = *this; 3

Page 3: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

After creating the temporary queues, clear the main queue, using the initializeQueue() method: this->initializedQueue(); Output: The output for the program after the function is implemented should appear as follows: Original queue: 3.14 0.11 7.18 0.29 9.62 Queue after moving item 2 to front: 0.11 3.14 7.18 0.29 9.62 Queue after moving item 7 to front: 0.11 3.14 7.18 0.29 9.62 Queue after moving item 5 to front: 9.62 0.11 3.14 7.18 0.29 ** Press any key to continue ** (b) [2 points] Briefly explain the differences between a queue and a priority queue data structure. Provide a computer related example that could use a priority queue instead of a queue.

Other Notes: Submit your solutions as a single Zip file using the Problem Set 1 link provided in the Assignments area. If you are using the Visual C++ or Dev-C++ compiler, you should only submit the source code files (e.g. the files ending in .cpp and .h). For space reasons, please do not submit the entire Visual C++ or Dev-C++ project folders. Do not hesitate to ask if you have any questions or need clarification on what to do.

Edit Program 1/** * Modified by: * * TODO: Include your name here * CS3330 Data Structures and Algorithms */

#ifndef H_LinkedListType#define H_LinkedListType

#include <iostream>#include <cassert>

using namespace std;

//Definition of the node

template <class Type>struct nodeType{

Type info;nodeType<Type> *link;

};

//***********************************************************// Author: D.S. Malik//// This class specifies the members to implement an iterator

Page 4: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

// to a linked list.//***********************************************************

template <class Type>class linkedListIterator{public: linkedListIterator(); //Default constructor //Postcondition: current = NULL;

linkedListIterator(nodeType<Type> *ptr); //Constructor with a parameter. //Postcondition: current = ptr;

Type operator*(); //Function to overload the dereferencing operator *. //Postcondition: Returns the info contained in the node.

linkedListIterator<Type> operator++(); //Overload the preincrement operator. //Postcondition: The iterator is advanced to the next node.

bool operator==(const linkedListIterator<Type>& right) const; //Overload the equality operator. //Postcondition: Returns true if this iterator is equal to // the iterator specified by right, otherwise it returns // false.

bool operator!=(const linkedListIterator<Type>& right) const; //Overload the not equal to operator. //Postcondition: Returns true if this iterator is not equal to // the iterator specified by right, otherwise it returns // false.

private: nodeType<Type> *current; //pointer to point to the current //node in the linked list};

template <class Type>linkedListIterator<Type>::linkedListIterator(){ current = NULL;}

template <class Type>

Page 5: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

linkedListIterator<Type>:: linkedListIterator(nodeType<Type> *ptr){ current = ptr;}

template <class Type>Type linkedListIterator<Type>::operator*(){ return current->info;}

template <class Type>linkedListIterator<Type> linkedListIterator<Type>::operator++() { current = current->link;

return *this;}

template <class Type>bool linkedListIterator<Type>::operator== (const linkedListIterator<Type>& right) const{ return (current == right.current);}

template <class Type>bool linkedListIterator<Type>::operator!= (const linkedListIterator<Type>& right) const{ return (current != right.current);}

//***********************************************************// Author: D.S. Malik// Modified: J. Davault//// This class specifies the members to implement the basic// properties of a linked list. This is an abstract class.// We cannot instantiate an object of this class.//***********************************************************

template <class Type>class linkedListType{public: const linkedListType<Type>& operator= (const linkedListType<Type>&);

Page 6: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

//Overload the assignment operator.

void initializeList(); //Initialize the list to an empty state. //Postcondition: first = NULL, last = NULL, count = 0;

bool isEmptyList() const; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, otherwise // it returns false.

void print() const; //Function to output the data contained in each node. //Postcondition: none

int length() const; //Function to return the number of nodes in the list. //Postcondition: The value of count is returned.

void destroyList(); //Function to delete all the nodes from the list. //Postcondition: first = NULL, last = NULL, count = 0;

Type front() const; //Function to return the first element of the list. //Precondition: The list must exist and must not be empty. //Postcondition: If the list is empty, the program terminates; // otherwise, the first element of the list is returned.

Type back() const; //Function to return the last element of the list. //Precondition: The list must exist and must not be empty. //Postcondition: If the list is empty, the program // terminates; otherwise, the last // element of the list is returned.

bool isEqual(const linkedListType<Type>& givenList); // Function to determine if the list provided equals to the // current list.

bool search(const Type& searchItem) const; //Function to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is in the list, // otherwise the value false is returned.

void insertFirst(const Type& newItem); //Function to insert newItem at the beginning of the list. //Postcondition: first points to the new list, newItem is

Page 7: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

// inserted at the beginning of the list, last points to // the last node in the list, and count is incremented by // 1.

void insertLast(const Type& newItem); //Function to insert newItem at the end of the list. //Postcondition: first points to the new list, newItem is // inserted at the end of the list, last points to the // last node in the list, and count is incremented by 1.

void deleteNode(const Type& deleteItem); //Function to delete deleteItem from the list. //Postcondition: If found, the node containing deleteItem is // deleted from the list. first points to the first node, // last points to the last node of the updated list, and // count is decremented by 1.

linkedListIterator<Type> begin(); //Function to return an iterator at the beginning of the //linked list. //Postcondition: Returns an iterator such that current is set // to first.

linkedListIterator<Type> end(); //Function to return an iterator one element past the //last element of the linked list. //Postcondition: Returns an iterator such that current is set // to NULL.

linkedListType(); //default constructor //Initializes the list to an empty state. //Postcondition: first = NULL, last = NULL, count = 0;

linkedListType(const linkedListType<Type>& otherList); //copy constructor

~linkedListType(); //destructor //Deletes all the nodes from the list. //Postcondition: The list object is destroyed.

protected: int count; //variable to store the number of list elements // nodeType<Type> *first; //pointer to the first node of the list nodeType<Type> *last; //pointer to the last node of the list

Page 8: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

private: void copyList(const linkedListType<Type>& otherList); //Function to make a copy of otherList. //Postcondition: A copy of otherList is created and assigned // to this list.};

template <class Type>bool linkedListType<Type>::isEmptyList() const{ return (first == NULL);}

template <class Type>linkedListType<Type>::linkedListType() //default constructor{ first = NULL; last = NULL; count = 0;}

template <class Type>void linkedListType<Type>::destroyList(){ nodeType<Type> *temp; //pointer to deallocate the memory //occupied by the node while (first != NULL) //while there are nodes in the list { temp = first; //set temp to the current node first = first->link; //advance first to the next node delete temp; //deallocate the memory occupied by temp }

last = NULL; //initialize last to NULL; first has already //been set to NULL by the while loop count = 0;}

template <class Type>void linkedListType<Type>::initializeList(){

destroyList(); //if the list has any nodes, delete them}

template <class Type>void linkedListType<Type>::print() const{ nodeType<Type> *current; //pointer to traverse the list

Page 9: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

current = first; //set current so that it points to //the first node while (current != NULL) //while more data to print { cout << current->info << " "; current = current->link; }}//end print

template <class Type>int linkedListType<Type>::length() const{ return count;} //end length

template <class Type>Type linkedListType<Type>::front() const{ assert(first != NULL);

return first->info; //return the info of the first node}//end front

template <class Type>Type linkedListType<Type>::back() const{ assert(last != NULL);

return last->info; //return the info of the last node}//end back

template <class Type>bool linkedListType<Type>::isEqual(const linkedListType<Type>& givenList){ // TODO: Implement the details of the isEqual method.

return true;}

template <class Type>bool linkedListType<Type>::search(const Type& searchItem) const{ nodeType<Type> *current; //pointer to traverse the list bool found = false; current = first; //set current to point to the first //node in the list

Page 10: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

while (current != NULL && !found) //search the list if (current->info == searchItem) //searchItem is found found = true; else current = current->link; //make current point to //the next node return found; }//end search

template <class Type>void linkedListType<Type>::insertFirst(const Type& newItem){ nodeType<Type> *newNode; //pointer to create the new node

newNode = new nodeType<Type>; //create the new node

newNode->info = newItem; //store the new item in the node newNode->link = first; //insert newNode before first first = newNode; //make first point to the //actual first node count++; //increment count

if (last == NULL) //if the list was empty, newNode is also //the last node in the list last = newNode;}//end insertFirst

template <class Type>void linkedListType<Type>::insertLast(const Type& newItem){ nodeType<Type> *newNode; //pointer to create the new node

newNode = new nodeType<Type>; //create the new node

newNode->info = newItem; //store the new item in the node newNode->link = NULL; //set the link field of newNode //to NULL

if (first == NULL) //if the list is empty, newNode is //both the first and last node { first = newNode; last = newNode; count++; //increment count } else //the list is not empty, insert newNode after last {

Page 11: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

last->link = newNode; //insert newNode after last last = newNode; //make last point to the actual //last node in the list count++; //increment count }}//end insertLast

template <class Type>void linkedListType<Type>::deleteNode(const Type& deleteItem){ nodeType<Type> *current; //pointer to traverse the list nodeType<Type> *trailCurrent; //pointer just before current bool found;

if (first == NULL) //Case 1; the list is empty. cout << "Cannot delete from an empty list." << endl; else { if (first->info == deleteItem) //Case 2 { current = first; first = first->link; count--; if (first == NULL) //the list has only one node last = NULL; delete current; } else //search the list for the node with the given info { found = false; trailCurrent = first; //set trailCurrent to point //to the first node current = first->link; //set current to point to //the second node

while (current != NULL && !found) { if (current->info != deleteItem) { trailCurrent = current; current = current-> link; } else found = true; }//end while

Page 12: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

if (found) //Case 3; if found, delete the node { trailCurrent->link = current->link; count--;

if (last == current) //node to be deleted //was the last node last = trailCurrent; //update the value //of last delete current; //delete the node from the list } else cout << "The item to be deleted is not in " << "the list." << endl; }//end else }//end else}//end deleteNode

template <class Type>linkedListIterator<Type> linkedListType<Type>::begin(){ linkedListIterator<Type> temp(first);

return temp;}

template <class Type>linkedListIterator<Type> linkedListType<Type>::end(){ linkedListIterator<Type> temp(NULL);

return temp;}

template <class Type>void linkedListType<Type>::copyList (const linkedListType<Type>& otherList) { nodeType<Type> *newNode; //pointer to create a node nodeType<Type> *current; //pointer to traverse the list

if (first != NULL) //if the list is nonempty, make it empty destroyList();

if (otherList.first == NULL) //otherList is empty { first = NULL; last = NULL;

Page 13: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

count = 0; } else { current = otherList.first; //current points to the //list to be copied count = otherList.count;

//copy the first node first = new nodeType<Type>; //create the node

first->info = current->info; //copy the info first->link = NULL; //set the link field of //the node to NULL last = first; //make last point to the //first node current = current->link; //make current point to //the next node

//copy the remaining list while (current != NULL) { newNode = new nodeType<Type>; //create a node newNode->info = current->info; //copy the info newNode->link = NULL; //set the link of //newNode to NULL last->link = newNode; //attach newNode after last last = newNode; //make last point to //the actual last node current = current->link; //make current point //to the next node }//end while }//end else}//end copyList

template <class Type>linkedListType<Type>::~linkedListType() //destructor{ destroyList();}//end destructor

template <class Type>linkedListType<Type>::linkedListType (const linkedListType<Type>& otherList){ first = NULL; copyList(otherList);}//end copy constructor

Page 14: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

//overload the assignment operatortemplate <class Type>const linkedListType<Type>& linkedListType<Type>::operator= (const linkedListType<Type>& otherList){ if (this != &otherList) //avoid self-copy { copyList(otherList); }//end else

return *this; }

#endif

Program2/** * Modified by: * * TODO: Include your name and course number here. */

//Header file linkedQueue.h

#ifndef H_linkedQueue#define H_linkedQueue

#include <iostream>#include <cassert>

using namespace std;

//*************************************************************// Author: D.S. Malik//// This class specifies the basic operations on a queue as a// linked list.//// Modified: J. M. Davault//// Modified for CS3330 Data Structures and Algorithms. Removed// queueADT base class and virtual functions.//*************************************************************

//Definition of the nodetemplate <class Type>struct nodeType{

Page 15: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

Type info; nodeType<Type> *link;};

template <class Type>class linkedQueueType{public: const linkedQueueType<Type>& operator= (const linkedQueueType<Type>&); //Overload the assignment operator.

bool isEmptyQueue() const; //Function to determine whether the queue is empty. //Postcondition: Returns true if the queue is empty, // otherwise returns false.

bool isFullQueue() const; //Function to determine whether the queue is full. //Postcondition: Returns true if the queue is full, // otherwise returns false.

void initializeQueue(); //Function to initialize the queue to an empty state. //Postcondition: queueFront = NULL; queueRear = NULL

Type front() const; //Function to return the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the program // terminates; otherwise, the first element of the // queue is returned.

Type back() const; //Function to return the last element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the program // terminates; otherwise, the last element of the // queue is returned.

void print(); // Function to print the items in the queue

void highestPriority(int position); // Moves an item at the given position to the front of the // queue.

void addQueue(const Type& queueElement); //Function to add queueElement to the queue. //Precondition: The queue exists and is not full. //Postcondition: The queue is changed and queueElement is

Page 16: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

// added to the queue.

void deleteQueue(); //Function to remove the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: The queue is changed and the first element // is removed from the queue.

linkedQueueType(); //Default constructor

linkedQueueType(const linkedQueueType<Type>& otherQueue); //Copy constructor

~linkedQueueType(); //Destructor

private: nodeType<Type> *queueFront; //pointer to the front of the queue nodeType<Type> *queueRear; //pointer to the rear of the queue};

//Default constructortemplate<class Type>linkedQueueType<Type>::linkedQueueType(){ queueFront = NULL; //set front to null queueRear = NULL; //set rear to null} //end default constructor

template<class Type>bool linkedQueueType<Type>::isEmptyQueue() const{ return(queueFront == NULL);} //end

template<class Type>bool linkedQueueType<Type>::isFullQueue() const{ return false;} //end isFullQueue

template <class Type>void linkedQueueType<Type>::initializeQueue(){ nodeType<Type> *temp;

while (queueFront!= NULL) //while there are elements left //in the queue { temp = queueFront; //set temp to point to the

Page 17: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

//current node queueFront = queueFront->link; //advance first to //the next node delete temp; //deallocate memory occupied by temp }

queueRear = NULL; //set rear to NULL} //end initializeQueue

template <class Type>void linkedQueueType<Type>::print(){ Type item; linkedQueueType<Type> tmpQueue = *this;

cout << " ";

while (tmpQueue.isEmptyQueue() == false) { item = tmpQueue.front(); tmpQueue.deleteQueue(); cout << item << " "; }

cout << endl;}

template <class Type>void linkedQueueType<Type>::highestPriority(int position){ // TODO: Implement the details of this function that moves // a given item at value of the position variable to // the front of the queue. Note that all of the other items // in the queue will remain in their same relative position. }

template <class Type>void linkedQueueType<Type>::addQueue(const Type& newElement){ nodeType<Type> *newNode;

newNode = new nodeType<Type>; //create the node

newNode->info = newElement; //store the info newNode->link = NULL; //initialize the link field to NULL

if (queueFront == NULL) //if initially the queue is empty { queueFront = newNode; queueRear = newNode;

Page 18: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

} else //add newNode at the end { queueRear->link = newNode; queueRear = queueRear->link; }}//end addQueue

template <class Type>Type linkedQueueType<Type>::front() const{ assert(queueFront != NULL); return queueFront->info;} //end front

template <class Type>Type linkedQueueType<Type>::back() const{ assert(queueRear!= NULL); return queueRear->info;} //end back

template <class Type>void linkedQueueType<Type>::deleteQueue(){ nodeType<Type> *temp;

if (!isEmptyQueue()) { temp = queueFront; //make temp point to the //first node queueFront = queueFront->link; //advance queueFront

delete temp; //delete the first node

if (queueFront == NULL) //if after deletion the //queue is empty queueRear = NULL; //set queueRear to NULL } else cout << "Cannot remove from an empty queue" << endl;}//end deleteQueue

//Destructortemplate <class Type>linkedQueueType<Type>::~linkedQueueType(){ nodeType<Type> *temp;

while (queueFront != NULL) //while there are elements

Page 19: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

//left in the queue { temp = queueFront; //set temp to point to the //current node queueFront = queueFront->link; //advance first to //the next node delete temp; //deallocate memory occupied by temp }

queueRear = NULL; // set rear to null} //end destructor

template <class Type>const linkedQueueType<Type>& linkedQueueType<Type>::operator=(const linkedQueueType<Type>& otherQueue){ nodeType<Type> *newNode; //pointer to create a node nodeType<Type> *current; //pointer to traverse the list.

if (this != &otherQueue) //avoid self-copy { if (queueFront != NULL) //if the list is not empty, destroy the list initializeQueue();

if (otherQueue.queueFront == NULL) //otherList is empty { queueFront = NULL; queueRear = NULL; } else { current = otherQueue.queueFront; //current points to the //list to be copied

//copy the front element queueFront = new nodeType<Type>; //create the node queueFront->info = current->info; //copy the info queueFront->link = NULL; //set the link field of //the node to null queueRear = queueFront; //make rear point to the first node current = current->link; //make current point to the next //node of the list being copied

//copy the remaining list while (current != NULL) { newNode = new nodeType<Type>; newNode->info = current->info; newNode->link = NULL; queueRear->link = newNode; queueRear = newNode;

Page 20: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

current = current->link; }//end while }//end else }//end else

return *this;

} //end assignment operator

//copy constructortemplate <class Type>linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue){ nodeType<Type> *newNode; //pointer to create a node nodeType<Type> *current; //pointer to traverse the list

if (otherQueue.queueFront == NULL) //otherList is empty { queueFront = NULL; queueRear = NULL; } else { current = otherQueue.queueFront; //current points to the //list to be copied

//copy the first node queueFront = new nodeType<Type>; //create the node queueFront->info = current->info; //copy the info queueFront->link = NULL; //set the link field of //the node to null queueRear = queueFront; //make rear point to the //front node current = current->link; //make current point to the //next node

//copy the remaining list while (current != NULL) { newNode = new nodeType<Type>; //create a node newNode->info = current->info; //copy the info newNode->link = NULL; //set the link of //newNode to null queueRear->link = newNode; //attach newNode after rear queueRear = newNode; //make rear point to //the actual rear node current = current->link; //make current point to //the next node }//end while }//end else

Page 21: f01.justanswer.com · Web viewJan 09, 2014 · The remaining problems for the assignment must be written up in a single Microsoft Word document. You must include …

}//end copy constructor

#endif