Buscar

listaDupla

Prévia do material em texto

Implementacao da Lista Duplamente
Encadeada
Denis R. S. Pinheiro
September 28, 2013
#inc lude <s t d i o . h>
#inc lude <s t d l i b . h>
s t r u c t no {
f l o a t i n f o ;
s t r u c t no∗ prox ;
s t r u c t no∗ ant ;
} ;
typede f s t r u c t no No ;
s t r u c t l i s t a {
No∗ i n i ;
No∗ f im ;
} ;
typede f s t r u c t l i s t a L i s t a ;
L i s t a ∗ c r i a ( void ){
L i s ta ∗ f = ( L i s t a ∗) mal loc ( s i z e o f ( L i s t a ) ) ;
f−>i n i = NULL;
f−>f im = NULL;
re turn f ;
}
void i n s e r e ( L i s t a ∗ f , f l o a t v ){
1
No∗ p = (No∗) mal loc ( s i z e o f (No ) ) ;
p−>i n f o = v ;
p−>prox = f−>i n i ;
p−>ant = NULL;
i f ( f−>i n i != NULL){
f−>i n i−>ant = p ;
} e l s e
f−>f im=p ;
f−>i n i = p ;
}
i n t vaz ia ( L i s t a ∗ f ){
r e turn ( f−>i n i == NULL) ;
}
void l i b e r a ( L i s t a ∗ f ){
No∗ q = f−>i n i ;
whi l e ( q != NULL){
No∗ t = q−>prox ;
f r e e ( q ) ;
q = t ;
}
f r e e ( f ) ;
}
No∗ busca ( L i s t a ∗ f , f l o a t v ){
No∗ i n i c i o = f−>i n i ;
No∗ f i n a l = f−>f im ;
// p r i n t f (”(%x != %x) && (%x != %x)\n” , i n i c i o−>ant e r i o r , fim , i n i c i o−>ant e r i o r , fim−>proximo ) ;
// whi l e ( i n i c i o != f i n a l−>prox && i n i c i o−>ant != f i n a l−>prox ){
whi le ( i n i c i o−>ant != f i n a l && ( i n i c i o−>ant != f i n a l−>prox | | i n i c i o−>ant == NULL)){
i f ( i n i c i o−>i n f o == v)
{
r e turn i n i c i o ;
}
2
i f ( f i n a l−>i n f o == v){
r e turn f i n a l ;
}
i n i c i o = i n i c i o−>prox ;
f i n a l = f i n a l−>ant ;
}
// r eve r c o n d i o de re to rno
p r i n t f (”\nNao encontrou .\n ” ) ;
r e turn NULL;
}
void r e t i r a (No ∗∗ l , f l o a t x ){ //Porque n o posso passar a e s t ru tu ra l i s t a ? u t i l i z a r assim : ∗ f−>i n i
No ∗ r e s ;
i n t cont = 0 ;
r e s = busca ( l , x ) ;
i f ( r e s != NULL){
i f ( r e s −> prox != NULL)
r e s −> prox −> ant = r e s −> ant ;
i f ( r e s −> ant != NULL)
r e s −> ant −> prox = re s −> prox ;
e l s e
(∗ l ) = r e s −> prox ;
f r e e ( r e s ) ;
cont = 1 ;
p r i n t f (”\ nElemento removido com suce s so \n ” ) ;
} e l s e p r i n t f (”\ nElemento nao encontrado\n ” ) ;
}
void imprime ( L i s t a ∗ f ){
No∗ q ;
p r i n t f (”\ nLis ta : ” ) ;
f o r ( q = f−>i n i ; q != NULL; q = q−>prox )
p r i n t f (”%.2 f −> ” ,q−>i n f o ) ;
p r i n t f (”NULL\n ” ) ;
}
i n t main ( ){
3
L i s ta ∗ f = c r i a ( ) ;
No∗ resSeach ;
i n s e r e ( f , 1 ) ;
i n s e r e ( f , 2 ) ;
i n s e r e ( f , 7 ) ;
i n s e r e ( f , 3 ) ;
i n s e r e ( f , 6 ) ;
i n s e r e ( f , 4 ) ;
i n s e r e ( f , 5 ) ;
i n s e r e ( f , 8 ) ;
imprime ( f ) ;
i n t i ;
f o r ( i = 1 ; i < 9 ; ++i )
{
resSeach = busca ( f , i ) ;
i f ( resSeach !=NULL)
{
p r i n t f (”\ nValor retornado da busca : %f \n” , resSeach−>i n f o ) ;
}
}
imprime ( f ) ;
r e t i r a (&f−>i n i , 5 ) ;
imprime ( f ) ;
p r i n t f (”\n ” ) ;
l i b e r a ( f ) ;
r e turn 0 ;
}
/∗
4
∗ By : Denis P inhe i ro . Todos os d i r e i t o s r e s e rvados .
∗ 17 :05 :2013
∗/
5

Outros materiais

Materiais relacionados

Perguntas Recentes