Estructuras con punteros: Nodos y Listas
Fichas ordenas en orden alfabético y por otro criterio, usando hilos : hilo de inicio : hilo de ficha : hilo final ficha + hilo que sale de ella = nodo hilo naranjo = puntero inicio hilo verde = puntero de nodo que apunta a “nada”
usando hilos, equivalencia en Java : hilo de inicio struct Nodo * inicio; : hilo de ficha struct Nodo { int x; struct Nodo * siguiente; } : hilo final es el puntero de un objeto que tiene asignado el valor null apunta a null: struct Nodo * x = new Nodo(…); x->siguiente = null;
Un programa C
Creación de “tipo” estructura struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } x p Valor entero Dirección a otra variable de misma estructura
Creación de una variable x p struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ?
Creación de un puntero b ? x p struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ?
Puntero apuntando a “nada” conocida b NULL x p struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ? b ?
Asignar un valor a un campo b NULL xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ? 55
puntero apuntando a variable NULL xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ? 55 b
Cambiando valor por medio de puntero xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ? 55 b 66
¿Puntero en “a” apuntar a algo? xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; } a x p ? 66 b Se puede hacer apuntar a otra variable del mismo tipo o a “nada” (NULL) La gracia es hacerlo sin usar el nombre de la variable “a” sino el puntero “p”
Creando otra variable estructurada xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; …… a x p ? 66 b d x p ? struct str d; }
Conectando los nodos xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; …… a x p ? 66 b d x p ? struct str d; a.p=&d; }
Cerrando la lista xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; …… a x p ? 66 b d x p ? struct str d; a.p=&d; d.p=NULL; } NULL
Dandole valor a x de nuevo nodo xp struct str { int x; struct str *p; }; main(){ struct str a; struct str *b; b=NULL; a.x=55; b=&a; b->x=66; …… a x p ? 66 b d x p struct str d; a.p=&d; d.p=NULL; d.x=77; } NULL 77