-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path8.17 LAB Inventory (linked lists insert at the front of a list).txt
87 lines (69 loc) · 1.83 KB
/
8.17 LAB Inventory (linked lists insert at the front of a list).txt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
----main.cpp----
#include "InventoryNode.h"
int main() {
int count;
int numItems;
string item;
InventoryNode *headNode = new InventoryNode();
InventoryNode *currNode;
// Obtain number of items
cin >> count;
// Get each item and number of each
for (int i = 0; i < count; i++) {
cin >> item;
cin >> numItems;
currNode = new InventoryNode(item, numItems);
currNode->InsertAtFront(headNode, currNode);
}
// Print linked list
currNode = headNode->GetNext();
while (currNode != NULL) {
currNode->PrintNodeData();
currNode = currNode->GetNext();
}
return 0;
}
----InventoryNode.h----
#include <iostream>
#include <string>
using namespace std;
class InventoryNode {
private:
string item;
int numberOfItems;
InventoryNode *nextNodeRef;
public:
//Constructor
InventoryNode() {
this->item = "";
this->numberOfItems = 0;
this->nextNodeRef = NULL;
}
//Constructor
InventoryNode(string itemInit, int numberOfItemsInit) {
this->item = itemInit;
this->numberOfItems = numberOfItemsInit;
this->nextNodeRef = NULL;
}
//Constructor
InventoryNode(string itemInit, int numberOfItemsInit, InventoryNode nextLoc) {
this->item = itemInit;
this->numberOfItems = numberOfItemsInit;
this->nextNodeRef = &nextLoc;
}
// TODO: Define an insertAtFront() method that inserts a node at the
// front of the linked list (after the dummy head node)
InventoryNode* InsertAtFront(InventoryNode *headNode, InventoryNode*currNode){
currNode->nextNodeRef = headNode->nextNodeRef;
headNode->nextNodeRef = currNode;
return currNode;
}
//Get the next node
InventoryNode *GetNext() {
return this->nextNodeRef;
}
//Print node data
void PrintNodeData() {
cout << this->numberOfItems << " " << this->item << endl;
}
};