• Nenhum resultado encontrado

AEDS-aula-lista

N/A
N/A
Protected

Academic year: 2021

Share "AEDS-aula-lista"

Copied!
29
0
0

Texto

(1)

Listas

Leonardo Reis

Departamento de Computação e Sistemas Universidade Federal de Ouro Preto

(2)

Problema da Agenda

agenda em um arquivo simples de texto: primeiro nome último nome telefone

(3)
(4)

Tipos de Dados

Contato: tipo para representar um contato e suas operações; Lista de contatos: lista com todos os contatos e operações para manipulá-los.

(5)

Tipo Contato

1 # i f n d e f CONTATO_H_INCLUDED 2 #define CONTATO_H_INCLUDED 3

4 typedef s t r u c t contato∗ Contato; 5

6 Contato c_new(const char∗, const char∗, const char∗) ;

7 void c_free(Contato) ;

8 void c_print(const Contato) ;

9 i n t c_equals(const Contato, const Contato) ; 10 const char∗ c_getNome(const Contato) ; 11 const char∗ c_getSobreNome(const Contato) ; 12 const char∗ c_getFone(const Contato) ;

13 void c_setNome(Contato, const char∗) ;

14 void c_setSobreNome(Contato, const char∗) ;

15 void c_setFone(Contato, const char∗) ;

16

17 #endif / / CONTATO_H_INCLUDED

(6)

Operações Sobre Listas

adicionarum elemento na lista; removerum elemento da lista;

obterum elemento de uma dado posição da lista; obter aquantidadede elementos que a lista contém; verificar se a lista évazia;

(7)

Tipo Lista de Contatos

1 ,,,# i f n d e f LISTA_H_INCLUDED 2 ,,,#define LISTA_H_INCLUDED 3 ,,, 4 ,,,#include " Contato . h" 5 ,,,

6 ,,,typedef s t r u c t lista∗ Lista; 7 ,,,

8 Lista l_new( ) ;,,, void l_free(Lista) ;

9 ,,,i n t l_isEmpty(const Lista) ; i n t l_size(const Lista) ;

10 ,,,void l_premove(Lista, const i n t) ; void l_remove(Lista, Contato) ;

11 ,,,void l_add(Lista, const Contato) ;

12 ,,,void l_padd(Lista, const Contato, const i n t) ;

13 Contato l_get(,,, const Lista, const i n t) ;

14 ,,,i n t l_pos(const Lista, const Contato) ; 15 ,,,i n t l_has(const Lista, const Contato) ; 16 ,,,

17 ,,,#endif / / LISTA_H_INCLUDED

(8)

Tipo Lista de Contatos

1 ,,,# i f n d e f LISTA_H_INCLUDED 2 ,,,#define LISTA_H_INCLUDED 3 ,,, 4 ,,,#include " Contato . h" 5 ,,,

6 ,,,typedef s t r u c t lista∗ Lista; 7 ,,,

8 Lista l_new( ) ;,,, void l_free(Lista) ;

9 ,,,i n t l_isEmpty(const Lista) ; i n t l_size(const Lista) ;

10 ,,,void l_premove(Lista, const i n t) ; void l_remove(Lista, Contato) ;

11 ,,,void l_add(Lista, const Contato) ;

12 ,,,void l_padd(Lista, const Contato, const i n t) ;

13 Contato l_get(,,, const Lista, const i n t) ;

14 ,,,i n t l_pos(const Lista, const Contato) ; 15 ,,,i n t l_has(const Lista, const Contato) ; 16 ,,,

17 ,,,#endif / / LISTA_H_INCLUDED

(9)

Tipo Lista de Contatos

1 ,,,# i f n d e f LISTA_H_INCLUDED 2 ,,,#define LISTA_H_INCLUDED 3 ,,, 4 ,,,#include " Contato . h" 5 ,,,

6 ,,,typedef s t r u c t lista∗ Lista; 7 ,,,

8 Lista l_new( ) ;,,, void l_free(Lista) ;

9 ,,,i n t l_isEmpty(const Lista) ; i n t l_size(const Lista) ;

10 ,,,void l_premove(Lista, const i n t) ; void l_remove(Lista, Contato) ;

11 ,,,void l_add(Lista, const Contato) ;

12 ,,,void l_padd(Lista, const Contato, const i n t) ;

13 Contato l_get(,,, const Lista, const i n t) ;

14 ,,,i n t l_pos(const Lista, const Contato) ; 15 ,,,i n t l_has(const Lista, const Contato) ; 16 ,,,

17 ,,,#endif / / LISTA_H_INCLUDED

(10)

Tipo Lista de Contatos

1 ,,,# i f n d e f LISTA_H_INCLUDED 2 ,,,#define LISTA_H_INCLUDED 3 ,,, 4 ,,,#include " Contato . h" 5 ,,,

6 ,,,typedef s t r u c t lista∗ Lista; 7 ,,,

8 Lista l_new( ) ;,,, void l_free(Lista) ;

