-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
104 lines (90 loc) · 1.83 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include "linkedlist.hpp"
using namespace std;
LinkedList::LinkedList()
{
head = nullptr;
tail = nullptr;
numberOfEntries = 0;
}
LinkedList::LinkedList(char *_listTitle)
{
head = nullptr;
tail = nullptr;
numberOfEntries = 0;
listTitle = _listTitle;
}
LinkedList::~LinkedList()
{
Node *current = head;
while (current != nullptr)
{
Node *next = current->next;
delete current;
current = next;
}
head = nullptr;
}
void LinkedList::AddListTitle(const char *text)
{
listTitle = text;
}
void LinkedList::AddListItem(const char *text)
{
Node *tmp = new Node;
tmp->itemText = text;
tmp->id = numberOfEntries++;
tmp->next = nullptr;
if (head == nullptr)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tmp;
}
}
void LinkedList::PrintList()
{
cout << listTitle << endl;
Node *current = head;
while (current != nullptr)
{
cout << current->id << ": " << current->itemText << endl;
current = current->next;
}
}
void LinkedList::PrintListWithMarkerFromTo(int start, int end, int marker)
{
cout << listTitle << endl;
Node *current = GotoId(start);
int count = 1;
while (current != nullptr)
{
if (count == end)
break;
if (current->id == marker)
{
cout << "> " << current->itemText << endl;
}
else
{
cout << " " << current->itemText << endl;
}
current = current->next;
count++;
}
}
Node *LinkedList::GotoId(int lookupId)
{
if (lookupId > numberOfEntries)
return nullptr;
Node *current = head;
while (current != nullptr && lookupId != current->id)
{
current = current->next;
}
return current;
}