PrecondViolatedExcepth ifndef PRECONDVIOLATEDEXCEPT define P
/PrecondViolatedExcept.h
#ifndef PRECOND_VIOLATED_EXCEPT_
#define PRECOND_VIOLATED_EXCEPT_
#include <stdexcept>
#include <string>
class PrecondViolatedExcept : public std::logic_error
{
public:
PrecondViolatedExcept(const std::string& message = \"\");
}; // end PrecondViolatedExcept
#endif
//PrecondViolatedExcept.cpp
#include \"PrecondViolatedExcept.h\"
PrecondViolatedExcept::PrecondViolatedExcept(const std::string& message)
: std::logic_error(\"Precondition Violated Exception: \" + message)
{
} // end constructor
//Node.h
#ifndef NODE_
#define NODE_
template<class ItemType>
class Node
{
private:
ItemType item; // A data item
Node* next; // Pointer to next node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node* nextNodePtr);
ItemType getItem() const;
Node* getNext() const;
}; // end Node
#endif
//Node.cpp
#include \"Node.h\"
template<class ItemType>
Node::Node() : next(nullptr)
{
} // end default constructor
template<class ItemType>
Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor
template<class ItemType>
Node::Node(const ItemType& anItem, Node* nextNodePtr) :
item(anItem), next(nextNodePtr)
{
} // end constructor
template<class ItemType>
void Node::setItem(const ItemType& anItem)
{
item = anItem;
} // end setItem
template<class ItemType>
void Node::setNext(Node* nextNodePtr)
{
next = nextNodePtr;
} // end setNext
template<class ItemType>
ItemType Node::getItem() const
{
return item;
} // end getItem
template<class ItemType>
Node* Node::getNext() const
{
return next;
} // end getNext
//LinkedSortedList.h
#ifndef PRIORITY_QUEUE_
#define PRIORITY_QUEUE_
#include \"LinkedSortedList.cpp\"
#include \"PrecondViolatedExcept.h\"
#include <memory>
template <class ItemType>
class SL_PriorityQueue
{
private:
std::unique_ptr<LinkedSortedList<ItemType>> slistPtr; //Ptr to sorted list of items
public:
SL_PriorityQueue();
SL_PriorityQueue(const SL_PriorityQueue& pq);
~SL_PriorityQueue();
bool isEmpty() const;
bool enqueue(const ItemType& newEntry);
bool dequeue();
ItemType peekFront() const throw(PrecondViolatedExcept);
}; //end SL_PriorityQueue
#endif
//LinkedSortedList.cpp
#include \"SL_PriorityQueue.h\"
#include <memory>
template<class ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue(): slistPtr(std::make_unique<LinkedSortedList<ItemType>>())
{
}
template<class ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue(const SL_PriorityQueue& pq)
{
}
template<class ItemType>
SL_PriorityQueue<ItemType>::~SL_PriorityQueue()
{
}
template<class ItemType>
bool SL_PriorityQueue<ItemType>::isEmpty() const
{
return slistPtr->isEmpty();
}
template<class ItemType>
bool SL_PriorityQueue<ItemType>::enqueue(const ItemType& newEntry)
{
return slistPtr-> insertSorted(newEntry);
}//end enqueue
template<class ItemType>
bool SL_PriorityQueue<ItemType>::dequeue()
{
//The highest-priority item is at the end of the sorted list
return slistPtr->remove(slistPtr->getLength());
}//end dequeue
template<class ItemType>
ItemType SL_PriorityQueue<ItemType>::peekFront() const throw(PrecondViolatedExcept)
{
if(isEmpty())
throw PrecondViolatedExcept(\"peekFront() called with empty queue.\");
// Queue is not empty: return highest-priority item
return slistPtr->getEntry(slistPtr->getLength());
}
//SL_PriorityQueue.h
#ifndef PRIORITY_QUEUE_
#define PRIORITY_QUEUE_
#include \"LinkedSortedList.cpp\"
#include \"PrecondViolatedExcept.h\"
#include <memory>
template <class ItemType>
class SL_PriorityQueue
{
private:
std::unique_ptr<LinkedSortedList<ItemType>> slistPtr; //Ptr to sorted list of items
public:
SL_PriorityQueue();
SL_PriorityQueue(const SL_PriorityQueue& pq);
~SL_PriorityQueue();
bool isEmpty() const;
bool enqueue(const ItemType& newEntry);
bool dequeue();
ItemType peekFront() const throw(PrecondViolatedExcept);
}; //end SL_PriorityQueue
#endif
/SL_PriorityQueue.cpp
#include \"SL_PriorityQueue.h\"
#include <memory>
template<class ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue(): slistPtr(std::make_unique<LinkedSortedList<ItemType>>())
{
}
template<class ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue(const SL_PriorityQueue& pq)
{
}
template<class ItemType>
SL_PriorityQueue<ItemType>::~SL_PriorityQueue()
{
}
template<class ItemType>
bool SL_PriorityQueue<ItemType>::isEmpty() const
{
return slistPtr->isEmpty();
}
template<class ItemType>
bool SL_PriorityQueue<ItemType>::enqueue(const ItemType& newEntry)
{
return slistPtr-> insertSorted(newEntry);
}//end enqueue
template<class ItemType>
bool SL_PriorityQueue<ItemType>::dequeue()
{
//The highest-priority item is at the end of the sorted list
return slistPtr->remove(slistPtr->getLength());
}//end dequeue
template<class ItemType>
ItemType SL_PriorityQueue<ItemType>::peekFront() const throw(PrecondViolatedExcept)
{
if(isEmpty())
throw PrecondViolatedExcept(\"peekFront() called with empty queue.\");
// Queue is not empty: return highest-priority item
return slistPtr->getEntry(slistPtr->getLength());
}
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
//#include \"ListQueue.cpp\"
//#include \"ArrayQueue.cpp\"
#include \"SL_PriorityQueue.cpp\"
using namespace std;
struct Event
{
char Type;
long Time;
int Length;
long Customer;
};
//void processArrival(Event arrivalEvent, )
void simulate()
{
/*
string fileName;
long arrivalTime;
int transactionLength;
int number_of_events=0;
Event event;
ifstream inputFile;
//ListQueue<long>* bankLine= new ListQueue<long>();
SL_PriorityQueue<Event>* eventPriorityQueue= new SL_PriorityQueue<Event>();
bool tellerAvailable=true;
cout<<\"Enter the file name:\"<<endl;
cin>>fileName;
inputFile.open(fileName);
if (inputFile)
{
while (inputFile >>arrivalTime)
{
event.Type=\'A\';
event.Time = arrivalTime;
inputFile >> transactionLength;
event.Length=transactionLength;
event.Customer=number_of_events+1;
number_of_events++;
eventPriorityQueue->enqueue(event);
}
}
else
cout << \"Error opening the file.\" << endl;
inputFile.close();
*/
}
int main()
{
//simulate();
/*
ListQueue<int>* bankLine= new ListQueue<int>();
bankLine->enqueue(1);
bankLine->enqueue(2);
cout<<bankLine->peekFront()<<endl;
bankLine->dequeue();
cout<<bankLine->peekFront()<<endl;
*/
SL_PriorityQueue<int>* bankLine= new SL_PriorityQueue<int>();
bankLine->enqueue(1);
bankLine->enqueue(2);
cout<<bankLine->peekFront()<<endl;
bankLine->dequeue();
cout<<bankLine->peekFront()<<endl;
return 0;
}
//makefile
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
PA05 : PA05.o Node.o PrecondViolatedExcept.o LinkedSortedList.o SL_PriorityQueue.o
$(CC) $(LFLAGS) -std=c++14 PrecondViolatedExcept.o Node.o PA05.o LinkedSortedList.o SL_PriorityQueue.o -o PA05
PA05.o : SL_PriorityQueue.cpp PA05.cpp
$(CC) $(CFLAGS) -std=c++14 PA05.cpp
LinkedSortedList.o : LinkedSortedList.h LinkedSortedList.cpp PrecondViolatedExcept.cpp Node.cpp
$(CC) $(CFLAGS) -std=c++14 LinkedSortedList.cpp
SL_PriorityQueue.o : SL_PriorityQueue.h SL_PriorityQueue.cpp PrecondViolatedExcept.cpp LinkedSortedList.cpp
$(CC) $(CFLAGS) -std=c++14 SL_PriorityQueue.cpp
PrecondViolatedExcep.o : PrecondViolatedExcept.cpp PrecondViolatedExcept.h
$(CC) $(CFLAGS) -std=c++14 PrecondViolatedExcept.cpp
Node.o : Node.h Node.cpp
$(CC) $(CFLAGS) -std=c++14 Node.cpp
clean:
\ m *.o PA05
I got \"LinkedSortedList.cpp:12:9: error: no match for ‘operator=’ (operand types are ‘std::shared_ptr<Node<int> >’ and ‘Node<int>*’)\"... Does anyone know how to fix it?
\"
Solution
You can\'t directly assign a Node<ItemType> to a shared_ptr. yu will have to use get method and shared_ptr constructor instead..
use this code for LinkedQueue.cpp
//LinkedQueue.cpp
#include \"LinkedQueue.h\"
#include \"Node.cpp\"
template<class ItemType>
LinkedQueue<ItemType>::LinkedQueue()
{
;
}
template<class ItemType>
LinkedQueue<ItemType>::LinkedQueue(const LinkedQueue& aQueue)
{
;
}
template<class ItemType>
LinkedQueue<ItemType>::~LinkedQueue()
{
;
}
template<class ItemType>
bool LinkedQueue<ItemType>::isEmpty() const
{
return false;
}
template<class ItemType>
bool LinkedQueue<ItemType>::enqueue(const ItemType& newEntry)
{
auto newNodePtr = std::make_shared<Node<ItemType>>(newEntry);
//Insert the new node
if (isEmpty())
frontPtr = newNodePtr; //The queue was empty
else
backPtr.get()->setNext(newNodePtr.get()); //The queue was not empty
backPtr = newNodePtr; //New node is at back
return true;
} //end enqueue
template<class ItemType>
bool LinkedQueue<ItemType>::dequeue()
{
bool result = false;
if (!isEmpty())
{
//Queue is not empty; remove front
auto nodeToDeletePtr = frontPtr;
if (frontPtr == backPtr)
{
//Special case: one node in queue
//Set frontPtr and backPtr to nullptr
frontPtr.reset();
backPtr.reset();
}
else{
std::shared_ptr<Node<ItemType>> newFront(frontPtr.get()->getNext());
frontPtr = newFront;
}
//Maintain an accurate reference count for first node
nodeToDeletePtr->setNext(nullptr);
//Removed node will be deallocated when ethod ends
result = true;
} //end if
return result;
} //end dequeue
template<class ItemType>
ItemType LinkedQueue<ItemType>::peekFront() const throw(PrecondViolatedExcept){
return 0;
}









