When you play a board or card game or when you use a shared computing resource, you get a turn and then wait
until everyone else has had a turn. Although the number of players in a game remains relatively static, the
number of users of a shared computing service fl uctuates. Let’s assume that this fl uctuation will occur.
Design an ADT that keeps track of turns within a group of people. You should be able to add or delete people
and determine whose turn occurs now.
Begin with a given group of people; assign these people an initial order. (This order can be random or
specifi ed by the user.) The fi rst new person joining the group should get a turn after all others have had an equal
number of turns. Each subsequent new person should get a turn after the person who joined the group most
recently has had a turn.
Also design an ADT to represent a person. (You can be conservative with the amount of data that this ADT
contains.) The data that your fi rst ADT stores is made up of instances of the ADT person.
Implement your ADTs as C++ classes. Write a program that uses—and therefore tests—your ADTs completely.
Your program should process several insertion and deletion operations, and demonstrate that people are
given turns correctly.
#ifndef _LIST_INTERFACE
#define _LIST_INTERFACE
usingnamespace std;
template < class ItemType>
class ListInterface
{
public:
/** Sees whether this list is empty.
@return True if the list is empty; otherwise returns false. */
virtualbool isEmpty() const = 0;
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtualint getLength() const = 0;
/** Inserts an entry into this list at a given position.
@pre None.
@post If 1 <= position <= getLength() + 1 and the insertion is
successful, newEntry is at the given position in the list,
other entries are renumbered accordingly, and the returned
value is true.
@param newPosition The list position at which to insert newEntry.
@param newEntry The entry to insert into the list.
@return True if insertion is successful, or false if not. */
virtualbool insert(int newPosition, const ItemType& newEntry) = 0;
/** Removes the entry at a given position from this list.
@pre None.
@post If 1 <= position <= getLength() and the removal is successful,
the entry at the given position in the list is removed, other
items are renumbered accordingly, and the returned value is true.
@param position The list position of the entry to remove.
@return True if removal is successful, or false if not. */
virtualbool remove(int position) = 0;
/** Removes all entries from this list.
@post List contains no entries and the count of items is 0. */
virtualvoid clear() = 0;
/** Gets the entry at the given position in this list.
@pre 1 <= position <= getLength().
@post The desired entry has been returned.
@param position The list position of the desired entry.
@return The entry at the given position. */
virtual ItemType getEntry(int position) const = 0;
/** Replaces the entry at the given position in this list.
@pre 1 <= position <= getLength().
@post The entry at the given position is newEntry.
@param position The list position of the entry to replace.
@param newEntry The replacement entry. */
virtualvoid setEntry(int position, const ItemType& newEntry) = 0;
}; // end ListInterface
#endif
//#ifndef _LINKED_BAG
//#define _LINKED_BAG
#pragma once
#include "BagInterface.h"
#include "Node.h"
usingnamespace std;
#include<iostream>
template < class ItemType>
class LinkedBag : public BagInterface<ItemType>
{
private:
Node<ItemType>* headPtr; // Pointer to first node
int itemCount; // Current count of bag items
// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
Node<ItemType>* getPointerTo(const ItemType& target) const;
public:
LinkedBag();
LinkedBag(const LinkedBag<ItemType>& aBag); // Copy constructor
virtual ~LinkedBag(); // Destructor should be virtual
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
void displayBag();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
}; // end LinkedBag
//#include "LinkedBag.cpp"
//#endif
////////////////////////////////////////////////////////////////////////////////////
template < class ItemType>
LinkedBag<ItemType>::LinkedBag() : headPtr(nullptr), itemCount(0)
{
} // end default constructor
template < class ItemType>
bool LinkedBag<ItemType>::add(const ItemType& newEntry)
{
// Add to beginning of chain: new node references rest of chain;
// (headPtr is nullptr if chain is empty)
Node<ItemType>* newNodePtr = new Node<ItemType>();
newNodePtr->setItem(newEntry);
newNodePtr->setNext(headPtr); // New node points to chain
headPtr = newNodePtr; // New node is now first node
itemCount++;
returntrue;
} // end add
template < class ItemType>
vector<ItemType> LinkedBag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
Node<ItemType>* curPtr = headPtr;
int counter = 0;
while ((curPtr != nullptr) && (counter < itemCount))
{
bagContents.push_back(curPtr->getItem());
curPtr = curPtr->getNext();
counter++;
} // end while
return bagContents;
} // end toVector
template < class ItemType>
bool LinkedBag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template < class ItemType>
int LinkedBag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template < class ItemType>
int LinkedBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int counter = 0;
Node<ItemType>* curPtr = headPtr;
while ((curPtr != nullptr) && (counter < itemCount))
{
if (anEntry == curPtr->getItem())
{
frequency++;
} // end if
counter++;
curPtr = curPtr->getNext();
} // end while
return frequency;
} // end getFrequencyOf
// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
template < class ItemType>
Node<ItemType>* LinkedBag<ItemType>::
getPointerTo(const ItemType& target) const
{
bool found = false;
Node<ItemType>* curPtr = headPtr;
while (!found && (curPtr != nullptr))
{
if (target == curPtr->getItem())
found = true;
else
curPtr = curPtr->getNext();
} // end while
return curPtr;
} // end getPointerTo
//The definition of the method contains is straightforward :
template < class ItemType>
bool LinkedBag<ItemType>::contains(const ItemType& anEntry) const
{
return (getPointerTo(anEntry) != nullptr);
} // end contains
template < class ItemType>
bool LinkedBag<ItemType>::remove(const ItemType& anEntry)
{
Node<ItemType>* entryNodePtr = getPointerTo(anEntry);
bool canRemoveItem = !isEmpty() && (entryNodePtr != nullptr);
if (canRemoveItem)
{
// Copy data from first node to located node
entryNodePtr->setItem(headPtr->getItem());
// Delete first node
Node<ItemType>* nodeToDeletePtr = headPtr;
headPtr = headPtr->getNext();
// Return node to the system
nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;
nodeToDeletePtr = nullptr;
cout << "Removing" << endl;
itemCount--;
} // end if
return canRemoveItem;
} // end remove
template < class ItemType>
void LinkedBag<ItemType>::clear()
{
Node<ItemType>* nodeToDeletePtr =NULL;
while (headPtr != nullptr)
{
Node<ItemType>* nodeToDeletePtr = headPtr;
headPtr = headPtr->getNext();
// Return node to the system
nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;
} // end while
// headPtr is nullptr
nodeToDeletePtr = nullptr;
itemCount = 0;
} // end clear
template < class ItemType>
LinkedBag<ItemType>::~LinkedBag()
{
clear();
} // end destructor
template < class ItemType>
void LinkedBag<ItemType>::displayBag()
{
cout << "The bag contains " << this->getCurrentSize()
<< " items:" << endl;
Node<ItemType>*curPtr=this->headPtr;
Node<ItemType>*curPtr2 = this->headPtr;
int counter=0;
cout <<endl<< "Economy class" << endl;
cout << "--------------------------------------------" << endl << endl;
while (curPtr2 != nullptr)
{
if (curPtr2->getItem().getSitn() >5){
cout << curPtr2->getItem();
}
curPtr2 = curPtr2->getNext();
counter++;
}
cout <<endl<< "First Class" << endl;
cout << "--------------------------------------------" << endl << endl;
while(curPtr!=nullptr)
{
if (curPtr->getItem().getSitn() <= 5){
cout << curPtr->getItem();
}
curPtr = curPtr->getNext();
counter++;
}
cout << endl << endl;
}
template < class ItemType>
LinkedBag<ItemType>::LinkedBag(const LinkedBag<ItemType>& aBag)
{
itemCount = aBag->itemCount;
Node<ItemType>* origChainPtr = aBag->headPtr;
if (origChainPtr == nullptr)
headPtr = nullptr; // Original bag is empty; so is copy
else
{
// Copy first node
headPtr = new Node<ItemType>();
headPtr->setItem(origChainPtr->getItem());
// Copy remaining nodes
Node<ItemType>* newChainPtr = headPtr; // Last-node pointer
while (origPtr != nullptr)
{
origChainPtr = origChainPtr->getNext(); // Advance pointer
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
// Link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
} // end while
newChainPtr->setNext(nullptr); // Flag end of new chain
} // end if
} // end copy constructor
#ifndef ArrayBag_BagInterface_h
#define ArrayBag_BagInterface_h
#include <vector>
usingnamespace std;
template<class ItemType> class BagInterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtualint getCurrentSize() const = 0;
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtualbool isEmpty() const = 0;
/** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and
the count of items in the bag has increased by 1.
@param newEntry The object to be added as a new entry. @return True if addition was successful, or false if not. */
virtualbool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag, if possible.
@post If successful, anEntry has been removed from the bag and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtualbool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtualvoid clear() = 0;
/** Counts the number of times a given entry appears in bag. @param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtualint getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtualbool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that are in this bag.
@return A vector containing all the entries in the bag. */
virtual vector<ItemType> toVector() const = 0;
virtual ~BagInterface(){}
}; // end BagInterface
#endif
//
// Header.h
// ArrayBag
//
//
#ifndef ArrayBag_Header_h
#define ArrayBag_Header_h
#include "BagInterface.h"
#include<iostream>
usingnamespace std;
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
protected:
staticconstint DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
bool add(const ItemType& newEntry);
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
//ArrayBag<ItemType> operator+(ArrayBag<ItemType> & anBag);
/*friend ostream& operator<<(ostream & out, const ArrayBag<ItemType> & Term);
friend istream& operator>>(istream & in, ArrayBag<ItemType> & Term);*/
void crateBag();
virtual ~ArrayBag();
// end ArrayBag
};
template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
}//end if
}//end while
return result;
}
template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
}// end add
template<class ItemType>
ArrayBag<ItemType>::ArrayBag()
{
itemCount = 0;
maxItems = DEFAULT_CAPACITY;
}
template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
}// end getCurrentSize
template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
}// end isEmpty
template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
}// end remove
template<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
}// end Clear
template<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry) {
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
}//end getFrequencyOf
template<class ItemType>
ostream& operator<<(ostream & out, const ArrayBag<ItemType> & Terms)
{
for(int i=0;i<Terms.itemCount;i++)
{
cout<<Terms.items[i];
}
return out;
}
/*template<class ItemType>
ArrayBag<ItemType> operator+(ArrayBag<ItemType> & anBag)
{
}*/
template<class ItemType>
void ArrayBag<ItemType>::crateBag()
{
Term temp;
for(int i=0;i<this->itemCount;i++)
{
cin>>temp;
add(temp);
}
}
template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
}
template<class ItemType>
ArrayBag<ItemType>::~ArrayBag(){}
#endif