Listas doble enlazadas, árboles binarios, pilas y colas en C: código y recorridos

Informática y Telecomunicaciones4,46 KB

Listas doblemente enlazadas

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

struct nodo{
    int dato;
    struct nodo *ant, *sig;
};

void agregarinicio(struct nodo **lista, int d)
{
    if(*lista==NULL)
    {
        *lista=(struct nodo *)malloc(sizeof(struct nodo));
        (*lista)->dato=d;
        (*lista)->ant=NULL;
        (*lista)->sig=NULL;
    }
    else
    {
        struct nodo *temp;
        temp=(struct nodo *)malloc(sizeof(struct nodo));
        temp->dato=d;
        temp->ant=NULL;
        temp->sig=*lista;
        (*lista)->ant=temp;
        *lista=temp;
    }
}

void agregarfinal(struct nodo **lista, int d)
{
    struct nodo *temp;
    for(temp=*lista;temp->sig!=NULL;temp=temp->sig);
    struct nodo *temp2;
    temp2=(struct nodo*)malloc(sizeof(struct nodo));
    temp2->dato=d;
    temp2->sig=NULL;
    temp2->ant=temp;
    temp->sig=temp2;
}

void eliminar(struct nodo **lista, int d)
{
    struct nodo *temp, *temp2;
    for(temp=*lista;temp->sig->dato!=d;temp=temp->sig)
    temp2=(struct nodo*)malloc(sizeof(struct nodo));
    temp2=temp->sig;
    temp->sig=temp2->sig;
    temp2->ant=temp;
    free(temp2);
}

void buscar(struct nodo *lista, int d)
{
    if(lista==NULL)
    {
        printf("no esta");
        return;
    }
    if(lista->dato==d)
    {
        printf("si esta");
        return;
    }
    buscar(lista->sig,d);
}

void mostrar(struct nodo *lista)
{
    if(lista==NULL)
    {
        return;
    }
    printf("%i",lista->dato);
    mostrar(lista->sig);
}

Árbol binario

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

struct nodo{
    int dato;
    struct nodo *der, *izq;
};

void agregararbol(struct nodo **arbol, int d)
{
    if (*arbol==NULL)
    {
        *arbol=(struct nodo*)malloc(sizeof(struct nodo));
        (*arbol)->dato=d;
        (*arbol)->der=NULL;
        (*arbol)->izq=NULL;
    }
    else
    {
        if((*arbol)->dato<d)
        {
            agregararbol(&((*arbol)->der),d);
        }
        if((*arbol)->dato>d)
        {
            agregararbol(&((*arbol)->izq),d);
        }
    }
}

void mostrar(struct nodo *arbol)
{
    if(arbol==NULL)
    {
        return;
    }
    printf("%i\n",arbol->dato);
    mostrar(arbol->izq);
    mostrar(arbol->der);
}

void buscar(struct nodo *arbol, int d)
{
    if(arbol==NULL)
    {
        printf("No está");
        return;
    }
    if(arbol->dato==d)
    {
        printf("Sí está");
        return;
    }
    if(arbol->dato>d) buscar(arbol->izq,d);
    if(arbol->dato<d) buscar(arbol->der,d);
}

Pilas y colas

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

struct nodo{
    int dato;
    struct nodo *sig;
};

void agregarpila(int d, struct nodo **lista)
{
    if (*lista==NULL)
    {
        *lista=(struct nodo*)malloc(sizeof(struct nodo));
        (*lista)->dato=d;
        (*lista)->sig=NULL;
    }
    else
    {
        struct nodo *temp;
        temp=(struct nodo*)malloc(sizeof(struct nodo));
        temp->dato=d;
        temp->sig=NULL;
        struct nodo *temp2=NULL;
        for(temp2=*lista;(temp2->sig)!=NULL;temp2=temp2->sig);
        temp2->sig=temp;
    }
}

void eliminarpila(struct nodo **lista)
{
    struct nodo *temp;
    for(temp=*lista;(temp->sig->sig)!=NULL;temp=temp->sig);
    temp->sig=NULL;
}

void mostrar(struct nodo *lista)
{
    struct nodo *temp;
    for(temp=lista;temp!=NULL;temp=temp->sig)
    {
        printf("%i", temp->dato);
    }
}

void agregarcola(int d, struct nodo **lis)
{
    if(*lis==NULL)
    {
        *lis=(struct nodo*)malloc(sizeof(struct nodo));
        (*lis)->dato=d;
        (*lis)->sig=NULL;
    }
    else
    {
        struct nodo *temp=NULL;
        temp=(struct nodo*)malloc(sizeof(struct nodo));
        temp->dato=d;
        temp->sig=NULL;
        struct nodo *temp2=NULL;
        for(temp2=*lis;temp2->sig!=NULL;temp2=temp2->sig);
        temp2->sig=temp;
    }
}

void eliminarcola(struct nodo **lis)
{
    *lis=(*lis)->sig;
}

Recorrido de árboles

  • Inorder: Izq → Muestro → Der
  • Preorder: Muestro → Izq → Der
  • Postorder: Izq → Der → Muestro

Descargar

PDF Word HTML