-
Notifications
You must be signed in to change notification settings - Fork 2
/
LList.cpp
57 lines (49 loc) · 959 Bytes
/
LList.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
/*
* LList.cpp
*
* Created on: Dec 3, 2016
* Author: rocco
*/
#include "LList.h"
LList::LList() : head(nullptr), sizeOfList(0) {}
void LList::add(double dataIn)
{
if (head == nullptr)
{
head = new Node(dataIn, nullptr);
head->setPos(0);
head->setNext(nullptr);
}
else
{
Node* prev = head;
Node* curr = head->getNext();
while(curr != nullptr)
{
prev = curr;
curr = curr->getNext();
}
curr = new Node(dataIn, nullptr);
int setPos = prev->getPos();
curr->setPos(++setPos);
prev->setNext(curr);
}
sizeOfList++;
}
double LList::getDataAt(int pos)
{
if (pos < 0 && pos > getSize()) { return -100; } // out of bounds access
else
{
bool done = false;
Node* temp = head;
while(!done && temp != nullptr)
{
if (temp->getPos() == pos) { done = true; }
else { temp = temp->getNext(); }
}
return temp->getPrice();
}
}
int LList::getSize() { return sizeOfList; }
LList::~LList() { delete head; }