-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocura_valor.cpp
116 lines (101 loc) · 2.18 KB
/
procura_valor.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct lista{
int info;
struct lista*prox;
}Lista;
Lista* lista_cria();
Lista* lista_insere(Lista* l, int i);
void lista_imprime(Lista* l);
int lista_vazia(lista* l);
Lista* lista_busca(Lista* l, int v);
Lista* lista_retira(Lista*l, int v);
void lista_libera(Lista* l);
int procuraValor(Lista* p, int busca);
int main(){
Lista* l;
l=lista_cria();
int op;
int x;
char resp;
int num= rand()%30+1;
do {
system("cls");
srand(time(NULL));
printf("Gerando elementos da lista: \n");
for(int i=0;i<num;i++)
l=lista_insere(l,(rand()%100+1));
lista_imprime(l);
printf("\n Digite o elemento para busca: ");
scanf("%d",&x);
if(procuraValor(l,x)){
printf(" VALOR ENCONTRADO !\n");
}
else{
printf(" VALOR NAO ENCONTRADO !\n");
}
printf("\nDeseja continuar??(s/n): \n");
scanf("%s", & resp);
}while (resp == 's');
system("pause");
return 0;
}
Lista* lista_cria(void){
return NULL;
}
Lista* lista_insere(Lista* l, int i){
Lista* novo= (Lista*) malloc(sizeof(Lista));
novo->info=i;
novo->prox=l;
return novo;
}
void lista_imprime(Lista* l){
Lista* p;
for(p=l;p!=NULL;p=p->prox)
printf(" info = %d\n", p->info);
}
int lista_vazia(lista* l){
return l==NULL;
}
Lista* lista_busca(Lista* l, int v){
Lista* p;
for(p=l;p!=NULL;p=p->prox){
if(p->info=v)
return p;
}
return NULL;
}
Lista* lista_retira(Lista*l, int v){
Lista* ant=NULL;
Lista* p=l;
while(p!=NULL && p->info!=v){
ant=p;
p=p->prox;
}
if(p==NULL)
return l;
if(ant==NULL){
l=p->prox;
}
else{
ant->prox=p->prox;
}
free(p);
return l;
}
void lista_libera(Lista* l){
Lista* p=l;
while(p!=NULL){
Lista* t=p->prox;
free(p);
p=t;
}
}
int busca_seq_lista_rec(Lista* p, int busca){
if (busca==p->info)
return 1;
if (p->prox==NULL)
return 0;
return busca_seq_lista_rec(p=p->prox,busca);
}