Article View: es.comp.os.linux.programacion
Article #7845Re: Ayuda con listas en C.
From: Javier Perez
Date: Tue, 14 Jan 2003 05:11
Date: Tue, 14 Jan 2003 05:11
85 lines
3324 bytes
3324 bytes
Hola.
El codigo q has expuesto me resulta bastante ilegible, asi q te pongo un
ejemplo general:
struct nodo {
int valor;
struct nodo *sig;
};
struct nodo *anadir(struct nodo **lista, int valor)
{
struct nodo *aux, *ultimo;
aux = (struct nodo*)malloc(sizeof(struct nodo));
if (aux) { //si se creo con exito...
(*aux).valor = valor;
(*aux).sig = NULL;
if (*lista) //si la lista no esta vacia... a�adimos al final
{
for (ultimo=*lista; (*ultimo).sig; ultimo=(*ultimo).sig);
//obtenemos el ultimo nodo de la lista
(*ultimo).sig = aux;
} else { //si la lista esta vacia, lo situamos como primer elemento
*lista = aux;
}
}
}
Este es el metodo general para a�adir un elemento a una lista, y aunque lo
he hecho de cabeza,
este es un metodo "de libro".
Espero q te sirva.
Saludos.
"Cesar" <cesarvalgar@hotmail.com> escribi� en el mensaje
news:9225f930.0301122140.6f5cc93c@posting.google.com...
> Hola, os agradeceria enormemente que me ayudaseis para decirme si esta
> implementacion de listas esta bien, os pongo todo lo necesario del
> codigo. A veces me fallan cuando tengo que meter muchos elementos:
>
> /*
> Esto son las declaraciones principales, incluyo el campo length en la
> lista porque tengo que calcular muchas veces la longitud de la lista y
> asi el programa va mucho mas rapido:
> */
> typedef unsigned int t_element;
>
> typedef struct node_list_struct *node_list;
> struct node_list_struct {
> t_element element;
> struct node_list_struct *next;
> };
>
> typedef struct list_struct *t_list;
> struct list_struct {
> node_list list;
> int length;
> };
>
> /*
> Esto es la funcion para a�adir elementos:
> */
> void
> list_append(t_list *list, t_element element)
> {
> node_list aux1 = NULL, aux2 = NULL;
>
> aux1 = (node_list) malloc(sizeof(struct node_list_struct));
> aux1->element = element;
> aux1->next = NULL;
>
> if ( *list == NULL) {
> *list = (t_list) malloc(sizeof(struct list_struct));
> (*list)->list = aux1;
> (*list)->length = 1;
> } else {
> aux2 = (*list)->list;
> while (aux2->next != NULL)
> aux2 = aux2->next;
> aux2->next = aux1;
> (*list)->length++;
> }
> }
Technical Details
Message-ID:
<b006ck$8l7$1@news.ya.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.vmunix.org!feed.news.nacamar.de!easynet-melon!easynet.net!news.rediris.es!news.ya.com!yacom!not-for-mailReferences:
<9225f930.0301122140.6f5cc93c@posting.google.com>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,f8c239b9a1587006 X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2003-01-13 21:16:03 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.vmunix.org!feed.news.nacamar.de!easynet-melon!easynet.net!news.rediris.es!news.ya.com!yacom!not-for-mail From: "Javier Perez" <javiperez78@yahoo.es> Newsgroups: es.comp.os.linux.programacion Subject: Re: Ayuda con listas en C. Date: Tue, 14 Jan 2003 06:11:02 +0100 Organization: ya.com internet factory Lines: 87 Message-ID: <b006ck$8l7$1@news.ya.com> References: <9225f930.0301122140.6f5cc93c@posting.google.com> NNTP-Posting-Host: 80-35-157-10.adsl.ya.com X-Trace: news.ya.com 1042521305 8871 80.35.157.10 (14 Jan 2003 05:15:05 GMT) X-Complaints-To: newsmaster@ya.com NNTP-Posting-Date: Tue, 14 Jan 2003 05:15:05 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106