Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
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.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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 CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
Objectives of these slides:
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Topic 3 The Stack ADT.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stack Applications.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Data Structures & Algorithms
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CHP-3 STACKS.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 5.
Stacks Chapter 6.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Stacks Stacks.
MEMORY REPRESENTATION OF STACKS
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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Data Structure, Reverse Polish Notation, Homework 7
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
CSC 1052 Stacks 1/11/2019.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Stacks CS-240 Dick Steflik.
5.3 Implementing a Stack Chapter 5 – The Stack.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College

Operations on a Stack Create an empty stack Is the stack empty? Add a new item to the stack. (Push) Remove the item that was most recently added (Pop) Retrieve the item that was most recently added (Peek)

Implementing a Stack All items in the stack must be stored Only the top item needs to be accessible for retrieval Therefore a stack is a restricted form of list –Use the List interface –Use an array (as inside ArrayList) –Use linked nodes (as inside LinkedList) Java does none of these - it uses a vector (growable array) –But, to understand stacks better, we will consider the 3 options above

Implementing using a List //Complete code pp public class ListStack { private List theData; public Stack { theData = new ArrayList (); } //more methods here }

ArrayList Implementation Push adds to the list –Where? Why? Pop removes an item –Which one? Peek looks at the top item –Where is it? ArrayList takes care of resizing the list, details of implementations

