-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
119 lines (107 loc) · 1.94 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
111
112
113
114
115
116
117
118
119
#include "LinkedList.h"
#include "HierarchyHeaders.h"
#include <iomanip>
Node* LinkedList::getLastNode(void)
{
Node *p;
for (p = head; p->next != nullptr; p = p->next);
return p;
}
LinkedList::LinkedList(void)
{
this->head = nullptr;
size = 0;
}
LinkedList::LinkedList(const LinkedList &o)
{
for (Node *p = o.head; p != nullptr; p = p->next)
addBack(p->data);
}
LinkedList::~LinkedList(void)
{
size = 0;
while (head != nullptr)
{
Node *p = head;
head = head->next;
delete p;
}
}
uint LinkedList::getSize(void)
{
return size;
}
void LinkedList::addBack(Vehicul* data)
{
if (++size == 1)
head = new Node(data, nullptr);
else
{
Node *p = getLastNode();
Node *q = new Node(data, nullptr);
p->next = q;
}
}
void LinkedList::addFront(Vehicul* data)
{
if(++size == 1)
head = new Node(data, nullptr);
else
{
Node *p = new Node;
p->data = data;
p->next = head;
head = p;
}
}
bool LinkedList::remove(char *numar)
{
Node *p = contains(numar);
if (p != nullptr)
{
--size;
Node *q = p->next;
p->next = q->next;
delete q;
return true;
}
return false;
}
Node* LinkedList::contains(char *numar)
{
Node *p, *q = head;
for (p = head; p != nullptr; p = p->next)
{
AutoVehicul *av = dynamic_cast<AutoVehicul*>(p->data);
if (Util::stringsEqual(av->getNumar(), numar))
return q;
q = p;
}
return nullptr;
}
void LinkedList::showContent(void)
{
int W = 20;
cout << std::setw(W) << "MARCA";
cout << std::setw(W) << "MODEL";
cout << std::setw(W) << "NUMAR" << enter;
AutoVehicul *av = nullptr;
myIterator it;
for (it = begin(); it != end(); ++it)
{
av = dynamic_cast<AutoVehicul*>(*it);
cout << std::setw(W) << av->getMarca();
cout << std::setw(W) << av->getModel();
cout << std::setw(W) << av->getNumar() << enter;
}
cout << enter;
}
LinkedList& LinkedList::operator = (const LinkedList &o)
{
if (this != &o)
{
for (Node *p = o.head; p != nullptr; p = p->next)
addBack(p->data);
}
return *this;
}