Saltar al contenido principal
LibreTexts Español

11.2: Ejemplo de lista enlazada

  • Page ID
    82430
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Ahora para un ejemplo concreto. Una definición típica de estructura puede verse así:

    struct Marmot (
        struct Marmot *NextMarmot;
        float Age;
        float Weight;
    };
    

    Podríamos declarar tres instancias de Marmotas y vincularlas de la siguiente manera:

    struct Marmot Larry = { 0, 3.4, 19.7 };
    struct Marmot Jane = { &Larry, 2.5, 13.1 };
    struct Marmot Felix = { &Jane, 2.9, 15.6 };
    

    Félix está en la parte superior de la lista, mientras que Larry está en la parte inferior. Tenga en cuenta que los elementos deben declararse en orden inverso ya que la referencia del puntero debe existir antes de la asignación (es decir, Jane debe existir para que Felix use la dirección de Jane). Es común también declarar un puntero para usar como jefe de la lista. Por lo tanto, también podríamos agregar:

    struct Marmot *MarmotList = &Felix;
    

    Por lo tanto, son ciertos los siguientes:

    • Felix.nextMarmot señala a Jane
    • Marmotlist->NextMarmot señala a Jane
    • Jane.nextMarmot apunta a Larry
    • Marmotlist->SiguienteMarmot->NextMarmot apunta a Larry
    • Larry.nextMarmota es 0
    • Marmotlist->SiguienteMarmot->NextMarmot->NextMarmot es 0

    La línea final de punteros a punteros no es muy práctica. Para sortear esto, podemos usar un puntero temporal. A continuación se muestra una función de ejemplo que toma como argumento la cabeza de una lista de Marmota, y luego imprime las edades de todas las Marmot s en la lista.

    void PrintMarmotAges( struct Marmot *top )
    {
        struct Marmot *temp;
    
        temp = top; /* initialize pointer to top of list */
    
        while( temp ) /* true only if marmot exists */
        {
            printf( "%f\n", temp->Age );
            temp = temp->NextMarmot );
        }
    }
    

    Se llamaría así:

    PrintMarmotAges( MarmotList );
    

    Tenga en cuenta que podríamos haber reutilizado top en lugar de usar la temperatura local en este caso. Sin embargo, si el encabezado de la lista será necesario para algo más en la función, entonces se requerirá la variable local (es decir, ya que temp = TEMP->nextMarmot efectivamente borra el valor anterior de temp, perdemos la cabeza de la lista mientras caminamos por la lista).


    This page titled 11.2: Ejemplo de lista enlazada is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.