-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlista_encadeada.cpp
More file actions
140 lines (119 loc) · 3.47 KB
/
lista_encadeada.cpp
File metadata and controls
140 lines (119 loc) · 3.47 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
#include <iostream>
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
int size;
public:
LinkedList() {
head = nullptr;
size = 0;
}
~LinkedList() {
Node* current = head;
while (current) {
Node* temp = current;
current = current->next;
delete temp;
}
}
bool isEmpty() {
return size == 0;
}
int getSize() {
return size;
}
int getElement(int position) {
if (position < 1 || position > size) {
std::cerr << "Posicao invalida!" << std::endl;
return -1;
}
Node* current = head;
for (int i = 1; i < position; i++) {
current = current->next;
}
return current->data;
}
void setElement(int position, int value) {
if (position < 1 || position > size) {
std::cerr << "Posicao invalida!" << std::endl;
return;
}
Node* current = head;
for (int i = 1; i < position; i++) {
current = current->next;
}
current->data = value;
}
void insert(int position, int value) {
if (position < 1 || position > size + 1) {
std::cerr << "Posicao invalida!" << std::endl;
return;
}
Node* newNode = new Node{value, nullptr};
if (position == 1) {
newNode->next = head;
head = newNode;
} else {
Node* current = head;
for (int i = 1; i < position - 1; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
size++;
}
void remove(int position) {
if (position < 1 || position > size) {
std::cerr << "Posicao invalida!" << std::endl;
return;
}
Node* temp;
if (position == 1) {
temp = head;
head = head->next;
} else {
Node* current = head;
for (int i = 1; i < position - 1; i++) {
current = current->next;
}
temp = current->next;
current->next = temp->next;
}
delete temp;
size--;
}
void print() {
Node* current = head;
while (current) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "NULL" << std::endl;
}
};
int main() {
LinkedList list;
std::cout << "Criacao da lista vazia." << std::endl;
std::cout << "Verificar se a lista esta vazia: " << (list.isEmpty() ? "Sim" : "Nao") << std::endl;
std::cout << "Obter o tamanho da lista: " << list.getSize() << std::endl;
list.insert(1, 10);
list.insert(2, 20);
list.insert(3, 30);
list.insert(2, 15);
std::cout << "Lista apos insercoes:" << std::endl;
list.print();
std::cout << "Obter o tamanho da lista: " << list.getSize() << std::endl;
list.remove(2);
std::cout << "Lista apos remocao na posicao 2:" << std::endl;
list.print();
std::cout << "Elemento na posicao 2: " << list.getElement(2) << std::endl;
list.setElement(2, 25);
std::cout << "Lista apos modificar o elemento na posicao 2:" << std::endl;
list.print();
return 0;
}