domingo, 5 de janeiro de 2014

Multilistas em C

All complete code was stored in:

https://github.com/lapuinka/basicCsamples



struct No{
        struct No *back;
        char   nome[MAX_NOMEALUNO];
        struct No *pilhacursos;

};
.
.
.
.
.

char menu(int total)
{

       char op;
       printf("\n[I] - inserir ");
       if (total>0)
       {
             printf("\n[E] - excluir ");
             printf("\n[P] - pesquisar ");
             printf("\n[L] - listar  ");
       }
       printf("\n[S] - sair");


       op=toupper(getch());
       if((op=='E' || op=='P' || op=='L') && total==0) //filtra as teclas
             op=NULL;

       return op;



}

//funcoes para o aluno

No   *insere  (No *topo,char *nome);
No   *edita   (No *idx);
No   *exclui (No *topo,No *idx,char *msg);
No   *pesquisa(No *topo,char *nome);
int  lista    (No *aluno);
int  Registros    (No *topo);


void ToUpper(char *nome);

void main(void)

{
        No *topo;
        char nome[MAX_NOMEALUNO];
        No *current;
        topo        =NULL;
        current=NULL;


        int continua=1;

        while(continua)
        {
                    clrscr();
                    printf("[ status] - inserindo alunos");
                    switch(menu(Registros(topo)))
                    {

                           case 'I':
                           {
                                  printf("\n[ wait ] - digite o nome do %s:",SZALUNO);
                                  ToUpper(gets(nome));
                                  current=pesquisa(topo,nome);
                                  if(current==NULL)
                                               topo=insere(topo,nome);
                                  else
                                  {
                                        printf("\n[error] - nome duplicado",SZALUNO);
                                        getch();

                                  }


                           }
                                  break;

                           case 'L':
                           {
                                  lista(topo);
                                  getche();
                           }
                                  break;
                           case 'E':
                           {
                                  if(lista(topo)>0)
                                  {
                                        printf("\n[wait] - digite o nome do %s: ",SZALUNO);
                                        ToUpper(gets(nome));
                                        current=pesquisa(topo,nome);
                                        if(current==NULL)
                                        {
                                               printf("\n[ error ] - %s nao encontrado",SZALUNO);
                                               getch();

                                        }
                                        else
                                        {
                                               printf("\n[ ok    ] - %s excluido!",SZALUNO);
                                               topo=exclui(topo,current,SZALUNO);
                                               getche();
                                        }
                                  }
                                  else
                                  {
                                               printf("\n[ error ] - %s nao encontrado",SZALUNO);
                                               getch();

                                  }

                           }
                                  break;
                           case 'P':
                           {
                                  lista(topo);
                                  printf("\n[ wait] - digite o nome do %s: ",SZALUNO);
                                  ToUpper(gets(nome));
                                  current=pesquisa(topo,nome);
                                  if(current==NULL)
                                        printf("\n[ error ] - %s nao encontrado",SZALUNO);
                                  else
                                  {
                                        printf("\n[ ok    ] - %s encontrado",SZALUNO);
                                        edita(current);
                                  }
                    }
                                  break;
                           case 'S':
                                  continua=0;
                                  break;
                    }



        }

        if(Registros(topo)>0)
        {
              while(topo!=NULL)
              {

                    if(Registros(topo->pilhacursos)>0)
                     {
                            while(topo->pilhacursos!=NULL)
                            {
                                  topo->pilhacursos=exclui(topo->pilhacursos,topo->pilhacursos,SZCURSO);
                            }
                                  printf("\n[ status ] - memoria de %s liberada para o S.O",SZCURSO);
                     }


                    topo=exclui(topo,topo,SZALUNO);
              }
              printf("\n[ status ] - memoria de %s liberada para o S.O",SZALUNO);
              getch();
        }
        else
        {
              printf("\n[ status ] - Have a nice day... :-)");
              getch();

        }

}





No *PegaNo()
{
             return((No*)malloc(sizeof(No)*1));
}

void liberaNo(No *pointer)
{

       free(pointer);
}


