A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

Apresentações semelhantes


Apresentação em tema: "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"— Transcrição da apresentação:

1 Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

2 Programação II – Prof. Mateus Raeder Listas Duplamente Encadeadas Cada nó possui dois ponteiros: Vantagem: simplifica certas operações e permite percorrer a lista nos dois sentidos. Desvantagem: gasta mais espaço do que a simplesmente encadeada (mais um ponteiro em cada nó) e pode tornar mais complexas certas operações. previousNode data nextNode nónó

3 Programação II – Prof. Mateus Raeder Lista encadeada com referência ao ultimo elemento da lista Apontador para o primeiro nó da lista firstNode Apontador para o último nó da lista lastNode null final de lista null início de lista

4 Programação II – Prof. Mateus Raeder Classe Node public class Node { private String data; private Node nextNode; private Node previousNode; public Node( String element ) { this( element, null ); } public Node( String element, Node node ){ data = element; nextNode = node; } public String getData() { return data; } public void setData (String element) { data = element; }

5 Programação II – Prof. Mateus Raeder Classe Node public Node getNext() { return nextNode; } public void setNext(Node n) { nextNode = n; } public Node getPrevious() { return previousNode; } public void setPrevious(Node n) { previousNode = n; }

6 Programação II – Prof. Mateus Raeder Class List (1/6) public class List { private Node firstNode ; private Node lastNode ; private String name ; public List() { this("list"); } public List (String listName) { name = listName; firstNode = lastNode = null; } public String getFirst () throws UnderflowException { if (isEmpty()) throw new UnderflowException(); return firstNode.getData(); } public String getLast () throws UnderflowException { if (isEmpty()) throw new UnderflowException(); return lastNode.getData(); } public boolean isEmpty () { return firstNode == null; }

7 Programação II – Prof. Mateus Raeder Class List (2/6) public void insertAtFront (String insertItem) { Node n = new Node(insertItem); if (isEmpty()) { firstNode = lastNode = n; } else { firstNode.setPrevious(n); n.setNext(firstNode); firstNode = n; } firstNode b lastNode cdea n

8 Programação II – Prof. Mateus Raeder Class List (3/6) public void insertAtBack (String insertItem) { Node n = new Node(insertItem); if (isEmpty()) { firstNode = lastNode = n; } else { lastNode.setNext(n); n.setPrevious(lastNode); lastNode = n; } firstNode b lastNode cdex n

9 Programação II – Prof. Mateus Raeder Class List (4/6) public String removeFromFront () throws UnderflowException { if (isEmpty()) { throw new UnderflowException(); } String removedItem = firstNode.getData(); if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.getNext(); firstNode.setPrevious(null); } return removedItem; } firstNode b lastNode cde

10 Programação II – Prof. Mateus Raeder Class List (5/6) public String removeFromBack () throws UnderflowException { if (isEmpty()) { throw new UnderflowException(); } String removedItem = lastNode.getData(); if (firstNode == lastNode) { firstNode = lastNode = null; } else { Node penultimo = lastNode.getPrevious(); lastNode = penultimo; lastNode.setNext(null); } return removedItem; } firstNode b lastNode cde penultimo

11 Programação II – Prof. Mateus Raeder Class List (6/6) public void print () { if (isEmpty()) { System.out.println("Empty " + name); } else { System.out.print("The " + name + " is: "); Node current = firstNode; while (current != null) { System.out.print(current.getData().toString() + " "); current = current.getNext(); } System.out.println("\n"); } firstNode 2 lastNode 345 current =null

12 Programação II – Prof. Mateus Raeder Testando a lista public class ListTest { public static void main( String args[] ) { List list = new List( ); list.insertAtBack("1"); list.insertAtBack("2"); list.insertAtBack("3"); list.insertAtBack("4"); list.print(); try { Object removedEl = list.removeFromFront(); System.out.println( removedEl.toString() + " removed" ); removedEl = list.removeFromFront(); System.out.println( removedEl.toString() + " removed" ); removedEl = list.removeFromBack(); System.out.println( removedEl.toString() + " removed" ); removedEl = list.removeFromBack(); System.out.println( removedEl.toString() + " removed" ); } catch ( UnderflowException e ) { e.printStackTrace(); }

13 Programação II – Prof. Mateus Raeder Exercício 1) Na classe List (implementação de lista duplamente encadeada), implementar um método com a seguinte assinatura: –“public boolean insertBefore (String val1, String val2)”. –Este método deve inserir na lista um nó com dado val2 na posição anterior ao nó que contenha o dado val1. 2) Implemente na classe List um método que insere um nó na k-ésima posição (firstNode é a posição 0) 3) Implemente um método que remova um nó que contém determinada informação (String o, por ex.)

14 Programação II – Prof. Mateus Raeder Resposta 1 public boolean insertBefore(String obj1, String obj2){ Node current = firstNode; if(firstNode.getData() == obj1){ insertAtFront(obj2); return true; } while (current != null){ if(current.getData() == obj1){ Node novo = new Node(obj2, current); novo.setPrevious(current.getPrevious()); current.setPrevious(novo); novo.getPrevious().setNext(novo); return true; } current = current.getNext(); } return false; }

15 Programação II – Prof. Mateus Raeder Resposta 2 public boolean insereKesimo(String obj1, int pos){ Node current = firstNode; int posAtual = 0; if(pos >= 0){ while (current != null){ if(posAtual == pos){ Node novo = new Node(obj1, current); novo.setPrevious(current.getPrevious()); current.setPrevious(novo); novo.getPrevious().setNext(novo); return true; } posAtual++; current = current.getNext(); } return false; }

16 Programação II – Prof. Mateus Raeder Resposta 3 public boolean removeObj(String obj1){ Node current = firstNode; while (current != null){ if(current.getData() == obj1){ current.getPrevious().setNext(current.getNext()); current.getNext().setPrevious(current.getPrevious()); return true; } current = current.getNext(); } return false; }


Carregar ppt "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"

Apresentações semelhantes


Anúncios Google