-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListaEncadeadaSimples.cpp
More file actions
178 lines (146 loc) · 3.55 KB
/
ListaEncadeadaSimples.cpp
File metadata and controls
178 lines (146 loc) · 3.55 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int elemento;
struct Node *proximo;
};
int tam;
void inicia(struct Node *List);
void menu();
void opcao(struct Node *List,char op);
void adiciona(struct Node *List);
void listaElementos(struct Node *List);
void excluir(struct Node *List);
int verificaCod(struct Node *List,int n);
int main(){
Node *Lista = (Node *)malloc(sizeof(Node));
inicia(Lista);
char op;
do{
menu();
scanf("%c",&op);
opcao(Lista,op);
}while(op!='0');
free(Lista);
}
void inicia(struct Node *List){
List->proximo = NULL;
tam=0;
}
void menu(){
system("cls");
printf("1 - Cadastrar elemento\n");
printf("2 - Excluir elemento\n");
printf("3 - Consultar elemento\n");
printf("4 - Listar elementos\n");
printf("0 - Sair\n");
printf("> ");
}
void opcao(struct Node *List,char op){
switch (op){
case '1':
adiciona(List);
break;
case '2':
excluir(List);
break;
case '3':
//consultar(List);
break;
case '4':
listaElementos(List);
break;
}
}
int verificaCod(struct Node *List,int n){
Node *temp;
temp = List->proximo;
while(temp!=NULL){
if(temp->elemento==n){
return 1;
}
temp = temp->proximo;
}
return 0;
}
void adiciona(Node *List){
Node *novo = (Node *)malloc(sizeof(Node));
if(!novo){
printf("Sem memoria disponivel!\n");
system("pause");
exit(1);
}else{
do{
system("cls");
printf("Novo elemento: ");
scanf("%d", &novo->elemento);
if(verificaCod(List,novo->elemento)){
printf("\nJa existe este elemento!");
system("pause");
}
}while (verificaCod(List,novo->elemento));
}
novo->proximo = NULL;
if(List->proximo == NULL){
List->proximo = novo;
}else{
Node *temp = List->proximo;
while(temp->proximo!=NULL ){
temp = temp->proximo;
}
temp->proximo = novo;
}
tam++;
}
void excluir(Node *List){
system("cls");
//Pergunta o código do elemento
if(List->proximo==NULL){
printf("Sem elementos cadastrados!\n");
system("pause");
return;
}
int num_elemento;
printf("Digite o codigo do elemento para excluir: ");
scanf("%d",&num_elemento);
Node *temp;
int flag=0, i=0;
temp = List->proximo;
while(temp!=NULL and flag==0){
if(temp->elemento==num_elemento){
printf("Elemento Encontrado!\n");
flag =1;
system("pause");
Node *atual=List->proximo, *anterior=List;
for(int x=0;x<i;x++){
anterior = atual;
atual = atual->proximo;
}
anterior->proximo=atual->proximo;
tam--;
return;
}
temp = temp->proximo;
i++;
}
printf("Elemento Nao Cadastrado!\n");
system("pause");
}
void listaElementos(Node *List){
system("cls");
if(List->proximo == NULL){
printf("Sem elementos cadastrados!\n");
system("pause");
return;
}
Node *temp;
temp = List->proximo;
printf("Elementos: \n");
while(temp!=NULL){
printf("%d ",temp->elemento);
temp = temp->proximo;
}
printf("\nQuantidade de Elementos: %d",tam);
printf("\n");
system("pause");
}