Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.

Slides:



Advertisements
Similar presentations
Problem of the Day  At what times do the minute and hour hands on an analog clock line up?
Advertisements

Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Stacks.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Chapter 3 Introduction to Collections – Stacks Modified
CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Problem of the Day  What do you get when you cross a mountain climber and a grape?
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
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.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Week 3 - Friday CS221.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

CSC 212 – Data Structures

Using Stack

public interface Stack extends Collection { public E peek() throws EmptyCollectionException; public E pop() throws EmptyCollectionException; public void push(E element); }  Any type of data stored within a Stack  Generics enable us to avoid rewriting this code  Minimum set of exceptions defined by interface  Classes could throw more unchecked exceptions Stack Interface

Array-based Implementation  Array reference in a field  Another field tracks top element’s index  Stack size also: top  Add to next lowest index  Remove highest index Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top  top  1 return stack[top] 012 … ArrayStack stack top  0

Array-based Implementation  Array reference in a field  Another field tracks top element’s index  Stack size also: top  Add to next lowest index  Remove highest index 012 top … ArrayStack stack top  0 Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top  top  1 return stack[top]

top Array-based Implementation  Array reference in a field  Another field tracks top element’s index  Stack size also: top  Add to next lowest index  Remove highest index ArrayStack stack top  X … X Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top  top  1 return stack[top]

Array-based Implementation  Array reference in a field  Another field tracks top element’s index  Stack size also: top  Add to next lowest index  Remove highest index ArrayStack stack top  X 012 … X top Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top  top  1 return stack[top]

 Could fill array with data  More push() s impossible  Throw exception on error  Specific to array-based  Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top]  elem top  top + 1 fi ArrayStack stack top  X 012 … X top

 Could fill array with data  More push() s impossible  Throw exception on error  Specific to array-based  Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top]  elem top  top + 1 fi ArrayStack stack top  X … X top

 Could fill array with data  More push() s impossible  Throw exception on error  Specific to array-based  Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top]  elem top  top + 1 fi ArrayStack stack top  length 012 … top

Oops… My Bad  Could fill array with data  More push() s impossible  Throw exception on error  Specific to array-based  Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top]  elem top  top + 1 fi ArrayStack stack top  length 012 … top

Simple Array-based Stack Why It RocksWhy It Sucks  Easy to write & read  Simple to find bugs  Quick running times  Methods take O(1) time huge  Array must be huge  Max. possible elements  Problems occur when too many exist at once  When full, throws specific exception

Better Approach (Maybe?)  Implement with array, but allow resizing array  Grow as needed rather that stating full  (In Java) Requires allocating new, larger array  Copy data into new array from current array  Reassign field so that it now aliases larger array  Arrays.copyOf() does everything in single call

Better Approach (Maybe?)  Implement with array, but allow resizing array  Grow as needed rather that stating full  (In Java) Requires allocating new, larger array  Copy data into new array from current array  Reassign field so that it now aliases larger array  Arrays.copyOf() does everything in single call  Method call is O (1), but method takes O ( n ) time  push() now O ( n ) in worst-case, but how bad is that?

Ways to Grow Array  Two ways to increase array size  Constant value (e.g., 2, 4, 6, 8, 10…)  Constant factor (e.g., 2, 4, 8, 16, 32…)  Both approaches requires O(n) time…  Instantiating & copying array are slow steps average  …average (amortized) costs differ, however  Difference in how often slow step needed

Increase Array Size by c  To hold n elements, must grow k = n / c times  Copy array each growth, so total copies is: 1 + (c+1) + + (((k-1)*c)+1) + (k*c+1) = 1 + (k*c+1) + (c+1) + (((k-1)*c)+1) + = ((k * c) + 2) + ((k * c) + 2) + = k / 2 * ((k * c) + 2) = O(c*k 2 )= O(c * ( n / c ) 2 ) = O(n 2 * 1 / c ) = O(n 2 )  Averaged over adding n elements = O(n) each!

Grow by Constant Factor  To hold n elements, need to grow k = log n times  Still copy array each time, so will make this many copies: k k = (2 k - 1) + 2 k = 2 k = (2 * 2 k ) - 1 = (2 * 2 log n ) - 1 = O(2n - 1) = O(n)  Average cost is O(1) ( O(n) copies ÷ n elements)

Your Turn  Get into your groups and complete activity

For Next Lecture  Read 4.1 – 4.3 before Wednesday's class  Can we do better than resizable arrays? GENIUS  Didn’t some GENIUS find too much memory expensive?  How could we grow & shrink memory demands?  Is linked list an ADT? Why or why not?  Week #7 weekly assignment due Wednesday  Programming Assignment #1 due in 2 weeks  Start before it gets too late! Planning saves time!