Merge static lists in C

Asked

Viewed 39 times

0

#include<stdio.h>  
#include<stdlib.h>  
#define tam 10  
#define TAM2 20  



typedef int Apontador;


typedef struct{  
    int item;  
}Elemento;

typedef struct{  
    Elemento elemento[tam];  
    Elemento intercalar[TAM2];// feito para intercalar as listas   
    Apontador inicio, fim;  
}Lista;


void startList(Lista* lst){
    lst->inicio=0;
    lst->fim=lst->inicio;
}

int insertList(Elemento E, Lista* lst){
    
    if(lst->fim>tam-1){
        printf("Lista cheia\n");
        return 0;
    }
    else{
        lst->elemento[lst->fim]=E;
        lst->fim++;
        return 1;
    }

}


int insertListAlt(Elemento E, Lista* lst){
    
    if(lst->fim>TAM2-1){
        printf("Lista cheia\n");
        return 0;
    }
    else{
        printf("\n elemento inserido: %d\n",E);
        lst->intercalar[lst->fim]=E;
        lst->fim++;
        return 1;
    }
    
}


void ImprimirLista(Lista* lst){
    int aux;
    
    for(aux=lst->inicio;aux<=(lst->fim-1);aux++){
        printf("\n%d\t",lst->elemento[aux].item);       
    }
        
}

void ImprimirListaAlt(Lista* lst){
    int aux;
    
    for(aux=0;aux<TAM2;aux++){
        printf("\n%d\t",lst->intercalar[aux].item);     
    }
        
}


int VerificaOrdenacao(Lista* lst, int n){
    int aux,k=1;
    
    for(aux=lst->inicio; aux<=(lst->fim-1);aux++){
        
        if(lst->elemento[aux+1].item > lst->elemento[aux].item){
            k++;
            if(k==n){
                printf("Ordenacao crescente.");
                return 1;
            }
                    
        }
    //  printf("contador k: %d\t;",k);
    }
    
    for(aux=lst->inicio; aux<=(lst->fim-1);aux++){
        
        if(lst->elemento[aux+1].item < lst->elemento[aux].item){
            k++;
            if(k==n){
                printf("Ordenacao decrescente.");
                return 1;
            }
                    
        }
    //  printf("contador k: %d\t;",k);
    }
    
    return 0;

    
    
}


int listasIguais(Lista* lst1, Lista* lst2){
    
    int aux;
    
    for(aux=0;aux<=tam;aux++){// REVER ESSA REPETIÇÃO
    
        if(lst1->elemento[aux].item!=lst2->elemento[aux].item){
            return 0;
            printf("erro");
        }
    }
    return lst1==lst2;
    
    
}


int lst_copia(Lista* lst1, Lista* lst2){
    int aux;
    
    for(aux=0;aux<=tam; aux++){// rever repetição
        lst2->elemento[aux]=lst1->elemento[aux];
        insertList(lst2->elemento[aux],lst2);
    }
    return 1;
}


int lst_inverte(Lista* lst1, Lista* lst2){
    int aux,i=0;
    
    for(aux=tam-1;aux>=0;aux--){
        lst2->elemento[i]=lst1->elemento[aux];
        insertList(lst2->elemento[i],lst2);
        i++;// rever
    }
    i=0;
    return 1;
}

int lst_intercalar(Lista* lst1, Lista* lst2,Lista* itc){
    int aux;
    
    for(aux=0;aux<TAM2;aux++){
        
        if(aux%2==0){   
            insertListAlt(lst2->elemento[aux], itc);    
        }
        if(aux%2!=0){   
            insertListAlt(lst1->elemento[aux], itc);    
        }   
        
    }

    return 1;
    
}



main(){  
    Elemento a1,a2;    
    Lista lstnum1, lstnum2,intercal;  
    startList(&lstnum1);  
    startList(&lstnum2);  
    startList(&intercal);  
    
        
    printf("\nPreencha a lista 1 com numeros inteiros\n");
    
    for(int i=0;i <tam; i++){
        printf("\nInformar o %d.o numero: ",i+1);
        scanf("%d",&a1.item);
        insertList(a1,&lstnum1);
    }
        
    printf("\nPreencha a lista 2 com numeros inteiros\n");
    
    for(int i=0;i <tam; i++){
        printf("\nInformar o %d.o numero: ",i+1);
        scanf("%d",&a2.item);
        insertList(a2,&lstnum2);
    }

    printf("\n\n");
    
//  listasIguais(&lstnum1,&lstnum2);
    
//  VerificaOrdenacao(&lstnum1, tam);
    
//  ImprimirLista(&lstnum1);
    
//  lst_copia(&lstnum1,&lstnum2);
    
//  lst_inverte(&lstnum1, &lstnum2);
    
//  ImprimirLista(&lstnum2);
    
    lst_intercalar(&lstnum1, &lstnum2,&intercal);
    ImprimirListaAlt(&intercal);
    
    
    
}

Guys, I’m not getting two static lists. As I had created two lists of size 10, I created another list only of size 20. For intercalation I define that if the index is even, it goes to one list and if it is odd it goes to another. Only an error is occurring and these numbers are not being entered.

1 answer

0

I thank everyone who viewed it. My mistake is in the repetition of the lst_intercalar function. As I fill two static lists, both addresses ranging from 0 to 9(10 elements), within the function, only 10 values will be inserted instead of 20.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.