Estructuras con punteros: Nodos y Listas
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 b x p ? struct str b; }
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 b x p ? struct str b; a.p=&b; }
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 b x p ? struct str b; a.p=&b; b.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 b x p struct str b; a.p=&b; b.p=NULL; b.x=77; } NULL 77