Skip to content

Commit 283ddb5

Browse files
authored
Merge pull request kelvins#120 from CleberSalustiano/patch-1
Create ListaEncadeada.c
2 parents d6c78b9 + 55b3f7f commit 283ddb5

File tree

2 files changed

+271
-1
lines changed

2 files changed

+271
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
4141
| [Fila Encadeada Dinâmica][19] | [C](./src/c/FilaEncadeadaDinamica.c) | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
4242
| [Grafo][20] | [C](./src/c/Grafos.c) | C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
4343
| [Lista Circular Ligada][52] | [C](./src/c/ListaCircularLigada.c) | C++ | Java | [Python](./src/python/lista_encadeada_circular.py) | Go | Ruby | JS | Pascal | Swift | Rust |
44-
| [Lista Encadeada][22] | C | [C++](./src/cpp/ListaEncadeada.cpp) | Java | [Python](./src/python/lista_encadeada.py) | Go | [Ruby](./src/ruby/Lista_encadeada.rb) | [JS](./src/javascript/ListaSimplesmenteEncadeada.js) | Pascal | Swift | [Rust](./src/rust/linked_list.rs) |
44+
| [Lista Encadeada][22] | [C](./src/c/ListaEncadeada.c) | [C++](./src/cpp/ListaEncadeada.cpp) | Java | [Python](./src/python/lista_encadeada.py) | Go | [Ruby](./src/ruby/Lista_encadeada.rb) | [JS](./src/javascript/ListaSimplesmenteEncadeada.js) | Pascal | Swift | [Rust](./src/rust/linked_list.rs) |
4545
| [Lista Duplamente Encadeada][23] | [C](./src/c/ListaDuplamenteEncadeada.c) | C++ | [Java](./src/java/ListaDuplamenteEncadeada.java) | [Python](./src/python/lista_duplamente_encadeada.py) | Go | [Ruby](./src/ruby/Lista_duplamente_encadeada.rb) | [JS](./src/javascript/ListaDuplamenteEncadeada.js) | Pascal | Swift | Rust |
4646
| [Lista Ligada Não Ordenada][24] | [C](./src/c/ListaLigadaNaoOrdenada.c) | C++ | Java | Python | [Go](src/go/listasequencialnaoordenada/listaSequencialNaoOrdenada.go) | Ruby | JS | Pascal | Swift | Rust |
4747
| Lista Sequencial Ordenada | [C](./src/c/ListaSequencialOrdenada.c) | C++ | Java | [Python](./src/python/lista_sequencial_ordenada.py) | [Go](src/go/listasequencialordenada/listaSequencialOrdenada.go) | Ruby | JS | Pascal | Swift | Rust |

src/c/ListaEncadeada.c

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
typedef struct tno {
5+
/*Caso queira usar isso para qualquer um use TAD
6+
E com o TAD defina: typedef void *tdado;
7+
E em todo local do código relacionado ao dado
8+
troque int por tdado.
9+
*/
10+
int dado;
11+
struct tno *anterior;
12+
struct tno *proximo;
13+
} tipoNo;
14+
15+
typedef tipoNo *pnoh;
16+
17+
typedef struct {
18+
int tamanho;
19+
pnoh primeiro;
20+
pnoh ultimo;
21+
} tcabec;
22+
23+
typedef tcabec *TLista;
24+
25+
26+
TLista criaLista() {
27+
TLista c = (tcabec *)malloc(sizeof(tcabec));
28+
29+
c->tamanho = 0;
30+
c->primeiro = NULL;
31+
c->ultimo = NULL;
32+
33+
return c;
34+
}
35+
36+
TLista appendLista(TLista lst, int dado) {
37+
pnoh novono = (tipoNo *)malloc(sizeof(tipoNo));
38+
39+
novono->dado = dado;
40+
novono->anterior = lst->ultimo;
41+
novono->proximo = NULL;
42+
43+
if(lst->tamanho == 0){
44+
lst->primeiro = novono;
45+
lst->ultimo = novono;
46+
}
47+
else {
48+
lst->ultimo->proximo = novono;
49+
lst->ultimo = novono;
50+
}
51+
52+
lst->tamanho++;
53+
54+
return lst;
55+
}
56+
57+
int lenLista(TLista lst) {
58+
return lst->tamanho;
59+
}
60+
61+
int primLista(TLista lst) {
62+
return lst->primeiro->dado;
63+
}
64+
65+
int ultLista(TLista lst) {
66+
return lst->ultimo->dado;
67+
}
68+
69+
TLista insertLista(TLista lst,int i, int dado) {
70+
int tam = lenLista(lst);
71+
72+
if((i < 0) || (i > tam)) return NULL;
73+
74+
if(lenLista(lst) == 0)
75+
appendLista(lst,dado);
76+
else
77+
if(i==tam)
78+
appendLista(lst,dado);
79+
else {
80+
if(i == 0) {
81+
pnoh novono = (tipoNo *)malloc(sizeof(tipoNo));
82+
novono->dado = dado;
83+
84+
novono->proximo = lst->primeiro;
85+
lst->primeiro = novono;
86+
}
87+
else {
88+
pnoh novono = (tipoNo *)malloc(sizeof(tipoNo));
89+
novono->dado = dado;
90+
91+
pnoh aux = lst->primeiro;
92+
int pos = 0;
93+
94+
while(pos != (i - 1)){
95+
aux = aux->proximo;
96+
pos++;
97+
}
98+
novono->proximo = aux->proximo;
99+
aux->proximo = novono;
100+
}
101+
102+
lst->tamanho++;
103+
}
104+
105+
return lst;
106+
}
107+
108+
pnoh infoLista(TLista lst,int i) {
109+
int tam = lenLista(lst);
110+
111+
if((tam == 0) || (i < 0) || (i > tam))
112+
return NULL;
113+
114+
if(i == 0) return lst->primeiro;
115+
116+
else if(i == tam - 1) return lst->ultimo;
117+
else {
118+
pnoh aux = lst->primeiro;
119+
int pos = 0;
120+
121+
while(pos != i){
122+
aux = aux->proximo;
123+
pos++;
124+
}
125+
126+
return aux;
127+
}
128+
}
129+
130+
void removeLista(TLista lst,int i) {
131+
int tam = lenLista(lst);
132+
133+
if((i < 0) || (i > tam) || (tam == 0))
134+
printf("Erro: indice inexistente dentro da Lista.");
135+
136+
if(tam == 1) {
137+
pnoh aux = lst->primeiro;
138+
lst->primeiro = NULL;
139+
lst->ultimo = NULL;
140+
lst->tamanho--;
141+
142+
int d =aux->dado;
143+
free(aux);
144+
145+
}
146+
else {
147+
if(i == 0){
148+
pnoh aux = lst->primeiro;
149+
lst->primeiro = aux->proximo;
150+
lst->tamanho--;
151+
152+
int d =aux->dado;
153+
free(aux);
154+
155+
}
156+
else {
157+
if(i == tam - 1) {
158+
pnoh aux = lst->ultimo;
159+
160+
pnoh penultimo = lst->primeiro;
161+
int pos = 0;
162+
163+
while(pos != i-1){
164+
penultimo = penultimo->proximo;
165+
pos++;
166+
}
167+
168+
penultimo->proximo = NULL;
169+
lst->ultimo = penultimo;
170+
171+
lst->tamanho--;
172+
173+
int d =aux->dado;
174+
free(aux);
175+
176+
}
177+
else {
178+
pnoh anterior = lst->primeiro;
179+
int pos = 0;
180+
181+
while(pos != i-1){
182+
anterior = anterior->proximo;
183+
pos++;
184+
}
185+
186+
pnoh aux = anterior->proximo;
187+
anterior->proximo = aux->proximo;
188+
lst->tamanho--;
189+
190+
int d = aux->dado;
191+
free(aux);
192+
193+
}
194+
}
195+
}
196+
}
197+
198+
int indexLista(TLista lst, int dado){
199+
int tam = lenLista(lst);
200+
int i, dadolst;
201+
pnoh no_dadolst;
202+
203+
if(tam == 0) return -1;
204+
205+
i = 0;
206+
no_dadolst = infoLista(lst,i);
207+
dadolst = no_dadolst->dado;
208+
while((i < tam) && (dado != dadolst)){
209+
i++;
210+
no_dadolst = infoLista(lst,i);
211+
dadolst = no_dadolst->dado;
212+
213+
}
214+
215+
if(i < tam) return i;
216+
else return -1;
217+
}
218+
219+
TLista clearLista(TLista lst){
220+
int tam = lenLista(lst);
221+
222+
if(tam == 0) return lst;
223+
224+
while(lenLista(lst) > 0)
225+
removeLista(lst,0);
226+
227+
return lst;
228+
}
229+
230+
TLista clonaLista(TLista lst){
231+
TLista clone = criaLista();
232+
int tam = lenLista(lst);
233+
234+
if(tam == 0) return clone;
235+
236+
for(int i=0;i < tam; i++)
237+
appendLista(clone,infoLista(lst,i));
238+
239+
return clone;
240+
}
241+
242+
int main(){
243+
TLista lista = criaLista();
244+
appendLista(lista, 3);
245+
appendLista(lista, 5);
246+
appendLista(lista, 7);
247+
printf("Lista criada e adicionado 3 numeros\n");
248+
int tamanho = lenLista(lista);
249+
int primeiro = primLista(lista);
250+
int ultimo = ultLista(lista);
251+
pnoh valor = infoLista(lista, 1);
252+
int valor_dado = valor->dado;
253+
printf("Tamanho da lista: %d\nPrimeiro da Lista: %d\nUltimo da Lista: %d\nSegundo valor: %d\n", tamanho, primeiro, ultimo, valor_dado);
254+
insertLista(lista, 2, 6);
255+
valor = infoLista(lista, 2);
256+
valor_dado = valor->dado;
257+
printf("Adicionando 6 na posição 2: %d\n", valor_dado);
258+
removeLista(lista, 1);
259+
tamanho = lenLista(lista);
260+
printf("Novo tamanho após adicionar e remover: %d\n", tamanho);
261+
int index = indexLista(lista, 3);
262+
printf("Index do elemento com valor 3 na lista: %d\n", index);
263+
TLista cloneLista = clonaLista(lista);
264+
printf("Lista Duplicada\n");
265+
clearLista(lista);
266+
267+
printf("Lista Apagada");
268+
269+
return 0;
270+
}

0 commit comments

Comments
 (0)