-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
58 lines (51 loc) · 1.43 KB
/
LinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "LinkedList.h"
LinkedList::LinkedList() {
headPtr = nullptr;
}
void LinkedList::addElement(listType d) {
Node *newNode = new Node;
newNode->data = d;
newNode->nextPtr = nullptr;
if (headPtr == nullptr) {
headPtr = newNode;
} else if (newNode->data.enterQTime < headPtr->data.enterQTime) {
newNode->nextPtr = headPtr;
headPtr = newNode;
} else {
Node *tempNode = headPtr;
while (tempNode->nextPtr != nullptr &&
tempNode->nextPtr->data.enterQTime < newNode->data.enterQTime) {
tempNode = tempNode->nextPtr;
}
newNode->nextPtr = tempNode->nextPtr;
tempNode->nextPtr = newNode;
}
}
void LinkedList::delElement() {
if (!listIsEmpty()) {
Node *tempNode = headPtr;
headPtr = headPtr->nextPtr;
delete tempNode;
tempNode = nullptr;
}
}
listType LinkedList::peek() {
listType tempData;
if (!listIsEmpty()) {
tempData = headPtr->data;
}
return tempData;
}
bool LinkedList::listIsEmpty() {
return headPtr == nullptr;
}
void LinkedList::printList() {
Node *tempNode = headPtr;
while (tempNode != nullptr) {
cout << "CART #" << tempNode->data.cartID
<< " -- Item Count: " << tempNode->data.itemCount
<< " -- Enter Queue Time: " << tempNode->data.enterQTime
<< endl;
tempNode = tempNode->nextPtr;
}
}