Ir ao conteúdo
  • Cadastre-se

jcgs98

Membro Pleno
  • Posts

    90
  • Cadastrado em

  • Última visita

Tópicos solucionados

  1. O post de jcgs98 em Funções para Lista Duplamente Encadeadas. foi marcado como solução   
    void remRepListaOrd(TLista *L2,TLista *U2) { TLista L=*L2, U=*U2; if ((!L)||(!L->prox)) return; remover (&L->prox, &U,L->valor); remRepListaOrd(&L->prox,&U); }  
    uso a função de remover a partir do próximo da lista.
    @devair1010 Não deve ter rodado por causa dessa forma de "escrever":
     
    (*U)?novo->ant=*U,(*U)->prox=novo,*U=novo:*L = novo;
     
    Tenta mudar.
     
    Por exemplo,
     
    (*U)?novo->ant=*U,(*U)->prox=novo,*U=novo:*L = novo;
     
    é igual a:
     
    if (*U) { novo->ant=*U; (*U)->prox=novo; *U=novo; }
    else
    *L = novo;
    #include <stdio.h> #include <stdlib.h> typedef struct No { int valor; struct No *prox, *ant; } TNo; typedef TNo* TLista; int inserir (TLista *L, TLista *U, int num); int remover (TLista *L, TLista *U,int numero); int alterar (TLista L, int velho, int novo); TLista pesquisar (TLista L, int num); void exibir (TLista L); void exibirAoContrario (TLista U); void clonar (TLista *L, TLista L2); int inserirNaPosicao (TLista *L, TLista *U, int posicao, int numero); int menu (TLista L, TLista L2); int tamanho (TLista L); void insereFim(TLista *L, TLista *U, int numero); void botarCrescente(TLista *L); void inverterLista (TLista *L,TLista *U); float media(TLista L2); int pesquisaMaior(TLista L2); int pesquisaMenor(TLista L2); float somaDosElementos(TLista L2); void remRepListaOrd(TLista *L, TLista *U); int main () { TLista L=NULL, L2=NULL, U=NULL, pos; int num1, num2, op; do { system ("cls"); op = menu (L, L2); switch (op) { //INSERIR case 1: printf ("Entre com o número: "); scanf ("%d", &num1); (inserir (&L, &U, num1))?printf ("Inserção realizada!\n"):printf ("ERRO: Inserção não realizada!\n"); break; //REMOVER case 2: printf ("Entre com o número a ser removido: "); scanf ("%d", &num1); printf ((!remover (&L, &U, num1))?"ERRO: Remoção não realizada!\n":"Remoção realizada %i vezes!\n", remover (&L, &U, num1)); break; //ALTERAR case 3: printf ("Entre com o número a ser alterado: "); scanf ("%d", &num1); printf ("Entre com o novo número: "); scanf ("%d", &num2); printf ((alterar (L, num1, num2))?"Alteração realizada!\n":"ERRO: Alteração não realizada!\n"); break; //PESQUISAR case 4: printf ("Entre com o número a ser pesquisado: "); scanf ("%d", &num1); printf ((pesquisar (L, num1))?"O número foi encontrado na lista!\n":"ERRO: O número não se encontra na lista!\n"); break; //EXIBE NORMAL case 5: (!L)?printf("Lista vazia!\n"):printf("Lista: "),exibir (L),printf("\n"); break; //EXIBE INVERTIDO case 6: (!U)?printf("Lista vazia!\n"):printf("Lista: "),exibirAoContrario (U),printf("\n"); break; //CLONAR case 7: clonar (&L2,L); break; //INSERE NA POSIÇÃO case 8: printf ("Entre com o número a ser inserido: "); scanf ("%d", &num1); printf ("Entre com o posicao onde número >>%i<< vai ser inserido: ", num1); scanf ("%d", &num2); printf ((inserirNaPosicao (&L, &U, num2, num1))?"Insercao realizada!\n":"ERRO: Inserção não realizada!\n"); break; //INSERE NO FIM case 9: printf ("Entre com o número a ser inserido: "); scanf ("%d", &num1); insereFim(&L, &U, num1); break; //ORDENA CRESCENTE case 10:printf((!L)?"Lista vazia!\n":0),botarCrescente(&L); break; //remover repeticao case 11:remRepListaOrd(&L,&U); break; //INVERTER case 12:inverterLista (&L,&U); break; //fim case 0: printf ("Fim do programa!\n"); break; //erro default:printf ("Opção inválida!\nTente novamente!\n"); } system ("pause"); } while (op != 0); } int menu (TLista L, TLista L2) { int opcao; system ("cls"); printf ("\nTamanho da Lista: %i\n\n",tamanho (L)); printf ("Lista: ");exibir (L); printf ("|\n"); printf ("Clone: ");exibir (L2); printf ("|\n\n"); printf ("Menor elemento da lista......: %i. \n", pesquisaMenor(L)); printf ("Maior elemento da lista......: %i. \n", pesquisaMaior(L)); printf ("Media dos elementos da lista.: %.2f. \n\n", media(L)); printf ("Menu de opções:\n\n"); printf ("( 1)\tInserir\n( 2)\tRemover\n( 3)\tAlterar\n"); printf ("( 4)\tPesquisar\n( 5)\tExibir na Ordem Natural\n"); printf ("( 6)\tExibir na Ordem Inversa\n( 7)\tClonar\n"); printf ("( 8)\tInserir na Posicao\n( 9)\tInsere no Fim\n(10)\tOrdenar Lista\n"); printf ("(11)\tRemove Repeticoes da lista; printf ("(12)\tInverter Lista\n(13)\tComparar Listas(*FAZER)\n( 0)\tSair\n\n"); printf ("Entre Com a Opcao: "); scanf ("%d", &opcao); return opcao; } int inserir (TLista *L, TLista *U, int num) { TLista aux = (TLista) malloc (sizeof(TNo)); if (!aux) { return 0; } else { aux->valor = num; aux->prox = *L; aux->ant = NULL; if (*L) { (*L)->ant = aux; } else { *U = aux; } *L = aux; return 1; } } int remover (TLista *L, TLista *U,int numero) { TLista aux1, aux2, aux3; int cont; while ((*L) && ((*L)->valor == numero)) { aux2 = (*L)->ant; aux1 = (*L)->prox; free (*L); cont++; *L = aux1; (*L)?(*L)->ant = aux2:0; } if (!*L) { *U=NULL; } else { aux2 = *L; aux1 = (*L)->prox; while (aux1) { if (aux1->valor == numero) { aux3 = aux2; (*U==aux1)?*U=aux3:0; (aux1->prox)?aux1->prox->ant=aux2:0; aux2->prox = aux1->prox; free (aux1); cont++; aux1 = aux2->prox; } else { aux2 = aux1; aux1 = aux1->prox; } } } return cont; } int alterar (TLista L, int velho, int novo) { TLista aux = L; int c = 0; while (aux) { if (aux->valor == velho) { aux->valor = novo; c++; } aux = aux->prox; } return c; } TLista pesquisar (TLista L, int num) { if (L==NULL) return NULL; if (L->valor==num) return L; pesquisar (L->prox, num); } void exibir (TLista L2) { TLista L=L2; if(L) { printf ("%d ", L->valor); exibir (L->prox); } } void exibirAoContrario (TLista U2) { TLista U=U2; if(U) { printf ("%d ", U->valor); exibirAoContrario (U->ant); } } void clonar (TLista *L, TLista L2) { TLista L3=L2; TLista U; while(L3) { insereFim(&*L, &U, L3->valor); L3=L3->prox; } } int inserirNaPosicao (TLista *L, TLista *U, int posicao, int num) { int t=tamanho (*L), con=0; TLista aux2=*L, aux, novo; if(!posicao||posicao>t+1) { return 0; } if(posicao==1) { inserir (&*L, &*U, num); return 1; } if(posicao==t+1) { insereFim(&*L, &*U, num); return 1; } posicao=posicao-1; novo=(TLista) malloc (sizeof(TNo)); novo->valor=num; while (con<posicao-1) { aux2=aux2->prox; con++; } aux=aux2->prox; novo->ant=aux2; aux2->prox=novo; aux->ant=novo; novo->prox=aux; return 1; } void insereFim(TLista *L, TLista *U, int numero) { TLista novo=(TLista) malloc(sizeof(TNo)); novo->prox=NULL; novo->valor=numero; (*U)?novo->ant=*U,(*U)->prox=novo,*U=novo:*L = novo; *U = novo; } void botarCrescente(TLista *L) { TLista genesis = *L, ordenada; int gV,oV; while(genesis) { ordenada=genesis->prox; while(ordenada) { if(genesis->valor > ordenada->valor) { gV=genesis->valor; oV=ordenada->valor; ordenada->valor=gV; genesis->valor=oV; } ordenada = ordenada->prox; } genesis=genesis->prox; } } int tamanho (TLista L1) { TLista L=L1; if (!L) return 0; return (1+tamanho (L->prox)); } void inverterLista (TLista *L,TLista *U) { TLista aux=*L; *L=NULL; *U=NULL; while(aux) { inserir (&*L, &*U, aux->valor); aux=aux->prox; } } int pesquisaMaior(TLista L2) { TLista L=L2; if (!L) return 0; int maior = pesquisaMaior(L->prox); if (L->valor > maior) return L->valor; return maior; } int pesquisaMenor(TLista L2) { TLista L=L2; if (!L) return 0; int menor=L->valor; while(L) { if(L->valor < menor) { menor = L->valor; } L = L->prox; } return menor; } float media(TLista L2) { if (L2) return (somaDosElementos(L2)/tamanho(L2)); return 0; } float somaDosElementos(TLista L2) { TLista L=L2; if (!L) return 0; return L->valor + somaDosElementos(L->prox); } void remRepListaOrd(TLista *L2,TLista *U2) { TLista L=*L2, U=*U2; if ((!L)||(!L->prox)) return; remover (&L->prox, &U,L->valor); remRepListaOrd(&L->prox,&U); } FIZ A 11
  2. O post de jcgs98 em Exibir Lista Encadeada Linguagem C foi marcado como solução   
    #include <stdio.h> #include <stdlib.h> typedef struct No { int valor; struct No *prox; } TNo; typedef TNo* TLista; void exibir (TLista L) { TLista i; printf("\nMostrando a lista:\t"); if(L) for (i=L;L!=NULL;L=L->prox) { printf("%d\t", i->valor); i=i->prox; } else printf("Lista vazia."); printf("\n"); }  
  3. O post de jcgs98 em Criar Lista Encadeada Com PA foi marcado como solução   
    #include <stdio.h> #include <stdlib.h> typedef struct No { int valor ; struct No *prox ; } TNo ; typedef TNo* TLista ; void criarPA (TLista *L,int N,int A1,int R) { TLista fim; int i=0,aux2=A1; while(i<N) { TLista aux = (TLista) malloc (sizeof(TNo)); aux->valor = A1; aux->prox = NULL; A1=A1+R; if(*L==NULL) { *L=aux; } else { fim->prox=aux; } fim=aux; i++; } }  
  4. O post de jcgs98 em Inverter Lista Encadeada Existente foi marcado como solução   
    #include <stdio.h> #include <stdlib.h> typedef struct No { int valor ; struct No *prox ; } TNo ; typedef TNo* TLista ; void inverter (TLista *L, TLista L2) { while(L2) { TLista aux = (TLista) malloc (sizeof(TNo)); aux->valor = L2->valor; aux->prox = *L; *L = aux; L2=L2->prox; } }  
  5. O post de jcgs98 em Verificar Se Lista Encadeada Está Ordenada foi marcado como solução   
    #include <stdio.h> #include <stdlib.h> typedef struct No { int valor ; struct No *prox ; } TNo ; typedef TNo* TLista ; int estaOrdenada(TLista L) { if (L != NULL) { while (L->prox != NULL) { if (L->valor > L->prox->valor) return 0; L = L->prox; } } return 1; }  
  6. O post de jcgs98 em Recursividade. Comparar Caracteres De Duas Strings. foi marcado como solução   
    #include <string.h> #include <strings.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> int comparaString2(char str1,char* str2); int comparaString (char* str1,char* str2); int main(void) { int cont=0; char str1[100],str2[100]; fflush(stdin); printf("\nString1...:\n"); gets(str1); fflush(stdin); printf("\nString2...:\n"); gets(str2); cont=comparaString (str1,str2); printf("\nExistem %i caracteres comuns entre as duas strings\n",cont); return 0; } int comparaString (char* str1,char* str2) { if (!str1[0]) { return 0; } return(comparaString2(str1[0],str2)+comparaString(++str1,str2)); return (comparaString (++str1,str2)); } int comparaString2(char str1,char* str2) { if (!str2[0]) { return 0; } if (str2[0]==str1) { return (1+comparaString2(str1,++str2)); } else return comparaString2(str1,++str2); } Solução!
  7. O post de jcgs98 em Inserindo valor em determinada posição da lista encadeada. Linguagem C. foi marcado como solução   
    @devair1010 Conitnue fazendo do meu jeito; é mais didático.
    As pessoas esquecem que vocês vem aqui para aprender.
    Sempre me irritei com algumas respostas que nada ajudam quem vem aqui pedir auxílio.
    Isso tudo acabou com minha chegada.
    Mesmo que digam que o post é inútil (se ensina é bem útil).
    Vou ver seu problema e tentar te explicar.
    @devair1010
    "ele diz que não tem a função main nesse código"
    Sim, não tem: é "só" uma função.
    Vou postar com a main para fins didáticos.
    Primeiro você deve construir a estrutura que vai criar os nós:
    //ESTRUTRA DO NÓ
    typedef struct No {
    int valor;
    struct No *prox;
    }TNo;
     
    //SERVER PARA VOCÊ NÃO TER QUE FAZER PONTEIRO DE PONTEIRO (**) E SE CONFUNDIR
    typedef TNo* TLista;
     
    #include <locale.h> #include <stdio.h> #include <stdlib.h> typedef struct No { int valor; struct No *prox; } TNo; typedef TNo* TLista; int insereNaPosicao (TLista *Lista,int inserido,int posicao); int colocarElementosNaLista (TLista *Lista, int elemento); void exibirElementosDaLista (TLista Lista); int main (void) { TLista Lista = NULL; int opcao, num1, num2; setlocale(LC_ALL,""); do { system ("cls"); printf ("Opções:\n\n"); printf ("(1)\tInserir na Lista\n(2)\tInserir Em Determinada Posição\n"); printf ("(3)\tExibir Lista Criada\n(4)\tSair\n\n"); printf ("Entre com a sua opção: "); scanf ("%d", &op); switch (op) { case 1: printf ("Entre com o número a ser inserido: "); scanf ("%d", &num1); if (colocarElementosNaLista (&Lista, num1)) { printf ("Elemento inserido!\n"); } else { printf ("ERRO: Elemento não inserido!\n"); } break; case 2: printf ("Entre com o número a ser inserido: "); scanf ("%d", &num1); printf ("Entre com o posição: "); scanf ("%d", &num2); if (insereNaPosicao (&Lista,num1, num2)) { printf ("Elemento %i inserido na %ia. posição da lista!\n",num1,num2); } else { printf ("ERRO: Elemento não inserido na lista!\n"); } break; case 3: exibirElementosDaLista (Lista); break; case 4: printf ("Fim do programa!\n"); break; default: printf ("Opção inválida! Tente novamente.\n"); } system ("pause"); } while (opcao != 4); } int insereNaPosicao (TLista *Lista,int inserido,int posicao) { int con=0,CON=0; TLista AUX, aux, novo, tam; AUX=*Lista; tam=*Lista; posicao=posicao-1; novo=(TLista) malloc (sizeof(TNo)); novo->valor = inserido; while (tam!=NULL) { tam=tam->prox; CON++; } if (posicao>CON||posicao<0) { return 0; } while (con<posicao-1) { AUX=AUX->prox; con++; } aux=AUX->prox; AUX->prox=novo; novo->prox=aux; return 1; } int colocarElementosNaLista (TLista *Lista, int numero) { TLista aux = (TLista) malloc (sizeof(TNo)); if (!aux) //(aux == NULL) { return 0; } else { aux->valor = numero; aux->prox = *Lista; *Lista = aux; return 1; } } void exibirElementosDaLista (TLista Lista) { TLista i; if (!Lista)//if (Lista == NULL) { printf ("Lista vazia!\n"); } else { printf ("Elementos: "); i = Lista; while (i) //while (i != NULL) { printf ("%d ", i->valor); i = i->prox; } printf ("\n"); } } @devair1010 código completo

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!