systems programming
play

Systems Programming Linked lists, stacks and queues Julio Villena - PowerPoint PPT Presentation

Systems Programming Linked lists, stacks and queues Julio Villena Romn (L ECTURER ) <jvillena@it.uc3m.es> C ONTENTS ARE MOSTLY BASED ON THE WORK BY : Carlos Delgado Kloos and Jess Arias Fisteus Systems Programming Linked lists Julio


  1. Array-based implementation … public T pop() throws StackEmptyException { T o; if (top == -1) throw new EmptyStackException(); o = data[top]; data[top--] = null; return o; } } 59

  2. Implementation based on linked lists top Madrid Miami Munich 60

  3. Implementation based on linked lists public class Node<T> { private T info; private Node<T> next; public Node(T info, Node next) { this.info = info; this.next = next; } void setInfo(T info) {this.info = info;} void setNext(Node<T> next) {this.next = next;} T getInfo() {return info;} Node<T> getNext() {return next;} } 61

  4. Implementation based on linked lists public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int size; public LinkedStack() { top = null; size = 0; } … 62

  5. Implementation based on linked lists … public int size() { return size; } public boolean isEmpty() { return (top == null); } public T top() throws EmptyStackException { if (top == null) throw new EmptyStackException(); return top.getInfo(); } … 63

  6. Insertion ( push ) top Madrid Miami Munich Moscow 64

  7. Implementation based on linked lists … public void push(T info) { Node<T> n = new Node<T>(info, top); top = n; size++; } … 65

  8. Extraction ( pop ) top Moscow Madrid Miami Munich 66

  9. Implementation based on linked lists … public T pop() throws EmptyStackException { T o; if (isEmpty()) throw new EmptyStackException(); o = top.getInfo(); top = top.getNext(); size--; return o; } } 67

  10. Systems Programming Queues Julio Villena Román (L ECTURER ) <jvillena@it.uc3m.es> C ONTENTS ARE MOSTLY BASED ON THE WORK BY : Carlos Delgado Kloos 68

  11. Example • The queue at the bus stop • The printer queue 69

  12. Characteristics • Linear structure • Access on one end for insertion and on the other for extraction 70

  13. Queue interface public interface Queue<T> { public int size(); public boolean isEmpty(); public void enqueue(T o) throws QueueOverflowException; public T dequeue() throws EmptyQueueException; public T front() throws EmptyQueueException; } 71

  14. One interface and several implementations Queue ArrayQueue LinkedQueue 72

  15. Array-based implementation top tail 1 2 3 4 5 N-1 0 1 2 3 4 5 top tail 8 9 5 6 7 N-1 0 1 2 3 4 5 73

  16. Implementation based on linked lists top tail Madrid Miami Munich 74

  17. Implementation based on linked lists public class LinkedQueue<T> implements Queue<T> { private Node<T> top = null; private Node<T> tail = null; private int size = 0; … } cdk@it.uc3m.es Java: Queues / 75 75

  18. Insertion ( enqueue ) top tail n Madrid Miami Munich Moscow 76

  19. Implementation based on linked lists public void enqueue(T info) { Node<T> n = new Node<T>(info, null); if (top == null) top = n; else tail.setNext(n); tail = n; size++; } 77

  20. Extraction ( dequeue ) top tail Madrid Miami Munich Moscow 78

  21. Implementation based on linked lists public T dequeue() throws EmptyQueueException { T o; if (top == null) throw new EmptyQueueException(); o = top.getInfo(); top = top.getNext(); if (top == null) tail = null; size--; return o; } 79

  22. Activity • View queue animations: http://courses.cs.vt.edu/csonline/DataStructures/L essons/QueuesImplementationView/applet.html 80

  23. Activity • Try the applet DepthBreadth.java that can be found here: http://www.faqs.org/docs/javap/c11/s3.html 81

  24. Other kinds of queues (not queues any more) • Double-ended queues • Priority queues cdk@it.uc3m.es 82

  25. Deques (Double-ended queues) first last removeLast insertFirst removeFirst insertLast 83

  26. Interface for deques public interface Deque<T> { public int size(); public boolean isEmpty(); public void insertFirst(T info); public void insertLast(T info); public T removeFirst() throws EmptyDequeException; public T removeLast() throws EmptyDequeException; public T first() throws EmptyDequeException; public T last() throws EmptyDequeException; } 84

  27. Stacks and queues as deques Stack Deque Queue Deque size() size() size() size() isEmpty() isEmpty() isEmpty() isEmpty() top() last() front() first() push(o) insertLast(o) enqueue(o) insertLast(o) pop() removeLast() dequeue() removeFirst() 85

  28. Definition of stacks from deques public class DequeStack<T> implements Stack<T> { private Deque<T> deque; public DequeStack() { deque = new Deque<T>(); } public int size() { return deque.size(); } public boolean isEmpty() { return deque.isEmpty(); } 86

  29. Definition of stacks from deques public void push(T info) { deque.insertLast(info); } public T pop() throws EmptyStackException { try { return deque.removeLast(); } catch (EmptyDequeException e) { throw new EmptyStackException(); } } 87

  30. Definition of stacks from deques public T top() throws EmptyStackException { try { return deque.last(); } catch (EmptyDequeException e) { throw new EmptyStackException(); } } 88

  31. Implementation of deques based on lists • Singly-linked lists are not appropriate because removeLast requires the whole list to be traversed, in order to get the reference of the last-but-one node • Solution: doubly-linked lists 89

  32. Doubly Linked Lists • Linked lists in which each node has an additional reference pointing to the previous node in the list – Can be traversed both from the beginning to the end and vice-versa – removeLast does not need the whole list to be traversed tail top info info info prev prev prev null next next next null 90

  33. The DLNode class Public class DLNode<T> { private T info; private DLNode<T> next; private DLNode<T> prev; public DLNode(T info ) {…} public DLNode(T info, DLNode prev, DLNode next ) {…} public DLNode<T> getNext () {…} public void setNext(DLNode<T> next ) {…} public DLNode<T> getPrev () {…} public void setPrev(DLNode<T> prev ) {…} public T getInfo () {…} public void setInfo(T info ) {…} } 91

  34. Inserting a node prev top tail null null node null null DLNode<T> node = new DLNode<T>(data); 92

  35. Inserting a node prev top tail null null node node.setPrev(prev); if (prev != null) { node.setNext(prev.getNext()); prev.setNext(node); } 93

  36. Inserting a node prev top tail null null node if (prev == null) { node.setNext(top); top = node; } if (node.getNext() != null) { node.getNext().setPrev(node); } else { tail = node; } 94

  37. Inserting a node /** * Inserts ‘data’ after the ‘ prev ’ node. If ‘ prev ’ * is null , ‘data’ is inserted at the first position */ public void insert(DLNode prev, T data) { DLNode<T> node = new DLNode<T>(data); node.setPrev(prev); if (prev != null) { node.setNext(prev.getNext()); prev.setNext(node); } else { node.setNext(top); top = node; } if (node.getNext() != null) { node.getNext().setPrev(node); } else { tail = node; } } 95

  38. Removing a node removed data top tail null null T data = removed.getInfo(); 96

  39. Removing a node removed data top tail null null if (removed.getPrev() != null) { removed.getPrev().setNext(removed.getNext()); } else { top = removed.getNext(); } 97

  40. Removing a node removed data top tail null null if (removed.getNext() != null) { removed.getNext().setPrev(removed.getPrev()); } else { tail = removed.getPrev(); } 98

  41. Removing a node data top tail null null 99

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend