Jump to content
Fórum Script Brasil
  • 0

Lista Encadeada


Cleber Griff

Question

Tenho esse código e, como podem ver, as variáveis Lista *inicio e Lista *fim estão globais, como eu passo elas para local?

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

typedef struct lista{
    int info;
    struct lista *prox;
}Lista;

Lista *inicio=NULL, *fim=NULL;

int inserir_lista(int elem, int pos){
    Lista *no;

    no = (Lista*) malloc (sizeof(Lista));

    if(no==NULL) return 0;

    else{
        no->info=elem;
        if(inicio==NULL){
            inicio=no;
            fim=no;
            no->prox=NULL;
            return 1;
        }

        else{
            if(pos==1){
                no->prox=inicio;
                inicio=no;
                return 1;
            }

            else if(pos==2){
                fim->prox=no;
                no->prox=NULL;
                fim=no;
                return 1;
            }

            else{
                int achou=0;
                int item;
                Lista *aux=inicio;

                printf("Inserir apos qual elemento?\n");
                scanf("%d", &item);

                while(aux!=NULL){
                    if(aux->info==item){
                        no->prox=aux->prox;
                        aux->prox=no;
                        achou=1;

                        if(aux==fim){
                            fim=no;
                            break;
                        }
                    }
                    aux=aux->prox;
                }
                return achou;
            }
        }


    }
}

void imprimir_lista(){
    Lista *aux;

    aux = (Lista*) malloc (sizeof(Lista));

    aux = inicio;

    while(aux!=NULL){
        printf("\n%d", aux->info);
        aux=aux->prox;
    }
}

void ordenada(){
    Lista *aux, *proxima;
    int ordenada=0;

    aux = (Lista*) malloc (sizeof(Lista));
    proxima = (Lista*) malloc (sizeof(Lista));

    aux = inicio;

    if(aux==NULL){
        printf("\n\nLista vazia\n\n");
    }

    else if(aux->prox==NULL){
        printf("\n\nLista contem apenas 1 elemento\n\n");
    }

    else{
        proxima=aux->prox;

        if(aux->info < proxima->info){
            while(proxima!=NULL){
                if(aux->info < proxima->info){
                    ordenada=1;
                    aux=aux->prox;
                    proxima=proxima->prox;
                }

                else{
                    ordenada=0;
                    break;
                }
            }
        }

        else{
            aux=inicio;
            proxima=aux->prox;

            while(proxima!=NULL){
                if(aux->info > proxima->info){
                    ordenada=1;
                    aux=aux->prox;
                    proxima=proxima->prox;
                }

                else{
                    ordenada=0;
                    break;
                }
            }
        }

        if(ordenada) printf("\n\nLista ordenada\n\n");
        else printf("\n\nLista não-ordenada\n\n");
    }
}

int main(){
    int elem, pos, opcao;

    do{
        printf("\n\nEscolha opcao: \n1- Inserir na lista\n2- Verificar - Ordenada\n3- Exibir lista\n4- Copiar lista\n5- Sair\n>>>> ");
        scanf("%d", &opcao);

        if(opcao==1){
            printf("\nInforme o elemento:\n");
            scanf("%d", &elem);
            printf("Aonde deseja inserir?\n1- Inicio\n2- Fim\n3- Apos determinado numero\n");
            scanf("%d", &pos);
            if (inserir_lista(elem, pos)) printf("\nElemento inserido com sucesso");
            else printf("Erro");
        }

        if(opcao==2) ordenada();

        if(opcao==3) imprimir_lista();

    }while(opcao!=5);
}

Edited by Cleber Griff
Link to comment
Share on other sites

2 answers 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
      152k
    • Total Posts
      651.5k
×
×
  • Create New...