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:

linkedListIteratorint> 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

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:

linkedQueueTypeType> tmpQueue1 = *this;

linkedQueueTypeType> tmpQueue2 = *this; 3

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;

nodeTypeType> *link;

};

//***********************************************************

// Author: D.S. Malik

//

// This class specifies the members to implement an iterator

// 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.

linkedListIteratorType> 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:

nodeTypeType> *current; //pointer to point to the current

//node in the linked list

};

template <class Type>

linkedListIteratorType>::linkedListIterator()

{

current = NULL;

}

template <class Type>

linkedListIteratorType>::

linkedListIterator(nodeType<Type> *ptr)

{

current = ptr;

}

template <class Type>

Type linkedListIterator<Type>::operator*()

{

return current->info;

}

template <class Type>

linkedListIteratorType> 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&);

//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

// 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.

linkedListIteratorType> begin();

//Function to return an iterator at the beginning of the

//linked list.

//Postcondition: Returns an iterator such that current is set

// to first.

linkedListIteratorType> 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

//

nodeTypeType> *first; //pointer to the first node of the list

nodeTypeType> *last; //pointer to the last node of the list

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>

linkedListTypeType>::linkedListType() //default constructor

{

first = NULL;

last = NULL;

count = 0;

}

template <class Type>

void linkedListType<Type>::destroyList()

{

nodeTypeType> *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

{

nodeTypeType> *current; //pointer to traverse the list

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

{

nodeTypeType> *current; //pointer to traverse the list

bool found = false;

current = first; //set current to point to the first

//node in the list

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)

{

nodeTypeType> *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