Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.
-
Upload
benjamin-frazier -
Category
Documents
-
view
219 -
download
1
Transcript of Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.
![Page 1: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/1.jpg)
Exercicios sobre a matéria da P3 de 2006.1
Listas, Árvores e Tabela de Dispersão
![Page 2: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/2.jpg)
ex_p3.c
![Page 3: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/3.jpg)
int intervalo(Arv* a, int x1, int x2) { if (a==NULL) return 0; if (a->info>x2) { return intervalo(a->esq,x1,x2); } else if (a->info<x1) { return intervalo(a->dir,x1,x2); } else { /* pertence ao intervalo */ return 1 + intervalo(a->esq,x1,x2) + intervalo(a->dir,x1,x2); }}
![Page 4: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/4.jpg)
hashAluno.c
![Page 5: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/5.jpg)
int busca(Hash tab, char* nome){ int i = hash(nome); Aluno* p; for (p=tab[i];p!=NULL;p=p->prox) { if (strcmp(p->nome,nome)==0) return p->quant; } return -1;}
int hash(char* nome){ int i,soma=0; for (i=0;nome[i]!=‘\0’;i++) soma=(soma+nome[i])%N; return soma;}
![Page 6: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/6.jpg)
lista2.c
![Page 7: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/7.jpg)
struct lista { char nome[81]; float nota; struct lista* prox};typedef struct lista Lista;
Lista* insere (Lista* lst, char* nome, float nota){
Lista* novo=(Lista*) malloc(sizeof(Lista));strcpy(novo->nome,nome);novo->nota=nota;novo->prox=lst;return novo;
}
![Page 8: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/8.jpg)
![Page 9: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/9.jpg)
Lista* retira_ultimo(Lista* lst){ if (lst==NULL) return NULL; else{ Lista* ant=NULL; Lista* p=lst; while (p->prox!=NULL) { ant=p; p=p->prox; } free(p);
if (ant!=NULL) { ant->prox=NULL; return lst; } else return NULL; }}
![Page 10: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/10.jpg)
![Page 11: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/11.jpg)
int cheia(Arv* a){ if (a==NULL) return 1; if (a->esq==NULL&&a->dir==NULL) return 1; if (a->esq==NULL||a->dir==NULL) return 0; return cheia(a->esq)&&cheia(a->dir);}
![Page 12: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/12.jpg)
![Page 13: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/13.jpg)
int maximo(ArvGen* a){ if (a->prim==NULL) return a->info; else { int max=a->info; ArvGen* p; for (p=a->prim;p!=NULL;p=p->prox) { int mp=maximo(p); max=(max>mp)?max:mp; } return max; }}
![Page 14: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/14.jpg)
![Page 15: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/15.jpg)
Lista* constroi(int n, int* v){ int i; Lista* head=NULL; for (i=0;i<n;i++) { Lista* novo=(Lista*) malloc(sizeof(Lista)); novo->info=v[i]; novo->prox=head; head=novo; } return head;}
![Page 16: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/16.jpg)
![Page 17: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/17.jpg)
int soma_info_folhas (Arv* a){ if (a==NULL ) return 0; else if (a->esq==NULL&&a->dir==NULL) return a->info; else return soma_info_folhas(a->esq)+soma_info_folhas(a->dir);}
![Page 18: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/18.jpg)
![Page 19: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/19.jpg)
int num_nos_x(ArvGen* a,int x){ if (a->prim==NULL) return (a->info>x)?1:0; else { ArvGen* p; int n=(a->info>x)?1:0; for (p=a->prim; p!=NULL; p=p->prox) { n+=num_nos_x(p,x); } return n; }}
![Page 20: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/20.jpg)
![Page 21: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/21.jpg)
void imprime (Arv* a){ if (!arv_vazia(a)){ imprime(a->esq); /* mostra sae */ printf("%d ", a->info); /* mostra raiz */ imprime(a->dir); /* mostra sad */ }}
![Page 22: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/22.jpg)
![Page 23: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/23.jpg)
/* conta o numero de nos ate o nivel n */int conta (Arv* a, int n){
if (a==NULL || n==-1) return 0; else
return 1+conta(a->esq,n-1)+conta(a->dir,n-1);}
![Page 24: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/24.jpg)
![Page 25: Exercicios sobre a matéria da P3 de 2006.1 Listas, Árvores e Tabela de Dispersão.](https://reader033.fdocumentos.tips/reader033/viewer/2022061305/55145fd5550346b0158b485c/html5/thumbnails/25.jpg)
void imprime (ArvGen* a){ ArvGen* p; printf("<%d",a->info); for (p=a->prim; p!=NULL; p=p->prox) imprime(p); /* imprime cada sub-árvore filha */ printf(">");}