-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (133 loc) · 2.68 KB
/
main.cpp
File metadata and controls
140 lines (133 loc) · 2.68 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>
using namespace std;
struct t_no {
int id;
struct t_no *prox;
struct t_no *anterior;
} typedef no;
void imprimir_frente(no *cabeca) {
no *aux = cabeca;
cout << "lista frente............." << endl;
while (aux != NULL) {
cout << aux->id << endl;
aux = aux->prox;
}
}
void imprimir_atras(no *cabeca) {
no *aux = cabeca;
cout << "lista atras:.............." << endl;
;
while (aux != NULL) {
cout << aux->id << endl;
aux = aux->anterior;
}
}
void buscar_frente(no *cabeca, int num) {
no *aux = cabeca;
while (aux != NULL) {
if (num == aux->id) {
cout << "o item " << aux->id << " esta na lista ";
return;
}
aux = aux->prox;
}
cout << "o item nao esta na lista";
}
void buscar_atras(no *cabeca, int num) {
no *aux = cabeca;
while (aux != NULL) {
if (num == aux->id) {
cout << "o item " << aux->id << " esta na lista ";
return;
}
aux = aux->anterior;
}
cout << "o item nao esta na lista";
}
int inserir_frente(no **cabeca, int id) {
no *aux = (no *)malloc(sizeof(no));
no *temp = NULL;
aux->id = id;
aux->prox = NULL;
aux->anterior = NULL;
if (*cabeca == NULL) {
*cabeca = aux;
} else {
temp = *cabeca;
while (temp->prox != NULL) {
temp = temp->prox;
}
temp->prox = aux;
}
return 1;
}
int inserir_atras(no **cabeca, int id) {
no *aux = (no *)malloc(sizeof(no));
no *temp = NULL;
aux->id = id;
aux->prox = NULL;
aux->anterior = NULL;
if (*cabeca == NULL) {
*cabeca = aux;
} else {
temp = *cabeca;
while (temp->anterior != NULL) {
temp = temp->anterior;
}
temp->anterior = aux;
}
return 1;
}
int excluir_frente(no **cabeca, int id) {
no *temp = *cabeca;
no *ant = NULL;
while (temp != NULL) {
if (temp->id == id) {
if (ant != NULL) {
ant->prox = temp->prox;
} else {
*cabeca = temp->prox;
if (temp != NULL) {
temp->prox->anterior = NULL;
}
}
free(temp);
return 1;
}
else{
ant = temp;
temp = temp->prox;
}
}
return 0;
}
int excluir_atras(no **cabeca, int id) {
no *temp = *cabeca;
no *proximo = NULL;
while (temp != NULL) {
if (temp->id == id) {
if (proximo != NULL) {
proximo->anterior = temp->anterior;
} else {
*cabeca = temp->anterior;
}
free(temp);
return 1;
}
else{
proximo = temp;
temp = temp->anterior;
}
}
return 0;
}
int main() {
no *lista = NULL;
inserir_frente(&lista, 10);
inserir_atras(&lista, 3);
inserir_frente(&lista, 20);
imprimir_frente(lista);
imprimir_atras(lista);
buscar_atras(lista, 3);
return 1;
}