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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
COMP 103 Linked Stack and Linked Queue.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
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.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
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.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Computer Science 209 Software Development Inheritance and Composition.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Click to edit Master text styles Stacks Data Structure.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Marcus Biel, Software Craftsman
Chapter 6: The Stack Abstract Data Type
Chapter 6: The Stack Abstract Data Type
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CSE 12 – Basic Data Structures
More Data Structures (Part 1)
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
slides created by Alyssa Harding
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

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 Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

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

Reading quiz!

 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

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?

Design Patterns

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 { …

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; …

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

Stack Using ArrayList

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

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

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

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

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() – = 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()

What does the implementation look like? 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 /** 17 * Create an empty stack. 18 */ 19 public ListStack() { 20 stack = new ArrayList (); 21 } 22 The contained instance of the adapted class

23 /** 24 * Determine if the stack is empty. 25 true if the stack is empty, 26 * otherwise return false. 27 */ 28 public boolean isEmpty() 29 { 30 return stack.isEmpty(); 31 } /** 34 * Return the top element of the stack without removing it. 35 * This operation does not modify the stack. 36 topmost element of the stack. 37 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?

46 /** 47 * Pop the top element from the stack and return it. 48 topmost element of the stack. 49 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 } /** 59 * Push element on top of the stack. 60 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?

Stack Using LinkedList

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

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

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

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

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

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

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