No *insere(No *topo,char *nome)
{
       No *novoNo;
       novoNo = PegaNo();

       *novoNo->nome='\0';
       novoNo->pilhacursos=NULL;




       strcat(novoNo->nome,nome);
       if(topo==NULL)
       {
             topo=novoNo;
             topo->back=NULL;

       }
       else
       {
             novoNo->back=topo;
             topo=novoNo;
       }

       return topo;

}


No *exclui(No *topo,No *idx,char *msg)
{
        No *last;
        No *current;


       if(idx==NULL)
              return NULL;


       //removo o indice, so que tenho que reposicinar a lista...


       if(idx!=topo)
       {
             last=topo;
             current=topo->back;
             while(current!=NULL)
             {
                           if(current==idx)
                           {
                                   last->back=current->back;
                                   printf("\n[ ok ] - excluido o %s %s",msg,idx->nome);
                                   liberaNo(current);
                                   break;
                           }

                           last=current;
                           current=current->back;
             }
       }
       else
       {
                           topo=topo->back;
                            printf("\n[ ok ] - excluido o %s %s",msg,idx->nome);
                           liberaNo(idx);
       }

       return topo;

}

No *pesquisa(No *topo,char *nome)
{
        No *idx;

        if(topo==NULL)
              return NULL;


       idx=topo;
       while(idx!=NULL)
       {

             if(strcmp(idx->nome,nome)==0)
                    break;
             else
                    idx=idx->back;
       }
       return idx;

}
int lista(No *topo)
{
        No *idx;
        int total=0;

        idx=topo;


       while(idx!=NULL)
       {

             printf("\n %d Nome: %s",total+1,idx->nome);
             idx=idx->back;
             total++;
       }
       printf("\n[ status] Total de alunos %d",total);
       return total;

}

int Registros(No *topo)
{
        No *idx;
        int total=0;
        idx=topo;
       while(idx!=NULL)
       {

             idx=idx->back;
             total++;
       }
       return total;

}

void ToUpper(char *nome)
{
       char *ch;
       ch=nome;

       while(ch)
       {
             *ch=toupper(*ch);
             ch++;
       }

}
No *edita(No *idx)
{

        No *topo;
        char nome[MAX_NOMEALUNO];
        No *current;
        topo        =idx->pilhacursos;
        current=NULL;

        int continua=1;

        while(continua)
        {
                    clrscr();
                    printf("\n[ status] - editando o aluno %s",idx->nome);
                    switch(menu(Registros(topo)))
                    {

                           case 'I':
                           {
                                  printf("\n[ wait] - digite o %s do %s: ",SZCURSO,SZALUNO);
                                  ToUpper(gets(nome));
                                  current=pesquisa(topo,nome);
                                  if(current==NULL)
                                               topo=insere(topo,nome);
                                  else
                                  {
                                        printf("\n[ error] - %s duplicado",SZCURSO);
                                        getch();

                                  }


                           }
                                  break;

                           case 'L':
                           {
                                  lista(topo);
                                  getche();
                           }
                                  break;
                           case 'E':
                           {
                                  if(lista(topo)>0)
                                  {
                                        printf("\n[ wait] - digite o nome do %s: ",SZCURSO);
                                        ToUpper(gets(nome));
                                        current=pesquisa(topo,nome);
                                        if(current==NULL)
                                        {
                                               printf("\n[ error ] - %s nao encontrado",SZCURSO);
                                               getch();

                                        }
                                        else
                                        {
                                               printf("\n[ ok    ] - %s excluido!",SZCURSO);
                                               topo=exclui(topo,current,SZCURSO);
                                               getche();
                                        }
                                  }
                                  else
                                  {
                                               printf("\n[ error ] - %s nao encontrado",SZCURSO);
                                               getch();

                                  }

                           }
                                  break;
                           case 'P':
                           {
                                  lista(topo);
                                  printf("\n[ wait] - digite o nome do : ",SZCURSO);
                                  ToUpper(gets(nome));
                                  current=pesquisa(topo,nome);
                                  if(current==NULL)
                                        printf("\n[ error ] - %s nao encontrado",SZCURSO);
                                  else
                                  printf("\n[ ok    ] - %s encontrado",SZCURSO);
                                   getche();
                           }
                                  break;
                           case 'S':
                                  continua=0;
                                  break;
                    }



        }
       idx->pilhacursos=topo;
       return idx;

}



Postar um comentário