Exemplo de Lista Encadeada em C






#include<stdio.h>
#include<stdlib.h>
int menu();
void InserirInicio(int num);
void InserirFim(int num);
void InserirMeio(int num, int posicao);
void Remover(int num);
void Listar();
int *Head;
typedef struct ElementoDaLista{
        int dado;
        struct ElementoDaLista *prox;
};
int main()
{
        int op, num, pos,c,dado;
        while (1){
                  op = menu();
                  switch (op){
        case 1: // inserir no inicio da lista
                  printf("Digite o numero desejado:\n ");
                  scanf("%d", &num);
                  InserirInicio(num);
                  break;
        case 2: // inserir no fim da lista
                  printf("Digite o numero desejado: ");
                  scanf("%d", &num);
                  InserirFim(num);
                  break;
        case 3: // inserir no meio da lista
                  printf("Digite o numero desejado: ");
                  scanf("%d", &num);
                  while ((c = getchar()) != '\n' && c != EOF){} // sempre limpe o buffer do teclado.
                  printf("Digite a posicao desejada: ");                  scanf("%d", &pos);
                  InserirMeio(num, pos);
                  break;
        case 4: // remover da lista
                  printf("Digite o numero desejado: ");
                  scanf("%d", &num);
                  while ((c = getchar()) !='\n'&& c != EOF){} // sempre limpe o buffer do teclado.
                  scanf("%d", &pos);
                  Remover(num);
                  break;
        case 5: // mostrar toda a lista
                  Listar();
                  break;
        case 6:
                 return 0;
                 default:
                  printf("Invalido\n");
}
}
}
int menu(){
         int op, c;
         system("Cls");
         printf("1.Inserir no inicio da lista encadeada simples\n" );
         printf("2.Inserir no fim da lista encadeada simples\n" );
         printf("3.Inserir no meio da lista encadeada simples\n");
         printf("4.Remover no inicio da lista encadeada simples\n");
         printf("5.Listar a lista encadeada simples\n");
         printf("6.Sair\n");
         printf("Digite sua escolha\n");
         scanf("%d",&op );
         while ((c = getchar()) !='\n'&& c != EOF){}
         system("Cls");
         return op;
}
void InserirInicio(int num)
{
        struct ElementoDaLista *NovoElemento;
        NovoElemento = (struct ElementoDaLista *)malloc(sizeof(struct ElementoDaLista));
        NovoElemento->dado = num;

              if (Head == NULL)
{
                  Head = NovoElemento;
                  NovoElemento->prox = NULL;
             }else{
                  NovoElemento->prox = Head;
                  Head = NovoElemento;
}
       printf("\n");
       system("pause");
       return;
}
void Listar()
{
       struct ElementoDaLista * ElementoVarredura;
       ElementoVarredura = (struct ElementoDaLista*)malloc(sizeof(struct ElementoDaLista));
       ElementoVarredura = Head;
       if(ElementoVarredura == NULL){
       return;
}
       while (ElementoVarredura != NULL){
       printf("%d", ElementoVarredura->dado);
       ElementoVarredura = ElementoVarredura->prox;
}
       printf("\n");
       system("pause");
       return;
}
void InserirFim(int num)
{
       struct ElementoDaLista * NovoElemento;
       NovoElemento = (struct ElementoDaLista *)malloc(sizeof(struct ElementoDaLista));
       struct ElementoDaLista * ElementoVarredura;
       ElementoVarredura = (struct ElementoDaLista*)malloc(sizeof(struct ElementoDaLista));
       NovoElemento->dado = num;
       if(Head == NULL)
{
       Head = NovoElemento;
       NovoElemento->prox = NULL;
}else{
       ElementoVarredura = Head;
       while (ElementoVarredura->prox != NULL)
       ElementoVarredura = ElementoVarredura->prox;
       ElementoVarredura->prox = NovoElemento;
       NovoElemento->prox = NULL;
}
}
void InserirMeio(int num, int posicao)
{
        struct ElementoDaLista *NovoElemento;
        NovoElemento = (struct ElementoDaLista *)malloc(sizeof(struct ElementoDaLista));
        struct ElementoDaLista * ElementoVarredura;
        ElementoVarredura = (struct ElementoDaLista*)malloc(sizeof(struct ElementoDaLista));
        struct ElementoDaLista * ElementoAuxiliar;
        ElementoAuxiliar = (struct ElementoDaLista_*)malloc(sizeof(struct ElementoDaLista));
        NovoElemento->dado = num;
        if (posicao == 0)
{
             Head = NovoElemento;
             NovoElemento->prox = NULL;
       }else{
             ElementoVarredura = Head;
                     for (int i = 0; i < posicao -1; i++)
                     ElementoVarredura = ElementoVarredura->prox;
                     ElementoAuxiliar = ElementoAuxiliar->prox;
                     ElementoVarredura->prox = NovoElemento;
                     NovoElemento->prox = ElementoAuxiliar;
}
}
void Remover(int num)
{
          struct ElementoDaLista *ElementoVarredura;
         ElementoVarredura = (struct ElementoDaLista *)malloc(sizeof(struct ElementoDaLista));
         struct ElementoDaLista *Anterior;
         Anterior = (struct ElementoDaLista*)malloc(sizeof(struct ElementoDaLista));
         ElementoVarredura = Head;
         while(ElementoVarredura != NULL){
         if(ElementoVarredura->dado == num){
         if(ElementoVarredura == Head){
              Head = ElementoVarredura->prox;
              free(ElementoVarredura);
              return 1;
         }else{
               Anterior-> prox = ElementoVarredura->prox;
               free(ElementoVarredura);
                return 1;
}
}
}
}

Postar um comentário

0 Comentários