9 ,,,i n t l_isEmpty(const Lista) ; i n t l_size(const Lista) ;

10 ,,,void l_premove(Lista, const i n t) ; void l_remove(Lista, Contato) ;

11 ,,,void l_add(Lista, const Contato) ;

12 ,,,void l_padd(Lista, const Contato, const i n t) ;

13 Contato l_get(,,, const Lista, const i n t) ;

14 ,,,i n t l_pos(const Lista, const Contato) ; 15 ,,,i n t l_has(const Lista, const Contato) ; 16 ,,,

17 ,,,#endif / / LISTA_H_INCLUDED

(11)

Como Implementar as Operações?

inserir; remover; procurar; obter.

Leonardo João Abraão

(12)

Definição do Tipo, Funções de Tamanho e Vazio

1 #include " s t d l i b . h" 2 #include " s t d i o . h" 3 4 #include " L i s t a . h" 5 6 s t r u c t lista { 7 i n t size, tam; 8 Contato∗ vetor; 9 } ; 10

11 i n t l_isEmpty(const Lista l) { 12 return l−>size== 0 ? 1 : 0; 13 }

14

15 i n t l_size(const Lista l) { 16 return l−>size;

17 }

(13)

Funções de Busca na Lista

1 i n t l_pos(const Lista l, const Contato c) { 2 i n t i;

3 f o r(i = 0; i< l−>size; ++i)

4 i f(c_equals(l−>vetor[i] , c) )

5 return i; / / achou

6 return −1; / / não achou

7 } 8

9 i n t l_has(const Lista l, const Contato c) { 10 return l_pos(l, c) >= 0 ? 1 : 0;

11 } 12

13 Contato l_get(const Lista l, const i n t p) {

14 i f(p>= 0 &&p< l−>size) 15 return l−>vetor[p] ; 16 else return NULL; 17 }

(14)

Funções de Inserção na Lista

1 void l_add(Lista l, const Contato c) {

2 l_padd(l, c, l−>size) ;

3 } 4

5 void l_padd(Lista l, const Contato c, const i n t p) {

6 i f(l−>size== l−>tam) 7 resize(l, 2∗l−>tam) ; 8 i f(p> l−>size) / / e r r o r 9 return; 10 i n t i; 11 f o r(i = l−>size; i >p;−−i) 12 l−>vetor[i] = l−>vetor[i−1]; 13 l−>vetor[p] =c; 14 l−>size++; 15 } Lista.c

(15)

Funções de Remoção na Lista

1 void l_premove(Lista l, const i n t p) {

2 i f(p< 0 | | p >=l−>size) / / Error 3 return; 4 c_free(l−>vetor[p] ) ; i n t i; 5 f o r(i =p; i< l−>size−1;i++) 6 l−>vetor[i] = l−>vetor[i+ 1 ]; 7 l−>size−−; 8 i f(l−>size== l−>tam/ 4 ) 9 resize(l, l−>tam/ 2 ) ; 10 } 11

12 void l_remove(Lista l, Contato c) {

13 i n t p =l_pos(l, c) ;

14 i f(p< 0)

15 return; / / o elemento não está na l i s t a

16 l_premove(l, p) ;

17 }

(16)

Função de Redimensionamento

1 void resize(Lista l, i n t tam) {

2 Contato∗ vetor = (Contato∗) calloc(tam, sizeof(Contato) ) ;

3 i n t i; 4 f o r(i = 0; i< l−>size; i++) 5 vetor[i] = l−>vetor[i] ; 6 free(l−>vetor) ; 7 l−>vetor=vetor; 8 l−>tam=tam; 9 } Lista.c

(17)

Funções Para Criar e Remover Listas

1 Lista l_new( ) {

2 Lista l = (Lista) malloc(sizeof(s t r u c t lista) ) ;

3 l−>size= 0;

4 l−>tam= 16;

5 l−>vetor= (Contato∗) calloc(l−>tam, sizeof(Contato) ) ;

6 return l; 7 } 8 9 void l_free(Lista l) { 10 i f(l==NULL) 11 return; 12 i n t i; 13 f o r(i = 0; i< l−>size; ++i) 14 c_free(l−>vetor[i] ) ; 15 free(l−>vetor) ; 16 free(l) ; 17 } Lista.c

(18)
(19)

1 Lista por Vetor

2 Lista Encadeada

(20)

Ideia de Listas Encadeadas

Enxergar a lista contendo duas partes: cabeça e calda

(21)

Como Implementar as Operações?

inserir; remover; procurar; obter.

Leonardo João Abraão Helen

(22)

Definição do Tipo, Funções de Tamanho e Vazio

1 #include " s t d l i b . h" 2 #include " s t d i o . h" 3 #include " L i s t a . h" 4 5 s t r u c t lista { 6 i n t size; 7 s t r u c t node∗ cabeca; 8 } ; 9 10 s t r u c t node { 11 Contato item; 12 s t r u c t node∗ calda; 13 } ; 14

