Está en la página 1de 16

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; x p
struct str *p;
};
main(){
struct str a;
struct str *b; Valor entero Dirección a
b=NULL; otra variable
a.x=55;
b=&a;
de misma
b->x=66; estructura
}
Creación de una variable

struct str {
int x; x p
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

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
}

a b ?
?
x p
Puntero apuntando a “nada” conocida

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
}

a b ?NULL
?
x p
Asignar un valor a un campo

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
}

a b NULL
?
55 x p
puntero apuntando a variable

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
}

a b NULL
?
55 x p
Cambiando valor por medio de puntero

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
}

b a
?
55
66 x p
¿Puntero en “a” apuntar a algo?

struct str {
int x; x p
struct str *p;
};
main(){
struct str a;
struct str *b; •Se puede hacer apuntar a otra variable
b=NULL; del mismo tipo o a “nada” (NULL)
a.x=55;
b=&a;
b->x=66;
}
• La gracia es hacerlo sin usar el
b a
?
nombre de la variable “a” sino el
66 x p puntero “p”
Creando otra variable estructurada

struct str { struct str d;


int x; x p
struct str *p; }
};
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
……

b a d
? ?
66 x p x p
Conectando los nodos

struct str { struct str d;


int x; a.p=&d; x p
struct str *p;
}; }
main(){
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
……

b a d
? ?
66 x p x p
Cerrando la lista

struct str { struct str d;


int x; a.p=&d; x p
struct str *p; d.p=NULL;
};
main(){ }
struct str a;
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
……

b a d
? ?
NULL
66 x p x p
Dandole valor a x de nuevo nodo

struct str { struct str d;


int x; a.p=&d; x p
struct str *p; d.p=NULL;
}; d.x=77;
main(){
struct str a; }
struct str *b;
b=NULL;
a.x=55;
b=&a;
b->x=66;
……

b a d
? NULL
66 x p 77 x p

También podría gustarte