Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Lista Linear

#include<stdio.h>
#include<stdlib.h>
#define MAX 11

struct lista
{
    int FL;
    int dados[MAX];
};

typedef struct lista Lista;

Lista *Cria_lista(void)
{
    Lista *pt = (Lista *) malloc(sizeof(Lista));
    if (pt != NULL)
        pt->FL = 0;
    return pt;
}

Lista *Libera_lista(Lista *Ptl)
{
    free(Ptl);
    //Debug
    printf("Liberando lista da memoria\n");
    return NULL;
}

Lista *Insere_elem(Lista *Ptl, int elem)
{

    if (Ptl == NULL || Ptl->FL == MAX){
        printf("Nao foi possivel inserir elemento, LISTA CHEIA!!!!\n");
        return Ptl;
    }
        
    // insere elemento no final da lista
    Ptl->dados[Ptl->FL] = elem;
    Ptl->FL++;
    return Ptl;
}

Lista *Remove_elem_mov(Lista *Ptl, int elem)
{
    int i, k;
    if (Ptl == NULL || Ptl->FL == 0)
        return Ptl;
    // procura elemento na lista

    i = 0;
    while (i < Ptl->FL && Ptl->dados[i] != elem)
        i++;

    if (i == Ptl->FL) // elemento nao encontrado
        return Ptl;

    //elemento encontrado na posiçao i. Remover elemento
    // com movimentaçao de dados

    for (k = i; k < Ptl->FL - 1; k++)
        Ptl->dados[k] = Ptl->dados[k + 1];

    // atualiza fim de lista
    Ptl->FL--;
    return Ptl;
}

int Consulta_nodo(Lista *Ptl, int pos, int *info)
{

    if (pos <= 0 || pos > Ptl->FL)
        return 0;

    *info = Ptl->dados[pos - 1];
    return 1;
}

int E_cheia(Lista *Ptl)
{
    if (Ptl->FL == MAX)
        return 1;
    else
        return 0;
}

int E_vazia(Lista *Ptl)
{
    if (Ptl->FL == 0){
        //Codigo para debub
        printf("Lista Vazia\n");
        return 1;
    }
    else{
        //Codigo para debug
        printf("Lista contem elementos\n");
        return 0;
    }
        
}

int Tamanho_lista(Lista *Ptl)
{
    if (Ptl == NULL)
        return -1;
    else
        return Ptl->FL;
}

int Imprime_lista(Lista *Ptl){
    int elemento;
    int tamanho = Tamanho_lista(Ptl);
    printf("\n*** IMPRIMINDO ELEMENTOS DA LISTA ***\n");
    
    for(int i=1; i<=tamanho;i++){
    Consulta_nodo(Ptl, i, &elemento);
    printf("%d - %d\n", i, elemento);    
 }
}

int main() {
 Lista* minhaLista;
 minhaLista = Cria_lista();
 //Libera_lista(minhaLista);
 
 int tamanho = Tamanho_lista(minhaLista);
 printf("Tamanho da lista = %d\n", tamanho);
 
 Insere_elem(minhaLista,10);
 Insere_elem(minhaLista,20);
 Insere_elem(minhaLista,30);
 Insere_elem(minhaLista,40);
 Insere_elem(minhaLista,50);
 Insere_elem(minhaLista,60);
 Insere_elem(minhaLista,60);

 
 int vazia = E_vazia(minhaLista);
 tamanho = Tamanho_lista(minhaLista);
 printf("Tamanho da lista = %d\n", tamanho);
 
 
 
 //Imprime todos elementos da lista na unha
 int elemento;
 for(int i=1; i<=tamanho;i++){
    Consulta_nodo(minhaLista, i, &elemento);
    printf("elemento da lista = %d\n", elemento);    
 }
 
 //Libera_lista(minhaLista);
 
 //Imprime pela operacao
 Imprime_lista(minhaLista);
 
 
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.