Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Stacks Chapter 11.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS Data Structures II Review COSC 2006 April 14, 2017
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CMPT 225 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
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.
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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
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.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
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.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Abstract Data Types (ADTs)
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks Chapter 5.
Abstract Data Types (ADTs)
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Chapter 7 Stacks I CS2006 - Data Structures I COSC 2006 April 22, 2017

Tutorial Do not teach new materials Review the lectures Hand out simple programming problems Solve them Use more examples to explain concepts Answer assignment problems

Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation of ADT Stack

Stack Of Cups Remove a cup from new stack. bottom top bottom top COSC 2006 April 22, 2017 Stack Of Cups bottom top C A B D E F bottom top C A B D E Picture is really a stack of cups and saucers. LIFO = last in first out. The first cup that is removed from a stack of cups is the Last one that was added to the stack. Other examples of LIFO lists in real life: stack of trays in a cafeteria; paper stack in a printer or copy machine; newspaper stack at a news stand. Add a cup to the stack. Remove a cup from new stack. Chapter 6: Stacks

Introduction Stack: Ordered group of homogeneous items Only one item can be accessed at a time (top) Items addition / removal take place only at the top Inserting an item is known as "pushing" onto the stack. Removing an item is known as "popping" off the stack. Has Last-In-First-Out (LIFO) behavior

Introduction Real-life examples: Very useful in computer science Books Pennies More??? Very useful in computer science Converting numeric strings into equivalent values Evaluating algebraic expressions

LIFO A stack is a LIFO (Last-in-first-out) list top push pop 5 4 5 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 1 stack depth = 5 bottom

Introduction Example: Line-editor a b c c d d d e   e f f g  means backspace will generate the following output: a b c d e f g Observations

Introduction Example: Line-editor Pseudocode: // Read the line, correcting mistakes along the way while (not end of line) { Read a new character ch if ( ch is not a ‘ ‘ ) Add ch to the ADT else Remove the most recently added item from the ADT } // end while

Introduction Example: Line-editor The following operations are needed Determine if the line is empty Add a character Remove the most-recently added character Retrieve the most recently added item These are exactly the operations needed for a stack The ADT Stack can be used to perform the reading and correction of the line

