-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
executable file
·161 lines (151 loc) · 3.06 KB
/
linked_list.cpp
File metadata and controls
executable file
·161 lines (151 loc) · 3.06 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "linked_list.h"
using namespace std;
linked_list::linked_list()
{
items = 0;
head = new node;
}
linked_list::~linked_list()
{
node *prox = NULL;
while(head != NULL) {
prox = head->next;
delete head;
head = prox;
}
}
bool linked_list::add(string value)
{
if (this->items == 0)
{
head->value.assign(value);
this->items++;
return true;
}
if (this->locate(value) == false)
{
node *nodo = new node;
nodo->value = value;
nodo->next = head;
head = nodo;
items++;
return true;
}
return false;
}
bool linked_list::adds(string value)
{
if (this->items == 0)
{
head->value.assign(value);
this->items++;
return true;
}
if (this->locate(value) == false)
{
node *temp;
temp = head;
while(temp != NULL)
{
if (value.compare(temp->value) < 0)
{
node *nodo = new node;
nodo->value.assign(temp->value);
temp->next = nodo;
temp->value.assign(value);
items++;
return true;
}
else
{
if (temp->next == NULL)
{
node *nodo = new node;
nodo->value.assign(value);
temp->next = nodo;
items++;
return true;
}
temp = temp->next;
}
}
}
return false;
}
bool linked_list::del(string value)
{
node *nodo;
if (head->value.compare(value) == 0)
{
if (head->next != NULL)
{
nodo = head;
head = head->next;
delete nodo;
items--;
return true;
}
else
{
head->value.assign("");
items--;
return true;
}
}
else
{
if (!head->next)
{
nodo = head->next;
while(nodo != NULL)
{
if (nodo->value.compare(value) == 0)
{
nodo->value.assign(nodo->next->value);
node *temp;
temp = nodo->next->next;
delete nodo->next;
nodo->next = temp;
items--;
return true;
}
}
}
}
return false;
}
bool linked_list::locate(string value)
{
node* nodo = this->head;
while(nodo != NULL)
{
if(nodo->value.compare(value) == 0)
{
return true;
}
else
nodo = nodo->next;
}
return false;
}
string linked_list::decapitate()
{
string item;
item.assign(head->value);
node *nodo;
nodo = head->next;
if (nodo != NULL)
{
delete head;
head = nodo;
}
else
{
head->value.assign("");
}
return item;
}
uint32_t linked_list::tam()
{
return items;
}