Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Listas
Listas
(Listas Encadenadas)
Listas encadenadas
Lista de Elementos Estructura de datos dinmica Compuesta por nodos:
Los datos/elementos se encuentran dentro de los nodos Cada nodo esta encadenado al siguiente (o al anterior) Dentro de la lista, solamente se guarda la referencia al primer nodo.
dato1
dato2
dato3
dato4
dato1
dato2
dato3
dato4
Implementando en Java:
Lista Simple: - Clase SNode representa los nodos de la
lista - Clase SList que representa una lista simplemente encadenada, y es en donde se guarda la referencia al primer nodo
Clase SNode
Campos Dato Referencia al siguiente nodo
Clase SNode
public class SNode { Object dato; SNode next;
Clase SNode
Campos
Dato Referencia al siguiente nodo
Constructores
Dato
Dato
nodo
new SNode(dato)
new SNode(dato,nodo)
Clase SNode
public class SNode { Object dato; SNode next; public SNode(Object d) { this.dato = d; this.next = null; } public SNode(Object d, SNode n) { this(d); this.next = n; } }
Clase SNode
SNode n1 = new SNode(new Integer(1));
n1
Clase SNode
SNode n1 = new SNode(new Integer(1));
n1
Clase SNode
n1.next = n2 n1 n2
Clase SNode
n1.next = n2 n1 n2
Clase SNode
SNode l = new SNode(new Integer(1)); l.next = new SNode(new Integer(2)); l.next.next = new SNode(new Integer(3));
Clase SNode
SNode l = new SNode(new Integer(1)); l.next = new SNode(new Integer(2)); l.next.next = new SNode(new Integer(3));
Clase SNode
SNode l = new SNode(new Integer(1)); l.next = new SNode(new Integer(2)); l.next.next = new SNode(new Integer(3));
Clase SList
Campos
Primer nodo de la lista (head) Tamao de la lista (size)
Constructor
Lista vacia
head null size 0 new SList()
Clase SList
public class SList { private SNode head; private int size;
Clase SList
Campos
Primer nodo de la lista (head) Tamao de la lista (size)
Constructor
Lista vacia
head null size 0 new SList()
Clase SList
public class SList { private SNode head; private int size; public SList() { this.head = null; this.size = 0; }
Clase SList
Metodos
isEmpty () insertFront (dato) insertEnd(dato) insertNth(dato,n) deleteFront() deleteEnd() deleteNth(n) search(dato) nth(n) toString()
lista.insertFront(new Integer(1));
lista head 1
lista.insertEnd(new Integer(3));
lista head 2 1 3
lista.deleteFront();
lista head 4 1 3
lista.deleteEnd();
lista head 4
Clase SList
Metodos
isEmpty () insertFront (dato) insertEnd(dato) insertNth(dato,n) deleteFront() deleteEnd() deleteNth(n) search(dato) nth(n) toString()
Clase SList
public boolean isEmpty() { return head == null; }
Clase SList
Metodos
isEmpty () insertFront (dato) insertEnd(dato) insertNth(dato,n) deleteFront() deleteEnd() deleteNth(n) search(dato) nth(n) toString()
Clase SList
public void insertFront(Object d) { if (this.isEmpty()) { head = new SNode(d); size = 1; } else { SNode nuevo = new SNode(d,head); head = nuevo; size++; } }
Clase SList
Metodos
isEmpty () insertFront (dato) insertEnd(dato) insertNth(dato,n) deleteFront() deleteEnd() deleteNth(n) search(dato) nth(n) toString()
Clase SList
public void insertEnd(Object d) { if (this.isEmpty()) { head = new SNode(d); size = 1; } else { SNode aux = head; while (aux.next != null) aux = aux.next; aux.next = new SNode(d); size++; } }