sexta-feira, 30 de novembro de 2012

Lista Encadeada em Linguagem C

Quando estava me sentindo que sabia programar em Linguagem C,
já relachando, e perdendo aos poucos o entusiasmo pela linguagem,
resolvi criar uma Lista Encadeada simples, e pra minha surprêsa
tive muitas dificuldades, e a bola muchou.
Passei um fim de semana lindo e ensolarado preso neste código, e pra ser
bem sincero, tive pena de postar na época que criei, há poucos mêses atrás,
más resolvi postar, pois o pouco que sei em programação aprendi mais na marra, estudando outros códigos do que por teorias intermináveis sobre o assunto.
Na verdade, Lista Encadeada em Linguagem C, não é difícil de criar, e nem de usar,
porém não é fácil de entender e mais ainda de explicar.
Ainda bem que existem milhares de informações sobre o assunto na internet,
incluindo até mesmo bons vídeos, que podem ser assistidos pelos interessados,
comecei a criar esta lista encadeada e tive que mesclar partes de alguns códigos para criar a moldura e chegar neste ótimo trabalho.
Confira a eficiência desta lista em todas as funções, aliás ela é toda baseada em funções,
que facilitam os interessados no assunto.
Como sempre criado no eclipse, onde rodou sem nenhum warnings.