Empty and Push Method Public boolean empty() { return theData.size() == 0; } public E push (E obj){ //add at end of ArrayList theData.add (obj); return (obj); }

Peek public E peek() { if (empty()) throw new EmptyStackException(); //top is at the end of the array return theData.get(theData.size()-1); }

Pop public E pop() { if (empty()) throw new EmptyStackException(); //top is at the end of the array //recall: remove returns the object removed return theData.remove(theData.size()-1); }

And now more detail… Pretend we don’t already have ArrayList or LinkedList, and had to implement all from scratch Why? –Understand in detail how stacks work –See the differences and tradeoffs between array and linked stacks –Appreciate how much easier it is to implement a stack than a list (either type)

Array Implementation Push: insert element into the array (where?) Peek: retrieve the most recently inserted element (where is it?) Pop: remove the most recently inserted element (how)? Static vs. dynamic array?

Array Push, Pop All stack elements go into the array (in order) Newest element is last (highest index) in the array Integer variable “top” indicates index of top Push increments top Pop decrements top –Popped elements stay in array until overwritten by new push

Choices for top Top points after the top element –Push: first copy, then increment –Top: A[top-1] –Pop(item): first decrement, then copy If top points at the top element –Push: first increment, then copy –Top: A[top] –Pop(item): copy item, decrement

Example: Top at Empty stack (top = –1) Push ‘a’, Push ‘b’, Push ‘c’ (top=2) Pop, Pop (top = 0) Push ‘z’ (top = 1) A[0]=A[1]=A[2]=A[3]=A[4]= A[0]=‘a’A[1]=‘b’A[2]=‘c’A[3]=A[4]= A[0]=‘a’A[1]=‘b’A[2]=‘c’A[3]=A[4]= A[0]=‘a’A[1]=‘z’A[2]=‘c’A[3]=A[4]=

Possible Errors Pop from empty stack Top of empty stack –Throw EmptyStackException Push if stack is full (CAPACITY items) –not a problem if resize is allowed (as in ArrayList) –If not, a FullStackException could be thrown

Class definition (data members) public class ArrayStack { // array for the stack E[ ] theData; //index of top item int topOfStack = -1;

Constructor and Check for Empty //Initial capacity private static final int INITIAL_CAPACITY = 10; //Construct an empty stack public ArrayStack() { theData = (E[]) new Object[INITIAL_CAPACITY]; } //Empty? public boolean empty(){ return top == -1; }

Push //Push an item onto the stack public E push(E obj){ if (topOfStack == theData.length-1) { reallocate(); //or could throw an exception } //++top increments first, returns new value theData[++topOfStack] = obj; return obj }

Pop //Pop an item from a stack and return it public E Pop(){ if (empty()) throw new EmptyStackException(); else return theData[topOfStack--]; }

Efficiency of stack Push: constant time –Even though insert into array is not! (why?) Pop: constant time –Even though delete from array is not!

Node (Linked-List) Implementation Push: insert element into the list (where?) Peek: retrieve the most recently inserted element (where is it?) Pop: remove the most recently inserted element (how)? isEmpty: (how do you know the stack is empty?) What special cases require exceptions?

Linked implementation Underlying data structure is a singly linked list Push inserts at the beginning of the list –Exception: cannot allocate a new node Pop removes from the beginning of the list –Exception: empty list (stack) Empty stack when top is null

Example (Linked Implementation) Empty stack Push ‘a’, Push ‘b’, Push ‘c’ Pop, Pop Push ‘z’ cba a za

Class Definition Public class LinkedStack { private Node topOfStackRef = null; … private static class Node { //same as for linked list } };

empty & peek //Is the stack empty? Public boolean empty() { return (topOfStackRef == null); } //Get item from top Public E peek() { if (empty()) throw new EmptyStackException(); return topOfStackRef.data; }

Push // push (add to beginning of list) public E push (E obj){ topOfStackRef = new Node (obj, topOfStackRef); return obj; }

Pop //Pop and recover old top item Public E pop(){ if (empty()) throw new EmptyStackException(); else{ E result = topOfStackRef.data; topOfStackRef = topOfStackRef.next; return result; }

Nodes vs. Arrays Array is simpler, requires extra space (unused capacity) Node uses minimum amount of space Push and pop are O(1) either way We never need to access the middle of the list. Note: replace ArrayList by LinkedList on slide 6 to get the list functionality without writing any new code. You must also replace add() by addFirst() in push, and remove() by removeFirst() in pop.

Postfix Expressions Also called RPN (reverse Polish notation) –Enter first operand, then second operand, then operator –(Operands can be recursive expressions) –Example 1+(2*3) becomes * + First operand is 1 Second operand is (recursively) 2 3 * Operator is +

Stacks Evaluate Postfix Reading the data: If you see an operand (number), push it If you see an operator, pop the first two items off the stack perform the operation push the result back on the stack Example: * + –Push 1, push 2, push 3, pop 3, pop 2, push 6 –Pop 6, pop 1, push 7

Infix Expressions Infix expressions are the kind we use in Java –6*3+7 –6+3*7 –(6+3) * 7 Order of operation depends on precedence –Multiplication first, then addition –Parentheses override order –Otherwise, left to right

Converting Infix to Postfix Operands (numbers) stay in the same order Operators go in after their operands are complete –6* * 7 + –6+3* * + –(6+3) * * How to decide when to insert the operator?

Algorithm for Infix to Postfix For each token –If it is an operand, insert it immediately in the result –If it is an operator –While stack is not empty AND this operator has lower precedence than top-of-stack operator – pop operator from stack into expression – push this operator Pop remaining operators into expression

Examples –6* * 7 + –6+3* * +

What about parentheses? When you see a left parenthesis, push it on the stack When you see a right parenthesis, pop all operators back to the left paren Why? –Parentheses delimit a new expression –Right paren acts like end of expression (pop all remaining operators at the end)

Example –(6+3) * *

A Search Problem Given a sequence of flight segments, can you reach one city from the other? If there is a flight from A to B, then A and B are adjacent (A->B) If (A->B) and (B->C), we can reach C from A

Recursive Solution (pseudocode) bool canReach(city A, city B){ If (A == B) return true; //trivial problem If (adjacent(A,B)) return true; //direct flight For (each city adjacent to A) if canReach(city, B) return true; Return false; }

Stack-based solution (pseudocode) bool canReach(city A, city B){ If (A == B) return true; //trivial problem Else push A onto the stack While(stack is not empty and top != B) if the city has an unvisited neighbor mark the neighbor as visited push the neighbor on the stack else pop the stack; } If the stack is empty return false; Otherwise return true (and the path is on the stack!) }

Comments on stack based search No essential difference between recursive and stack algorithms This algorithm is called “depth-first search” Path that is found will not necessarily be the shortest Order of visits doesn’t matter for correctness, but matters significantly for efficiency