My problem is that when I run the simulation for the bank it keeps removing the wrong customers from the Queues at incorrect departure times. Also when it reaches the closing time I receive a nulltpr error when trying to finish departure events and customers left in the queue and list.
#include "EventList.h"
#include <stdlib.h>
#include <iostream>
usingnamespace std;
EventList::EventList()
{
front = nullptr;
last = nullptr;
numOfEvents = 0;
}
EventList::~EventList()
{
Event* current = front;
Event* tmp;
numOfEvents = 0;
while (current != NULL)
{
tmp = current;
current = current->next;
delete tmp;
}
}
Event* EventList::getFront()
{
return front;
}
//checks to see if the list is empty. returns true if empty false if not
bool EventList::isEmpty()
{
if (front == NULL)
returntrue;
returnfalse;
}
Event* EventList::removeEvent()
{
if (isEmpty())
cout << "Cannot remove event from empty list" << endl;
Event* tmp = front;
if (tmp->next != NULL)
{
tmp = tmp->next;
deleteFront();
}
return tmp;
}
//inserts a node to the front of a ListNode object (list). checks if list is empty.
void EventList::frontInsert(Event* newFront)
{
//may have to create node first test this!!
if (newFront->next != NULL || newFront->occurTime >= 0)
{
if (isEmpty() == true) {
newFront->next = front;
front = newFront;
last = newFront;
}
else
{
newFront->next = front;
front = newFront;
}
numOfEvents++;
}
else cout << "error this node either has no data or is connected to something!!" << endl;
}
//method to insert at the end of the list. uses last node as reference to insert the new node after.
//error checking for adding a node with no data
void EventList::insertEnd(Event* newTail)
{
if (isEmpty() == true)
frontInsert(newTail);
else {
last->next = newTail;
last = newTail;
numOfEvents++;
}
}
//method to insert a node into the nth position in a list
void EventList::midInsert(Event* event, Event* nextEvent)
{
Event* node = event;
node->occurTime = event->occurTime;
node->type = event->type;
node->next = NULL;
Event* tmp = front;
if (tmp == NULL)
insertEnd(node);
while (tmp->next != nextEvent)
{
tmp = tmp->next;
}
event->next = nextEvent;
tmp->next = event;
numOfEvents++;
}
//method to delete a node from the front of the list.
void EventList::deleteFront()
{
if (isEmpty() == false)
{
Event* newFront = front->next;
delete front;
front = newFront;
numOfEvents--;
}
else cout << "Cannot delete from empty list!" << endl;
}
//method to delete a node from the end of the list.
//traverse the list to node before the last node.
void EventList::deleteEnd()
{
if (isEmpty() == true)
cout << "Empty list" << endl;
if (front->next == NULL)
deleteFront();
Event* tmp = front;
while (tmp->next != last)
{
tmp = tmp->next;
}
delete last;
last = tmp;
last->next = NULL;
numOfEvents--;
}
//method to delete a node from a point in the list. parameter passed is the node to be deleted from the list.
//traverse the list to the node before the target node. update previous node's pointer to delNode->next then delete target node
void EventList::deleteMid(Event* delNode)
{
Event* p = front;
if (p == NULL)
cout << "error Empty List!!" << endl;
if (delNode == last)
deleteEnd();
while (p->next != delNode)
{
p = p->next;
}
p->next = delNode->next;
delete delNode;
delNode = NULL;
numOfEvents--;
}
void EventList::sort(Event* head)
{
Event* p = head;
Event* current = p;
Event* next = p->next;
int val;
if (head == NULL)
cout << "list is empty" << endl;
while (p != last)
{
while (next != NULL)
{
if (current->occurTime > next->occurTime)
{
val = current->occurTime;
current->occurTime = next->occurTime;
next->occurTime = val;
current = p;
next = p->next;
}
else {
current = current->next;
next = next->next;
}
}
p = p->next;
}
}
constvoid EventList::print(Event* listFront)
{
Event* p = listFront;
if (p == NULL)
cout << "NULL" << endl;
else
{
cout << p->occurTime << " ";
print(p->next);
}
}
Event* EventList::addNode(int num, int type)
{
Event* newEvent = new Event;
newEvent->occurTime = num;
newEvent->type = type;
newEvent->next = NULL;
insertEnd(newEvent);
return front;
}
void EventList::updateLast()
{
while (last->next != NULL)
last = last->next;
}
void EventList::clearList()
{
if (isEmpty() == true)
cout << "list is empty" << endl;
Event* del = front;
Event* p = front;
while (p != last)
{
p = p->next;
delete del;
del = p;
}
}