Saltar al contenido
Underc0de /foro

Lista Simplemente Enlazada

Iniciado por [Z]tuX, Mayo 05, 2013, 09:15:02 PM

Tema anterior - Siguiente tema

0 Miembros y 0 Visitantes están viendo este tema.

Avatar de [Z]tuX
Hola, últimamente he estado trabajando mucho con listas dinámicas, así que hoy traigo un ejemplo de una lista Simplemente Enlazada en lenguaje C, el cual requiere de uso de apuntadores, no es nada del otro mundo si se tiene claro el concepto, por lo que si no conoces las Listas Simplemente Enlazadas, te recomiendo que leas este artículo de Wikipedia. Los enlaces son solo para miembros. Registrate o iniciá sesión

Código: C

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

/* Ejemplo de Lista Simplemente Enlazada
* [Z]tuX
* http://ztux.blogspot.com/
*/

typedef struct Node{
    struct Node *next;
    int data;
}node;

node *newList(node *list);
node *insertInList(int data,node *list);
void showList(node *list);
int isEmpty(node *list);


int main(){
    node *LISTA = newList(LISTA);
    LISTA=insertInList(5,LISTA);
    LISTA=insertInList(6,LISTA);
    LISTA=insertInList(90,LISTA);
    LISTA=insertInList(0,LISTA);
    LISTA=insertInList(915,LISTA);
    showList(LISTA);
    return 0;
}

node *newList(node *list){
    return(list=NULL);
}

int isEmpty(node *list){
    return (list==NULL);
}

node * insertInList(int data,node *list){
    node * new_Node;
    new_Node = (node*)malloc(sizeof(node));
    node * aux;
    //First, check if the new node is not NULL
    if(new_Node!=NULL){
        new_Node->data=data;
        new_Node->next=NULL;
        if(isEmpty(list)){
            //If the list is empty...
            list=new_Node;
        }else{
            //If not is empty...
            aux = list;
            while(aux->next!=NULL){
                aux=aux->next;
            }
            aux->next=new_Node;
        }
    }else{
        printf("[!]There isn't space in memory...");
    }
    return list;
}

void showList(node *list){
    node *aux;
    for(aux=list;aux!=NULL;aux=aux->next){
        printf("Data: %d\n",aux->data);
    }
}


Salud[OS]
WhatsApp Telegram

¿Te quedó una duda sobre Códigos Fuentes?

Preguntala y te responde la comunidad (y el asistente Underc0de IA si nadie llega antes). Es gratis y tarda un minuto.

Ya tengo cuenta
Guardamos tu pregunta en este navegador: después de entrar la publicás con un clic.
Grupo de WhatsApp