Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <vector>
- // the problem with this program is in the stackTop not with the rest of it
- using namespace std;
- template <typename T>
- class stackNode {
- public:
- T data;
- stackNode<T> *nextNode = nullptr;
- };
- template <typename T>
- class stackTop {
- public:
- stackNode<T> *top, *bottom;
- int sizeOfList = 0;
- stackTop() {
- bottom = nullptr;
- top = nullptr;
- }
- void display()
- {
- stackNode<T> *temp = new stackNode<T>;
- temp = bottom;
- while (temp != NULL)
- {
- cout << temp->data << endl;
- temp = temp->nextNode;
- }
- }
- void addNode(T userData) {
- stackNode<T> *tempNode = new stackNode<T>;
- tempNode->data = userData;
- tempNode->nextNode = nullptr;
- if (bottom == nullptr) {
- bottom = tempNode;
- top = tempNode;
- }
- else {
- bottom->nextNode = tempNode;
- bottom = tempNode;
- }
- sizeOfList++;
- }
- void pop()
- {
- sizeOfList--;
- stackNode<T> *current = new stackNode<T>;
- stackNode<T> *previous = new stackNode<T>;
- current = top;
- while (current->nextNode != NULL)
- {
- previous = current;
- current = current->nextNode;
- }
- top = previous;
- previous->nextNode = nullptr;
- delete current;
- }
- T last() {
- stackNode<T> *temp = new stackNode<T>;
- temp = top;
- T returnObject = temp->data;
- return returnObject;
- }
- };
- struct personInLine {
- public:
- int timeInLine;
- int timeLeft;
- int ID;
- };
- stackTop<personInLine> createPILVector(int numberOfPeople) {
- stackTop<personInLine> returnObject;
- int counter = 0;
- personInLine tempPerson;
- for (int i = 0; i < numberOfPeople; i++) {
- tempPerson.timeInLine = rand() % 4 + 1;
- cout << "[" << tempPerson.timeInLine << "]" << endl;
- tempPerson.timeLeft = tempPerson.timeInLine;
- tempPerson.ID = counter++;
- returnObject.addNode(tempPerson);
- }
- return returnObject;
- }
- struct envVariables {
- int time = 0;
- int maxTime = 120;
- int numberOfPeople = 0;
- };
- struct varPair {
- envVariables pairVariables;
- stackTop<personInLine> mainList;
- };
- varPair runTimer(stackTop<personInLine> userInput) {
- varPair returnObject;
- returnObject.mainList = userInput;
- while (returnObject.pairVariables.time < returnObject.pairVariables.maxTime) {
- if (returnObject.mainList.sizeOfList > 0) {
- returnObject.pairVariables.time = returnObject.pairVariables.time + returnObject.mainList.top->data.timeInLine;
- cout << returnObject.mainList.top->data.timeInLine << endl;
- returnObject.mainList.pop();
- returnObject.pairVariables.numberOfPeople++;
- }
- }
- return returnObject;
- }
- void main(){
- varPair main = runTimer(createPILVector(100));
- cout << main.pairVariables.time << endl << main.pairVariables.numberOfPeople << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment