-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-lista-simples.c
138 lines (111 loc) · 2.66 KB
/
1-lista-simples.c
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
//
// main.c
// listas-aula1
//
// Created by Natarajan Ferreira on 13/05/15.
// Copyright (c) 2015 Natarajan Ferreira. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct noh{
int dado;
struct noh * proximo;
struct noh * anterior;
} Node;
Node * novo(int dado);
Node * novo(int dado){
Node * n = (Node *) malloc (sizeof(Node));
n->dado = dado;
n->proximo = NULL;
n->anterior = NULL;
return n;
}
typedef struct lista{
int tamanho;
Node *first;
Node *last;
} Lista;
Node* find (Node* head, int dado);
Node* find (Node* head, int dado){
Node * current = head;
while (current != NULL){
if (current->dado == dado){
return current;
}
current = current->proximo;
}
return NULL;
}
int removeNode (Node * node, int dado){
if (node->dado == dado){
Node * aux = node;
node = node->proximo;
node->anterior = NULL;
free(aux);
return 1;
} else {
Node * aux = node;
while (aux != NULL && aux->dado != dado)
aux = aux->proximo;
if (aux->dado == dado){
Node * temp = aux->proximo;
aux->proximo = temp->proximo;
free (temp);
return 0;
} else {
return 1;
}
}
}
//addAt de LIsta duplamente encadeada
int addAt(Node * l, int data, int position);
int addAt(Node * l, int data, int position){
if (position < 0)
return 0;
Node * aux = l;
int cont = 0;
while (cont < position && aux != NULL){
if (aux->proximo != NULL)
aux = aux->proximo;
cont++;
}
Node * newNode = novo(data);
if (aux->anterior == NULL){
newNode->proximo = l;
l = newNode;
return 1;
} else {
Node * temp1 = aux->anterior;
temp1->proximo = newNode;
newNode->anterior = temp1;
newNode->proximo = aux;
aux->anterior = newNode;
return 1;
}
}
void imprime(Node *first);
void imprime(Node *first){
Node *aux = first;
while (aux) {
printf("%d -> ", aux->dado);
aux = aux->proximo;
}
printf(" //");
}
Node * adiciona (Node *l, int novoDado);
Node * adiciona (Node *l, int novoDado){
Node *novoNode = novo(novoDado);
novoNode->proximo = l;
l->anterior = novoNode;
return novoNode;
}
int main(int argc, const char * argv[]) {
//Lista * minhaLista = (Lista *) malloc(sizeof(Lista));
Node *p = novo(1);
//addAt(p, 2, 0);
//addAt(p, 3, 3);
p = adiciona(p, 2);
p = adiciona(p, 3);
imprime(p);
return 0;
}