Jump to content
Fórum Script Brasil
  • 0

[ajuda] listas estaticas


underground

Question

oi pessoal estou com problemas nesse código , ele insere um cadastro , mas quando insere o segundo , ele insere por cima, preciso de uma ajuda

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define num_nos 10

struct tipo_no{
       char nome[50];
       char cor[20];
       int ano;
       char chassis[25];
       int prox;
       };

struct tipo_no nos[num_nos];
       int inicioLista = -1;
       int noDisp = 0;

void init(){
     int i;
     for(i=0; i<num_nos -1; i++){
             nos[i].nome[0]='';
             nos[i].cor[0]='';
             nos[i].ano=0;
             nos[i].chassis[0]=''; 
             nos[i].prox = i+1;
     }  
     nos[num_nos -1].nome[0]='';
     nos[num_nos -1].cor[0]='';
     nos[num_nos -1].ano=0;
     nos[num_nos -1].chassis[0]=''; 
     nos[num_nos -1].prox = -1;
}    
       
int buscaNoDisp(){
    int a = noDisp;
    noDisp = nos[noDisp].prox;
    return(a);
}

int buscaUltimoNo(int ano){
    int posanterior = inicioLista;
    int posatual = inicioLista;
    while(posatual != -1){
                   posanterior = posatual;
                   posatual = nos[posatual].prox;
    }
                   return (posanterior);
}

int buscaChasi(char chasi1[]){
    int i, a;
    for (i=0; i<num_nos; i++){
        
        if (strcmp(nos[i].chassis,chasi1)==0){
           i=a;
        }
    }
    return (a);
}
              
              
    
    

void liberaNo(int no){
     if(no == inicioLista){
           inicioLista = nos[no].prox;
     }
     nos[no].prox = noDisp;
     noDisp = no;
}

void insereNo(char nome[50], char cor[20], int ano, char chassis[25]){
     int noD = buscaNoDisp();
     if(noD != -1){
            int ultimoNo = buscaUltimoNo(ano);
            if(inicioLista == -1){
                           inicioLista = noD;
                           ultimoNo = inicioLista;
            }
            nos[ultimoNo].prox = noD;               
            strcpy(nos[noD].nome, nome);
            strcpy(nos[noD].cor, cor);
            nos[noD].ano = ano;
            strcpy(nos[noD].chassis, chassis);
            nos[noD].prox = -1;
     }
}  

int main(){ 
    
    int opcao, opcao2, ano, year1;
    char nome[50], cor[20], chassis[25], chasi1[25], color1[20], name1[50];
    int i, indice=0;
    
    do{
           do{
                   printf("1 - Incluir novo carro \n");
                   printf("2 - Alterar os dados de um carro \n");
                   printf("3 - Remover um carro \n");
                   printf("4 - Listar todos os carros cadastrados \n");
                   printf("9 - Sair do programa \n");
                   printf("Digite a opcao desejada:");
                   scanf("%i", &opcao);
                   printf("\n");
                   
                   }while(opcao != 1 && opcao != 2 && opcao != 3 && opcao != 4 && opcao != 9);
                   
           switch(opcao){                          
                                 case 1:
                                      printf("Digite o nome: \n");
                                      scanf("%s", &nome);
                                      printf("Digite a cor: \n");
                                      scanf("%s", &cor);
                                      printf("Digite o ano: \n");
                                      scanf("%d", &ano);
                                      printf("Digite o chassis: \n");
                                      scanf("%s", &chassis);
                                      insereNo(nome, cor, ano, chassis);
                                      
                                 break;
                                 
                                 case 2:
                                      printf("Digite o chassis do carro.\n");
                                      scanf("%s", &chasi1);
                                      indice=buscaChasi(chasi1);
                                      
                                      
                                      do{
                                           do{
                                                   printf("1 - Alterar o nome. \n");
                                                   printf("2 - Alterar a cor. \n");
                                                   printf("3 - Alterar o ano. \n");
                                                   printf("9 - Voltar ao menu principal. \n");
                                                   scanf("%i", &opcao2);
                                                   
                                                   }while(opcao2 != 1 && opcao2 != 2 && opcao2 != 3 && opcao2 != 9);
                                           switch(opcao2){                          
                                                                 case 1:
                                                                      printf("Digite o nome: \n");
                                                                      scanf("%s", &name1);
                                                                      strcpy(name1, nos[indice].nome);
                                                                 break;
                                                                 
                                                                 case 2:
                                                                      printf("Digite a cor: \n");
                                                                      scanf("%s", &color1);
                                                                      strcpy(nos[indice].cor, color1);
                                                                 break;
                                                                 
                                                                 case 3:
                                                                      printf("Digite o ano: \n");
                                                                      scanf("%d", &year1);
                                                                      nos[indice].ano = year1;
                                                                 break;
                                                                 }     
                                           }while(opcao2 != 9);
                                      
                                 break;
                                      
                                 case 4:
                                      
                                      printf("\n");
                                      for(i=0; i<2; i++){
                                            printf("Nome:%s \n",nos[i].nome);
                                            printf("Cor:%s \n", nos[i].cor);
                                            printf("Ano:%i \n", nos[i].ano);
                                            printf("Chassis:%s \n", nos[i].chassis);
                                      }
                                 break;
                                                             
                                      
                                      
                   
           }
    }while(opcao != 9);               
        
    return 0;
}

Edited by underground
Adicionar tag CODE
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...