-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
63 lines (55 loc) · 1.06 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
59
60
61
62
/*
* LinkedList.cpp
*
* Created on: Apr 21, 2020
* Author: jaken
*/
#include "LinkedList.h"
LinkedList::LinkedList() {
head = NULL;
}
void LinkedList::insert(int key) {
LLNode* toInsert = new LLNode; toInsert->key = key;
if(head == NULL) {
head = toInsert;
return;
}
LLNode* temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = toInsert;
}
LLNode* LinkedList::search(int key) {
if(head == NULL) {
std::cout << "List is empty. Cannot search." << std::endl;
return NULL;
}
LLNode* temp = head;
while(temp != NULL) {
if(temp->key == key) {
return temp;
}
temp = temp->next;
}
std::cout << "Value not found." << std::endl;
return NULL;
}
void LinkedList::displayList() {
LLNode* temp = head;
std::string fullList = "";
int n = 0;
while(temp != NULL) {
std::cout << temp->key << " ";
n++;
if(n == 100) {
//Eclipse IDE doesn't like me printing the list in one line.
std::cout << std::endl;
n = 0;
}
temp = temp->next;
}
}
LinkedList::~LinkedList() {
// TODO Auto-generated destructor stub
}