16 dynamic data structures
play

16. Dynamic Data Structures A data structure is a particular way of - PowerPoint PPT Presentation

Data Structures 16. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue 395 396 Examples using a Stack Motivation:


  1. Data Structures 16. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue 395 396 Examples using a Stack Motivation: Stack Browsing Websites (back button) Undo function in a text-editor Calculator (using Suffix-notation) 2 * 5 + 3 3 5 2 * + = 3 + (5 * 2) = 13 Suitable for introduction in a lecture like this 397 398

  2. Stack Operations ( push, pop, top, empty ) We Need a new Kind of Container Up to this point: container = Array ( T[] ) 4 3 3 3 1 Contiguous area of memory, random access (to i th element) 5 5 5 5 5 push(4) pop() pop() push(1) Simulation of a stack with an array? 1 1 1 1 1 No, at some time the array will become “full”. 2 2 2 2 2 3 3 top Goal: we implement a stack 5 5 top() → 3 empty() → false class 1 1 1 5 6 3 8 9 3 3 8 9 3 Question: how do we create 2 2 space on the stack when push is not possible to execute push(3) here! called? 399 400 Arrays are no All-Rounders... Arrays are no All-Rounders... It is expensive to insert or delete elements “in the middle ”. It is expensive to insert or delete elements “in the middle ”. 1 5 6 3 8 9 3 3 8 9 1 5 6 3 8 8 9 3 3 8 9 8 If we want to insert, If we want to remove this we have to move ev- element, we have to move erything to the right everything to the right. (if at all there is enough space!) 401 402

  3. The new Container: Linked List Linked List: Zoom No contiguous area of memory and no random ListNode 1 5 6 access null Each element “knows” its successor next (type ListNode ) value (type int ) Insertion and deletion of arbitrary elements is simple, even at the beginning of the list class ListNode { ⇒ A stack can be implemented as linked list int value; ListNode next; 1 5 6 3 8 8 9 ListNode (int value, ListNode next){ reference this.value = value; this.next = next; } 403 404 } Abstract Data Types Stack = Reference to Top Element A stack is an abstract data type (ADT) with operations Stack s.push(x) : Puts element x on the stack s . 1 5 6 null top_node s.pop() : Removes and returns top most element of s or null (or error message) s.top() : Returns top most element of s or null (or error public class Stack { message). private ListNode top_node; s.empty() : Returns true if stack is empty, false otherwise. public void push (int value) {...} public int pop() {...} new Stack() : Returns an empty stack. public int top() {...} public boolean empty {...} }; 405 406

  4. Implementation push Implementation push in Java public class Stack{ top null x n x n − 1 x 1 private ListNode top_node; ... public void push (int value){ x top_node = new ListNode (value, top_node); push(x) : } 1. Create new list element with x and pointer to the value of } top . push(4); 2. Assign the node with x to top . top_node 4 1 5 6 407 408 Implementation empty in Java Implementation pop top null x n x n − 1 x 1 public class Stack{ private ListNode top_node; ... r s.pop () : public boolean empty(){ return top_node == null; 1. If top=null , then return null , or emit error message } 2. otherwise memorize pointer p of top in auxiliary variable } r . 3. Set top to p.next and return r 409 410

  5. Implementation pop in Java Another Example: Sorted Linked List public int pop() { Required Functionality: assert (!empty()); (Sorted) Output ListNode p = top_node; top_node = top_node.next; Add a value return p.value; (Search for a value) } Remove a value top_node p 1 5 6 411 412 Goal ListNode class ListNode{ public class SortedList{ int value; ListNode head = null; ListNode next; // insert value in a sorted way ListNode (int value, ListNode next){ public void insert(int value){ ... } this.value = value; this.next = next; // remove value if in list, return if value was found in list } public boolean remove(int value){ ... } } null // output list values element by element 3 7 13 22 public void output(){ ... } } n unreachable from n 413 414

  6. Invariants output public class SortedList{ ListNode head = null; null 3 7 13 22 ... // output list values element by element, starting from head n public void output(){ ListNode n = head; For a reference n to a node in a sorted list it holds that while (n != null){ either n = null , Out.print(n.value + " -> "); or n.next = null , n = n.next; } or n.next � = null and n.value ≤ n.next.value . Out.println("NIL"); } } 415 416 Invariants: Insertion of x Insertion // insert value in a sorted way (sorted increasingly by value) (a) List is empty or public void insert(int value){ if (head == null || value <= head.value){ // (a) or (b) (b) x ≤ n.value for all nodes n head = new ListNode(value, head); (c) x > n.value for all nodes n } else { // (c), (d) (d) There is a node n with successor m , such that ListNode n = head; x > n.value and x ≤ m.value ListNode prev = null; while (n != null && value > n.value){ prev = n; Development of the following code live in the lecture n = n.next; } prev.next = new ListNode(value, n); } 417 418 }

  7. Combine Invariants: Deletion of x // insert value in a sorted way (sorted increasingly by value) public void insert(int value){ ListNode n = head; ListNode prev = null; (a) x is not contained while (n != null && value > n.value){ (b) x is the first element (head) prev = n; n = n.next; (c) x has a predecessor } if (prev == null){ head = new ListNode(value, n); } else { prev.setNext(new ListNode(value,n)); } } 419 420 Removal Queue (FIFO) public boolean remove(int value){ A queue is an ADT with the following operations ListNode n = head; ListNode prev = null; q.enqueue(x) : adds x to the tail (=end) of the queue q . while (n != null && value != n.value) { q.dequeue() : removes x from the head of the queue and prev = n; n = n.next; returns x , null (or error message) otherwise } if (n == null) { // (a) q.empty() : return true if the queue is empty, otherwise return false; false } else if (prev == null){ // (b) F irst I n F irst O ut: Elements inserted first will be extracted first. head = head.next; } else { // (c) (implementation in the exercises) prev.setNext(n.next); } return true; 421 422 }

Recommend


More recommend