forked from MoustafaMagdy10/Online-wallet-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
110 lines (88 loc) · 1.67 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
105
106
107
108
109
110
#include "LinkedList.h"
LinkedList::Node::Node()
{
this->next = nullptr;
}
LinkedList::Node::Node(const string &name)
{
this->value = name;
this->next = nullptr;
}
void LinkedList::addSuggestion(const string &name)
{
if (Person::getUserByName(name) == nullptr)
return;
int x = count;
Node *tmp = head;
if (head && head->value == name)
{
head = head->next;
delete tmp;
count--;
}
else
{
while (tmp && tmp->next)
{
if (tmp->next->value == name)
{
Node *del = tmp->next;
tmp->next = del->next;
if (tail == del)
{
tail = tmp;
}
delete del;
count--;
}
tmp = tmp->next;
}
}
tmp = head;
if (count == 5)
{
head = head->next;
delete tmp;
count--;
}
Node *newNode = new Node(name);
if (head == 0)
head = tail = newNode;
else
{
tail->next = newNode;
tail = newNode;
}
count++;
}
stack<string> LinkedList::getSuggestions()
{
stack<string> suggestions;
Node *tmp = head;
while (tmp)
{
if (Person::getUserByName(tmp->value) != nullptr)
suggestions.push(tmp->value);
tmp = tmp->next;
}
return suggestions;
}
int LinkedList::length()
{
return count;
}
LinkedList::LinkedList()
{
head = tail = 0;
count = 0;
}
LinkedList::~LinkedList()
{
while (head != nullptr)
{
Node *temp = head;
head = head->next;
delete temp;
count--;
}
}