Problem of the Day  What do you get when you cross a mountain climber and a grape?

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Problem of the Day  At what times do the minute and hour hands on an analog clock line up?
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 
CS Data Structures II Review COSC 2006 April 14, 2017
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
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.
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. 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.
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.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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?
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
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.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
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.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
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.
Linked Structures, LinkedStack
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.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
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.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Parasol Lab, Dept. CSE, Texas A&M University
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
Stack: a Linked Implementation
Elementary Data Structures
CSCI 3333 Data Structures Stacks.
Week 3 - Friday CS221.
Stacks.
Stacks.
Stacks.
Presentation transcript:

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?  Nothing, you cannot cross a scalar.

CSC 212 – Data Structures

Using Stack

public interface Stack extends Collection { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; 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 + 1  Add to next lowest index  Remove highest index Algorithm isEmpty() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top  top  1 return S[top + 1] 012 … ArrayStack S top  -1

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

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

Array-based Implementation  Array reference in a field  Another field tracks top element’s index  Stack size also: top + 1  Add to next lowest index  Remove highest index Algorithm isEmpty() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top  top  1 return S[top + 1] ArrayStack S top  (X -1) 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 == S.length  1 then throw new FullStackException else top  top + 1 S[top]  elem fi ArrayStack S top  (X -1) 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 == S.length  1 then throw new FullStackException else top  top + 1 S[top]  elem fi ArrayStack S 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 == S.length  1 then throw new FullStackException else top  top + 1 S[top]  elem fi ArrayStack S top  S.length … 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 == S.length  1 then throw new FullStackException else top  top + 1 S[top]  elem fi ArrayStack S top  S.length … top

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 Stack using linked list  Grows & shrinks as elements added & removed  Add element in push() by allocating a Node  pop() removes Node s from linked list in  Concerns about size limits are forgotten

Once You pop() …  Check for empty Stack  If it is, throw exception  Pop the top Node  Node ’s element saved  top moved to next Node  Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal  top.getElement() top  top.getNext() size  size - 1 fi return retVal NodeStack size  6 top

 Once You pop() …  Check for empty Stack  If it is, throw exception  Pop the top Node  Node ’s element saved  top moved to next Node retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal  top.getElement() top  top.getNext() size  size - 1 fi return retVal NodeStack size  6 top

 Once You pop() …  Check for empty Stack  If it is, throw exception  Pop the top Node  Node ’s element saved  top moved to next Node retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal  top.getElement() top  top.getNext() size  size - 1 fi return retVal NodeStack size  6 top

 Once You pop() …  Check for empty Stack  If it is, throw exception  Pop the top Node  Node ’s element saved  top moved to next Node retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal  top.getElement() top  top.getNext() size  size - 1 fi return retVal NodeStack size  5 top

 Linked list-based Stack  push ing very easy, too  Adds new top node  Easy to check if empty  Simplified w/o sentinel Algorithm push(e) newN  new Node(e, top) top  newN size  size + 1 Algorithm isEmpty() return top == null NodeStack size  5 top

 Linked list-based Stack  push ing very easy, too  Adds new top node  Easy to check if empty  Simplified w/o sentinel Algorithm push(e) newN  new Node(e, top) top  newN size  size + 1 Algorithm isEmpty() return top == null e NodeStack size  5 top

 Linked list-based Stack  push ing very easy, too  Adds new top node  Easy to check if empty  Simplified w/o sentinel Algorithm push(e) newN  new Node(e, top) top  newN size  size + 1 Algorithm isEmpty() return top == null newN NodeStack size  5 top e

 Linked list-based Stack  push ing very easy, too  Adds new top node  Easy to check if empty  Simplified w/o sentinel Algorithm push(e) newN  new Node(e, top) top  newN size  size + 1 Algorithm isEmpty() return top == null newN NodeStack size  5 top e

 Linked list-based Stack  push ing very easy, too  Adds new top node  Easy to check if empty  Simplified w/o sentinel Algorithm push(e) newN  new Node(e, top) top  newN size  size + 1 Algorithm isEmpty() return top == null NodeStack size  6 top

Your Turn  Get into your groups and complete activity

For Next Lecture  Read GT section 5.2 before Monday's class  Discusses design of the Queue ADT  Array-based implementation of Queue presented  Queue implementation of linked-list also shown  Week #8 weekly assignment due Tuesday  Programming Assignment #1 due next week