Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Similar presentations


Presentation on theme: "CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia."— Presentation transcript:

1 CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at http://peerinstruction4cs.org. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

2 Today’s Topics 1. Adapter design pattern vs inheritance design pattern 2. Stacks 2

3 Reading quiz!

4  We define the new class C, declared to implement interface I, to include an instance of type A (composition design pattern).  QUESTION:  A private instance variable from A can be accessed directly and used to represent the corresponding variable in C A. TRUE B. FALSE

5 Reading Quiz!  A stack is a Last-In-First-Out (LIFO) data structure. Your task is to implement a Stack directly by using a singly linked list (a class that implements List only).  QUESTION:  Is it more efficient to use (A) the end /or/ (B) the front of the linked list as the top of the stack?

6 Design Patterns

7 Inheritance Design Pattern  Problem: We need to create a class C that implements interface I  We know of an existing class B that offers some of the functionality required by I  And this functionality in B corresponds exactly to a subset of the API of I  Solution: define the new class C to extend the existing class B  C “is a” B public class C extends B implements I { …

8 Adapter Design Pattern  Problem: We need to create a class C that implements interface I  Identify an existing class A that has attributes and behavior similar to those required by I  A is not an exact subset of I’s requirements  Define a class named, say, C, declared to implement I, which includes an instance variable of type A  C “has a” A public class C implements I { private A myA; …

9 Adapter Design Pattern  So each instance of C includes an instance of A : every C has-a A  This is called containment or composition  The methods in C are defined to call methods in A to do (some of) their work  This is called message forwarding or delegation

10 Stack Using ArrayList

11 What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List…  If List is implemented using an array? Head: ________ Tail: _________ A. O(1), O(1) B. O(1), O(n) C. O(n), O(n) D. O(n 2 ), O(n 2 ) E. Other/none/more

12 What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List…  If List is implemented using a singly linked list? Head: ________ Tail: _________ A. O(1), O(1) B. O(1), O(n) C. O(n), O(n) D. O(n 2 ), O(n 2 ) E. Other/none/more

13 What attribute of List corresponds to Stack.top?  What is the time cost of adding or removing an element at the head or at the tail of an N- element List…  If List is implemented using a doubly linked list with tail pointer? Head: ________ Tail: _________ A. O(1), O(1) B. O(1), O(n) C. O(n), O(n) D. O(n 2 ), O(n 2 ) E. Other/none/more

14 Map Stack Attributes to ArrayList Attributes and/or Methods Stack Attribute ArrayList Equivalent top size() – 1 size size() Don’t underestimate the importance of doing this mapping first. Planning now saves time later

15 Map Stack Methods to List Methods  A consequence of that attribute mapping is that a push operation results in adding to the tail of the List tail of list next position beyond tail location to “push” the new stack element size() – 1 + 1 = size() Stack operation List operation equivalent push( element )add( size(), element ) E pop()E remove( size() - 1 ) E peek()E get( size() – 1 ) int size() boolean isEmpty()

16 What does the implementation look like? 1 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.EmptyStackException; 6 7 /** 8 * An implementation of the Stack interface that adapts 9 * a java.util.List 10 */ 11 public class ListStack implements Stack { 12 private java.util.List stack; 13 // the top element of stack is stored at position 14 // s.size() - 1 in the list. 15 16 /** 17 * Create an empty stack. 18 */ 19 public ListStack() { 20 stack = new ArrayList (); 21 } 22 The contained instance of the adapted class

17 23 /** 24 * Determine if the stack is empty. 25 * @return true if the stack is empty, 26 * otherwise return false. 27 */ 28 public boolean isEmpty() 29 { 30 return stack.isEmpty(); 31 } 32 33 /** 34 * Return the top element of the stack without removing it. 35 * This operation does not modify the stack. 36 * @return topmost element of the stack. 37 * @throws EmptyStackException if the stack is empty. 38 */ 39 public E peek() 40 { 41 if ( stack.isEmpty() ) 42 throw new EmptyStackException(); 43 return stack.get( stack.size() - 1 ) ; 44 } message forwarding – let the List object do as much work as possible Having done the attribute mapping, this is easy to figure out What does the implementation look like?

18 46 /** 47 * Pop the top element from the stack and return it. 48 * @return topmost element of the stack. 49 * @throws EmptyStackException if the stack is empty. 50 */ 51 public E pop() 52 { 53 if ( stack.isEmpty() ) 54 throw new EmptyStackException(); 55 return stack.remove( stack.size() - 1 ); 56 } 57 58 /** 59 * Push element on top of the stack. 60 * @param element the element to be pushed on the stack. 61 */ 62 public void push( E element) 63 { 64 stack.add( stack.size(), element ); 65 } Compare with peek() List.size() -1 is top, so “push” new element at List.size() What does the implementation look like?

19 Stack Using LinkedList

20 Single or Double?  To implement the Stack interface with linked list, we should use: A. Singly-linked list with head pointer only B. Singly-linked list with head + tail pointers C. Doubly-linked list with head pointer only D. Doubly-linked list with head+ tail pointers E. Other/none/more than one

21 To implement Stack interface with linked list, we should use:  Singly-linked list with head pointer only  We will use the head of the list as the top of the stack  All operations can happen there directly  Stack only needs to access one end

22 Map Stack ADT attributes to a linked list implementation public class LinkedStack implements Stack { private int size; private SLNode top; SLNode element successor LinkedStack size LinkedStack top 0

23 Map Stack ADT attributes to a linked list implementation public class LinkedStack implements Stack { private int size; private SLNode top; SLNode element successor LinkedStack size LinkedStack top 0  This Stack with Linked List is using the Adapter design pattern, because it contains a link list object rather than inherits one by extending a linked list. A. TRUE B. FALSE

24 The push() operation 1 public void push( E element ) { 2 3 SLNode newNode = 4 new SLNode (element, 5 top.getSuccessor()); (a, b) 6 7 top.setSuccessor( newNode ); (c) 8 9 size++; (d) 10 } 1. create a new SLNode (a) 2. Set the new node’s successor field to be the same as top’s next field (b) 3. Set top’s successor field to reference the new node. (c) 4. Increment size by 1 (d) Steps (a) and (b)Step (c) Pushing an element onto an empty stack

25 08-25/32 The push() operation 1 public void push( E element ) { 2 3 SLNode newNode = 4 new SLNode (element, 5 top.getSuccessor()); (a, b) 6 7 top.setSuccessor( newNode ); (c) 8 9 size++; (d) 10 } 1. create a new SLNode (a) 2. Set the new node’s successor field to be the same as top’s next field (b) 3. Set top’s successor field to reference the new node. (c) 4. Increment size by 1 (d) Steps (a) and (b)Step (c) Pushing an element onto a non- empty stack

26 Stack Operation Costs Stack Operation ArrayList Operation Array List Cost push( element )add( size(), element )Ο(1) pop()remove( size() – 1 )Ο(1) peek()get( size() – 1 )Ο(1) What is the cost of Stack operations for the direct implementation using a singly linked list ? A.push=O(1), pop=O(1), peek=O(1) B.push=O(1), pop=O(n), peek=O(n) C.push=O(n), pop=O(1), peek=O(1) D.push=O(n), pop=O(n), peek=O(n) E.Other/none/more


Download ppt "CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia."

Similar presentations


Ads by Google