The Stack Interface In Java COSC 2006 April 22, 2017 The Stack Interface In Java We can define our own stack interface like this: public interface StackInterface { public boolean isFull(); // returns if the stack is full public boolean isEmpty(); // return if the stack is empty public Object top() // return the top element throws StackException; // if the stack is empty public void push(Object obj); // push obj onto the stack public void popAll (); public Object pop() // return and remove the top element of the stack throws StackException; // if the stack is empty } Chapter 6: Stacks

Using ADT Stack Read & Correct a text of line: Using stack operations readAndCorrect ( out aStack : Stack) // Reads input line and either enters characters onto stack S // or corrects contents if the character is the ‘ ‘ symbol aStack.createStack Read newChar while ( newChar != eoln ) { if ( newChar != ‘ ‘ ) aStack.Push( newChar) else if (! aStack.isEmpty()) aStack.Pop( ) read newChar } // end while How to write the contents in a stack backwards?

Stack Application Example: Balanced Braces Assumption Example: All braces are of the same type Example: a b c { d e f g { i j k } { l { m n } } o p } q r Balanced a b c { d e f } } { g h I j { k l } m Unbalanced Any idea to evaluate?

Stack Application Balanced Braces Idea: keep track for unmatched ‘{‘ by pushing them onto a stack When ‘}’ is encountered , pop one of the unmatched ‘{‘ from the stack Stop when The string and stack are both empty  Success Stack is empty, but string still contains closing braces (‘}‘)  Failure: more closing braces String is empty but stack still contains unmatched opening braces (‘{‘’)  Failure: more opening braces

Stack Application Balanced Braces: Pseudocode aStack.createStack ( ) balancedSoFar = true i = 0 while ( balancedSoFar and i < length of aString) { ch = character at position i ++ i if ( ch = ‘{‘) aStack.push( ‘{‘ ) else if ( ch = ‘}’ ) aStack.pop( ) // Pop a matching open brace else balancedSoFar = false // ignore all characters other that braces } // end while if ( balancedSoFar && aStack.isEmpty( ) ) aString has balanced braces else aString doesn’t have balanced braces

Stack Application Balanced Braces: Examples { a { b } c } { a { b c }

Stack Application How about math expression with 3 types of brackets () [] {} {(A+C)*D

Stack Application Algorithm Scan expression from left to right Each time a left bracket is encountered, push it onto stack When a right bracket is encountered, compare it with the top item on the stack If it is a match, pop the stack If not, report illegal bracketing If the stack id prematurely empty, report illegal If there are items left on the stack after the expression has been scanned, report illegal bracketing

Stack Application (3+4)*{7+[4/8]}

Stack Application Write the pseudo code by yourself Special-Palindromes L = { w $ w ' : w is either empty or any string not including $, and w ' = reverse (w) } Idea: Traverse the first half and push its elements onto the stack When $ is reached, ignore it and start traversing and matching each element from the input string with that on the stack Termination: Success: Both stack and string are empty Failure: Character on input doesn't match character on stack top String empty, but stack isn't Stack empty, but string isn't Write the pseudo code by yourself

ADT Stack Implementation Implementations Arrays Linked lists And?

ADT Stack Implementation Array Linked List ADT List Where is the top? Top 10 20 30 10 20 30 Top 10 20 30 Top

COSC 2006 April 22, 2017 An Array-Based Stack We can use arrays for our stacks by requiring a maximum size N for our stack (e.g. N = 50) The stack would then consist of an N-element array, s, and the array index of the top element, t Array indices start at 0,so we will initialize t to -1 Chapter 6: Stacks

Stack Interface public interface StackInterface { COSC 2006 April 22, 2017 Stack Interface public interface StackInterface { public boolean isEmpty(); public boolean isFull(); public void push(Object newItem) throws StackException; public Object pop() throws StackException; public void popAll(); public Object peek/top () throws StackException; } // end StackInterface Chapter 6: Stacks

Stack Exception public class StackException COSC 2006 April 22, 2017 Stack Exception public class StackException extends java.lang.RuntimeException { public StackException(String s) { super(s); } // end constructor } // end StackException Chapter 6: Stacks

Array Stack Implementation (1) COSC 2006 April 22, 2017 Array Stack Implementation (1) public class ArrayStackBased implements StackInterface { final int MAX_STACK = 50; // maximum size of stack private Object items[ ]; private int top; public ArrayStackBased() { items = new Object[MAX_STACK]; top = -1; } // end default constructor public boolean isEmpty() { return top < 0; } // end isEmpty public boolean isFull() { return top == MAX_STACK-1; } // end isFull Chapter 6: Stacks

Array Stack Implementation (1) COSC 2006 April 22, 2017 Array Stack Implementation (1) public class ArrayStackBased implements StackInterface { final int MAX_STACK = 50; // maximum size of stack private Object items[ ]; private int top; public ArrayStackBased() { items = new Object[MAX_STACK]; top = -1; } // end default constructor public boolean isEmpty() { return top < 0; } // end isEmpty public boolean isFull() { return top == MAX_STACK-1; } // end isFull Chapter 6: Stacks

Array Stack Implementation (2) COSC 2006 April 22, 2017 Array Stack Implementation (2) public void push(Object newItem) throws StackException { if (!isFull()) { items[++top] = newItem; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public void popAll() { items = new Object[MAX_STACK]; top = -1; } // end popAll Chapter 6: Stacks

Array Stack Implementation (2) COSC 2006 April 22, 2017 Array Stack Implementation (2) public void push(Object newItem) throws StackException { if (!isFull()) { items[++top] = newItem; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public void popAll() { items = new Object[MAX_STACK]; top = -1; } // end popAll Chapter 6: Stacks

Array Stack Implementation (3) COSC 2006 April 22, 2017 Array Stack Implementation (3) public Object pop() throws StackException { if (!isEmpty()) { return items[top--]; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop public Object peek() throws StackException { return items[top]; throw new StackException("Stack exception on " + "peek - stack empty"); } // end peek } // end ArrayStack Chapter 6: Stacks

Array Stack Implementation (3) COSC 2006 April 22, 2017 Array Stack Implementation (3) public Object pop() throws StackException { if (!isEmpty()) { return items[top--]; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop public Object peek() throws StackException { return items[top]; throw new StackException("Stack exception on " + "peek - stack empty"); } // end peek } // end ArrayStack Chapter 6: Stacks

Array Stack Test public class ArrayStackTest { COSC 2006 April 22, 2017 Array Stack Test public class ArrayStackTest { public static final int MAX_ITEMS = 15; public static void main(String[ ] args) { ArrayStackBased stack = new ArrayStackBased(); Integer items[ ] = new Integer[MAX_ITEMS]; System.out.println("Pushing:"); for (int i=0; i<MAX_ITEMS; i++) { items[i] = new Integer(i); if (!stack.isFull()) { System.out.print(" "+i); stack.push(items[i]); } // end if } // end for System.out.println("\nPopping:"); while (!stack.isEmpty()) { // cast result of pop to Integer System.out.print(" "+(Integer)(stack.pop())); } // end while System.out.println(); } // end main } // end ArrayStackTest Chapter 6: Stacks

Array Stack Questions: How to access each object? COSC 2006 April 22, 2017 Array Stack Questions: Can we push/pop primitive type value onto the stack? Can we push/pop different objects onto the stack? Student Car How to access each object? Chapter 6: Stacks

Review If the array: 6, 2, 7, 13, 5, 4 is added to a stack, in the order given, which number will be the first number to be removed from the stack? 6 2 5 4

Review The item that is removed first from a stack is called the ______ of the stack. front top Base prime

Review If the array: 6, 21, 35, 3, 6, 2, 13 is added to a stack, in the order given, which of the following is the top of the stack? 2 6 3 13 35

Review The ______ method of the ADT stack adds an item to the top of the stack. createStack push Pop peek