#include <stdio.h>
#include <conio.h>
#include <string.h>
#define TRUE 1
int resp, opc;
int i = 0;
char lista [ 100 ];
struct ListaEncadeada {
     char nome [ 100 ];
     struct ListaEncadeada *liga;
};
typedef ListaEncadeada ListEnc;
int insere ( char ins_nome [ 100 ] );
void imprime ( );
void inserindo ( );
void Mostra ( );
void Excluindo ( );
void Elementos ( );
int exclui ( char ins_nome [ 100 ] );
ListEnc *Lista;
ListEnc *Primeiro;
void Janela ( ) {
     int c, l;
     for ( c = 3; c <= 78 ; c++ ) {
         gotoxy ( c , 1 );
         printf ( "%c" , 205 );
         gotoxy ( c , 4 );
         printf ( "%c" , 205 );
     }
     gotoxy ( 2 , 1 );
     printf ( "%c" , 201 );
     gotoxy ( 79 , 1 );
     printf ( "%c" , 187 );
     gotoxy ( 79 , 4 );
     printf ( "%c" , 188 );
     gotoxy ( 2 , 4 );
     printf ( "%c" , 200 );
     for ( l = 2; l <= 3 ; l++ ) {
         gotoxy ( 2 , l );
         printf ( "%c" , 186 );
         gotoxy ( 79 , l );
         printf ( "%c" , 186 );
     }
     for ( c = 3; c <= 78 ; c++ ) {
         for ( l = 2; l <= 3 ; l++ ) {
              gotoxy ( c , l );
              printf ( "%c" , 177 );
         }
     }
     for ( c = 3; c <= 78 ; c++ ) {
         gotoxy ( c , 5 );
         printf ( "%c" , 205 );
         gotoxy ( c , 25 );
         printf ( "%c" , 205 );
     }
     gotoxy ( 2 , 5 );
     printf ( "%c" , 201 );
     gotoxy ( 79 , 5 );
     printf ( "%c" , 187 );
     gotoxy ( 79 , 25 );
     printf ( "%c" , 188 );
     gotoxy ( 2 , 25 );
     printf ( "%c" , 200 );
     gotoxy ( 2 , 8 );
     printf ( "%c" , 204 );
     gotoxy ( 79 , 8 );
     printf ( "%c" , 185 );
     for ( l = 9; l <= 24 ; l++ ) {
         gotoxy ( 2 , l );
         printf ( "%c" , 186 );
         gotoxy ( 79 , l );
         printf ( "%c" , 186 );
     }
     for ( l = 6; l <= 7 ; l++ ) {
         gotoxy ( 2 , l );
         printf ( "%c" , 186 );
         gotoxy ( 79 , l );
         printf ( "%c" , 186 );
     }
     for ( c = 3; c <= 78 ; c++ ) {
         gotoxy ( c , 8 );
         printf ( "%c" , 205 );
     }
     for ( c = 5; c <= 25 ; c++ ) {
         gotoxy ( c + 25 , 9 );
         printf ( "%c" , 205 );
         gotoxy ( c + 25 , 24 );
         printf ( "%c" , 205 );
     }
     gotoxy ( 29 , 9 );
     printf ( "%c" , 201 );
     gotoxy ( 51 , 9 );
     printf ( "%c" , 187 );
     gotoxy ( 51 , 24 );
     printf ( "%c" , 188 );
     gotoxy ( 29 , 24 );
     printf ( "%c" , 200 );
     for ( l = 10; l <= 23 ; l++ ) {
         gotoxy ( 29 , l );
         printf ( "%c" , 186 );
         gotoxy ( 51 , l );
         printf ( "%c" , 186 );
     }
}
int main ( ) {
     system ( "title PROGRAMA LISTA ENCADEADA" );
     do {
         system ( "cls" );
         Janela ( );
         textcolor ( LIGHTCYAN );
         gotoxy ( 30 , 3 );
         printf ( "PROGRAMA LISTA ENCADEADA" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 30 , 7 );
         printf ( "ESCOLHA UMA OPCAO ABAIXO" );
         textcolor ( YELLOW );
         gotoxy ( 35 , 11 );
         printf ( "1 - INSERIR" );
         gotoxy ( 35 , 13 );
         printf ( "2 - LISTAR" );
         gotoxy ( 35 , 15 );
         printf ( "3 - EXCLUIR" );
         gotoxy ( 35 , 17 );
         printf ( "4 - ELEMENTOS" );
         gotoxy ( 35 , 19 );
         printf ( "5 - SAIR" );
         gotoxy ( 35 , 21 );
         textcolor ( LIGHTRED );
         scanf ( "%d" , &opc );
         fflush ( stdin );
         if ( opc == 1 ) {
              system ( "cls" );
              inserindo ( );
         }
         if ( opc == 2 ) {
              Mostra ( );
         }
         if ( opc == 3 ) {
              Excluindo ( );
         }
         if ( opc == 4 ) {
              Elementos ( );
         }
         if ( opc == 5 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 34 , 21 );
              printf ( "O ROCCO AGRADECE" );
              Sleep ( 1800 );
              exit ( 0 );
         } else {
              textcolor ( LIGHTRED );
              gotoxy ( 34 , 21 );
              printf ( "\a OPCAO ERRADA" );
              Sleep ( 800 );
         }
     } while ( 1 );
}
void inserindo ( ) {
     textcolor ( LIGHTRED );
     gotoxy ( 30 , 3 );
     printf ( "PROGRAMA LISTA ENCADEADA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30 , 5 );
     printf ( "INSERINDO ITENS" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 30 , 7 );
     printf ( "NOME DO ITEM A SER INSERIDO: " );
     textcolor ( LIGHTRED );
     gets ( lista );
     fflush ( stdin );
     resp = insere ( lista );
     if ( resp == TRUE ) {
         textcolor ( LIGHTCYAN );
         gotoxy ( 30 , 9 );
         printf ( "%s ITEM INSERIDO COM SUCESSO" , lista );
         i++;
     }
     getche ( );
     main ( );
}
void Mostra ( ) {
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 30 , 3 );
     printf ( "PROGRAMA LISTA ENCADEADA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30 , 5 );
     printf ( "ELEMENTOS DA LISTA" );
     printf ( "\n\n\t\t" );
     imprime ( );
     getche ( );
     main ( );
}
void Excluindo ( ) {
     system ( "cls" );
     do {
         textcolor ( LIGHTRED );
         gotoxy ( 30 , 3 );
         printf ( "PROGRAMA LISTA ENCADEADA" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 30 , 5 );
         printf ( "ELEMENTOS DA LISTA" );
         printf ( "\n\n\t\t" );
         imprime ( );
         textcolor ( LIGHTCYAN );
         gotoxy ( 30 , 17 );
         printf ( "ESCOLHA UM PARA SER EXCLUIDO" );
         gotoxy ( 30 , 19 );
         textcolor ( LIGHTRED );
         gets ( lista );
         fflush ( stdin );
         resp = exclui ( lista );
         if ( resp == TRUE ) {
              textcolor ( YELLOW );
              gotoxy ( 30 , 21 );
              printf ( "%s EXCLUIDO COM SUCESSO" , lista );
              i--;
              getche ( );
              main ( );
         } else {
              textcolor ( YELLOW );
              gotoxy ( 30 , 21 );
              printf ( "\aNAO EXISTE ESTE ITEM NA LISTA" );
         }
         getche ( );
         system ( "cls" );
     } while ( 1 );
}
void Elementos ( ) {
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 30 , 3 );
     printf ( "PROGRAMA LISTA ENCADEADA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30 , 5 );
     printf ( "ELEMENTOS DA LISTA" );
     printf ( "\n\n\t\t" );
     imprime ( );
     textcolor ( LIGHTCYAN );
     gotoxy ( 30 , 17 );
     printf ( "QUANTIDADE DE ELEMENTOS" );
     textcolor ( YELLOW );
     gotoxy ( 30 , 19 );
     printf ( "%d ELEMENTOS" , i );
     getche ( );
     main ( );
}
int insere ( char Nome_Palavra [ 100 ] ) {
     ListEnc *Aux;
     ListEnc *Analisa_lista;
     Aux = ( ListEnc * ) malloc ( sizeof ( ListEnc ) );
     if ( Aux == NULL ) {
         return ( 0 );
     } else {
         strcpy ( Aux->nome , Nome_Palavra );
         Aux->liga = NULL;
         if ( Lista != NULL ) {
              if ( Primeiro != NULL ) {
                   Aux->liga = Primeiro;
                   Lista = Aux;
                   Analisa_lista = Primeiro;
                   if ( stricmp ( Lista->nome , Primeiro->nome ) > 0 ) {
                        Aux = Primeiro;
                        Analisa_lista = Primeiro->liga;
                        while ( ( Analisa_lista != NULL )
                                 && ( ( ( stricmp ( Lista->nome ,
                                          Analisa_lista->nome ) ) > 0 ) ) ) {
                            Aux = Analisa_lista;
                            Analisa_lista = Analisa_lista->liga;
                        }
                        Lista->liga = Analisa_lista;
                        Aux->liga = Lista;
                   } else {
                        Primeiro = Lista;
                   }
              } else {
                   if ( stricmp ( Aux->nome , Lista->nome ) > 0 ) {
                        Lista->liga = Aux;
                        Primeiro = Lista;
                        Lista = Aux;
                   }
                   if ( stricmp ( Aux->nome , Lista->nome ) < 0 ) {
                        Aux->liga = Lista;
                        Lista = Aux;
                        Primeiro = Lista;
                   }
              }
         } else {
              Lista = Aux;
         }
     }
     return ( 1 );
}
void imprime ( ) {
     ListEnc *Aux;
     if ( Lista == NULL ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 30 , 7 );
         printf ( "\aA LISTA ESTA VAZIA" );
         Sleep ( 1800 );
         main ( );
     } else {
         if ( Primeiro == NULL ) {
              Aux = Lista;
              textcolor ( LIGHTGREEN );
              printf ( "%s" , Aux->nome );
         } else {
              Lista = Primeiro;
              Aux = Lista;
              while ( Aux != NULL ) {
                   printf ( "%s\n\t\t" , Aux->nome );
                   Aux = Aux->liga;
              }
         }
     }
}
int exclui ( char stringnome [ 100 ] ) {
     ListEnc *Aux;
     ListEnc *Repl = 0;
     int Testa_Compara;
     if ( Lista == NULL ) {
         return ( 0 );
     } else {
         if ( Primeiro == NULL ) {
              Aux = Lista;
              Testa_Compara = stricmp ( Aux->nome , stringnome );
              if ( Testa_Compara == 0 ) {
                   free ( Aux );
                   Lista = NULL;
                   return ( 1 );
              } else {
                   return ( 0 );
              }
         } else {
              Lista = Primeiro;
              Aux = Lista;
              while ( Aux != NULL ) {
                   Testa_Compara = stricmp ( Aux->nome , stringnome );
                   if ( Testa_Compara == 0 ) {
                        if ( Aux == Primeiro ) {
                            if ( Aux->liga == NULL ) {
                                 free ( Aux );
                                 Lista = NULL;
                                 return ( 1 );
                            } else {
                                 Primeiro = Aux->liga;
                                 free ( Aux );
                                 return ( 1 );
                            }
                        } else {
                            Repl->liga = Aux->liga;
                            free ( Aux );
                            return ( 1 );
                        }
                   } else {
                        Repl = Aux;
                        Aux = Aux->liga;
                   }
              }
              return ( 0 );
         }
         return ( 1 );
     }
}



Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.