Está en la página 1de 31

Linked List

(Listas Encadenadas)

Ing. Andrea Quan

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.

Listas simplemente encadenadas


Nodo simple guarda: - dato - referencia al siguiente nodo (next)
lista head

dato1

dato2

dato3

dato4

Listas doblemente encadenadas


Nodo doble guarda: - dato - referencia al siguiente nodo (next) - referencia a nodo anterior (previous)
lista head tail

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

SNode n2 = new SNode(new Integer(2));


n1 n2

Clase SNode
n1.next = n2 n1 n2

Clase SNode
n1.next = n2 n1 n2

SNode n3 = new SNode(new Integer(3),n1); n3 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()

Listas simplemente encadenadas


SList lista = new SList();
lista head

lista.insertFront(new Integer(1));
lista head 1

Listas simplemente encadenadas


lista.insertFront(new Integer(2));
lista head 2 1

lista.insertEnd(new Integer(3));
lista head 2 1 3

Listas simplemente encadenadas


lista.insertNth(new Integer(4),2);
lista head 2 4 1 3

lista.deleteFront();
lista head 4 1 3

Listas simplemente encadenadas


lista.deleteNth(2);
lista head 4 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++; } }

También podría gustarte