15 i n t l_isEmpty(const Lista l) { return l−>size== 0 ? 1 : 0; } 16

17 i n t l_size(const Lista l) { return l−>size; } ListaEncadeada.c

(23)

Funções de Busca na Lista

1 i n t l_pos(const Lista l, const Contato c) { 2 i n t i = 0; s t r u c t node∗ cabeca= l−>cabeca; 3 while(cabeca != NULL) {

4 i f(c_equals(cabeca−>item, c) ) return i; / / achou

5 cabeca=cabeca−>calda; i++; / / vai para o próximo

6 }

7 return −1; / / não achou

8 }

9 i n t l_has(const Lista l, const Contato c) { 10 return l_pos(l, c) >= 0 ? 1 : 0;

11 } 12

13 Contato l_get(const Lista l, const i n t p) {

14 i n t i = 0; s t r u c t node∗ c= l−>cabeca;

15 while(c != NULL&&i <p) { c =c−>calda; i++; } 16 return i==p&&c != NULL? c−>item : NULL; 17 }

(24)

Funções de Inserção na Lista

1 void l_padd(Lista l, const Contato c, const i n t p) {

2 i f(p> l−>size) / / e r r o r

3 return;

4 s t r u c t node∗ novo= (s t r u c t node∗) malloc(sizeof(s t r u c t node) ) ; 5 novo−>item= c; l−>size++;

6 i f(p== 0 | | l_isEmpty(l) ) {

7 novo−>calda= l−>cabeca;

8 l−>cabeca=novo;

9 return;

10 }

11 i n t i = 1; s t r u c t node∗ aux= l−>cabeca; 12 while(i <p) { aux=aux−>calda; i++; } 13 novo−>calda=aux−>calda;

14 aux−>calda=novo;

15 } 16

17 void l_add(Lista l, const Contato c) { l_padd(l, c, 0) ; }

(25)

Funções de Remoção na Lista

1 void l_premove(Lista l, const i n t p) {

2 i f(p< 0 | | p >=l−>size) return; / / Error

3 s t r u c t node∗ aux =l−>cabeca, ∗r; l−>size−−;

4 i f(p== 0) { / / remover o primeiro

5 aux= l−>cabeca−>calda; c_free(l−>cabeca−>item) ;

6 free(l−>cabeca) ; l−>cabeca=aux; return;

7 }

8 i n t i = 1; while(i <p) { aux=aux−>calda; i++; }

9 r =aux−>calda;

10 i f(r != NULL) { / / Não é o último

11 aux−>calda= r−>calda; c_free(r−>item) ; free(r) ;

12 } else aux−>calda=NULL;

13 }

14 void l_remove(Lista l, Contato c) {

15 i n t p =l_pos(l, c) ;

16 i f(p< 0) return; / / o elemento não está na l i s t a

17 l_premove(l, p) ; }

(26)

Funções Para Criar e Remover Listas

1 Lista l_new( ) {

2 Lista l = (Lista) malloc(sizeof(s t r u c t lista) ) ;

3 l−>size= 0; l−>cabeca=NULL; 4 return l;

5 } 6

7 void n_free(s t r u c t node∗ n) {

8 i f(n==NULL) return; 9 c_free(n−>item) ; 10 n_free(n−>calda) ; 11 free(n) ; 12 } 13 14 void l_free(Lista l) { 15 i f(l==NULL) return;

16 n_free(l−>cabeca) ; free(l) ;

17 }

(27)

1 Lista por Vetor

2 Lista Encadeada

(28)

Ideia de Listas Duplamente Encadeadas

generalização de listas encadeadas

(29)

Implementação das Operações

inserir remover buscar obter

Referências

Documentos relacionados

9.5 Dentro do prazo estabelecido para início da sua execução ou da exploração comercial do serviço, a autorizada ou permissionária, com a finalidade de realizar

Frente a um resultado laboratorial negativo para Ebola (PCR negativo), e caso o paciente não apresente condições para transferência para o hospital de

A renovação do prazo da permissão para exploração de Serviços Especiais poderá implicar pagamento pela permissionária pelo direito de exploração do Serviço e uso de

trabalhadores, e no fortalecimento da sua resiliência, especialmente com relevância à valorização dos colaboradores por meio do estímulo dos seus talentos, virtudes e forças

Cada financiamento não tem de incluir todas as fontes de financiamento público (FEEI - Fundos Europeus Estruturais e de Investimento, CPN - Contrapartida Pública Nacional, BEI -

Matemática Sólidos geométricos; Classificação dos sólidos geométricos; Faces, arestas e vértices de um poliedro; Situações- problema; Tabela da multiplicação;

Resumo da ANOVA para as variáveis de fotossíntese líquida (A), condutância estomática (gs), concentração interna de CO2 (Ci), transpiração (E), temperatura foliar (TF),

Na opção “Outros”, cite e explique aquelas ferramentas não citadas nas opções anteriores, mas que são importantes para o planejamento urbano em sua cidade/consórcio... O Pacto