Baixe o app para aproveitar ainda mais
Prévia do material em texto
Implementacao da Lista Simplesmente Encadeada Denis R. S. Pinheiro September 29, 2013 #inc lude <s t d i o . h> #inc lude <s t d l i b . h> #de f i n e TRUE 1 #de f i n e FALSE 0 s t r u c t no { f l o a t i n f o ; s t r u c t no∗ prox ; } ; 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 ; } ; 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 ) ; void i n s e r e ( L i s t a ∗ , f l o a t ) ; i n t r e t i r a ( L i s t a ∗ , f l o a t ) ; i n t vaz ia ( L i s t a ∗ ) ; void l i b e r a ( L i s t a ∗ ) ; No∗ busca ( L i s t a ∗ , f l o a t ) ; void imprime ( L i s t a ∗ ) ; 1 i n t main ( ){ L i s ta ∗ l = c r i a ( ) ; No∗ resSeach ; i n t r e s ; i n s e r e ( l , 2 ) ; i n s e r e ( l , 1 ) ; i n s e r e ( l , 3 ) ; i n s e r e ( l , 7 ) ; i n s e r e ( l , 5 ) ; i n s e r e ( l , 6 ) ; i n s e r e ( l , 4 ) ; i n s e r e ( l , 8 ) ; imprime ( l ) ; i n t i ; f o r ( i = 1 ; i < 9 ; ++i ) { resSeach = busca ( l , i ) ; i f ( resSeach !=NULL) { p r i n t f (”\ nValor retornado da busca : %f \n” , resSeach−>i n f o ) ; } } imprime ( l ) ; // r e s=r e t i r a ( f , 4 ) ; r e s = r e t i r a ( l , 5 ) ; imprime ( l ) ; l i b e r a ( l ) ; r e turn 0 ; 2 } L i s ta ∗ 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; re turn f ; } No∗ i n s i n i ( L i s t a ∗ f , f l o a t v ){ No∗ p = (No∗) mal loc ( s i z e o f (No ) ) ; p−>i n f o = v ; p−>prox = f−>i n i ; i f ( f−>i n i == NULL) p−>prox = NULL; re turn p ; } void i n s e r e ( L i s t a ∗ f , f l o a t v ){ f−>i n i= i n s i n i ( f , v ) ; } 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∗ q ; 3 f o r ( q = f−>i n i ; q != NULL; q = q−>prox ){ i f (q−>i n f o == v) { r e turn q ; } } r e turn NULL; } i n t r e t i r a ( L i s t a ∗ f , f l o a t v ){ No∗ q ; i f ( vaz ia ( f ) ){ p r i n t f (” F i l a vaz ia .\n ” ) ; e x i t ( 1 ) ; } //Recebendo um ponte i r o para o n q = busca ( f , v ) ; i f ( q==NULL) { p r i n t f (”\ nValor nao r e t i r a d o .\n ” ) ; r e turn FALSE; } e l s e { No ∗aux ; aux = f−>i n i ; // se e s t i v e r na pr ime i ra p o s i o i f ( aux == q) f−>i n i = q−>prox ; e l s e { // pe r co r r e a t chegar no n an t e r i o r ao dese jado whi le ( aux−>prox != q) aux = aux−>prox ; aux−>prox = q−>prox ; } 4 f r e e ( q ) ; p r i n t f (”\ nValor r e t i r a d o .\n ” ) ; r e turn TRUE; } } 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\n ” ) ; } /∗ ∗ 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
Compartilhar