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,

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

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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
CMPT 225 Stacks.
Chapter 3 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?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
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.
© 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.
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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
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,
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
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.
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.
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 The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
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.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
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.
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.
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.
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Stacks.
Stack and Queue APURBO DATTA.
Stack.
Chapter 6 Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
CSC 205 – Java Programming II
ADT list.
Stacks CS-240 Dick Steflik.
Ch. 7 Stack 도입을 위한 예 “←” in keyboard input line
Chapters 6 and 7 Stacks.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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, or simply LIFO

3 ADT Stack Operations 1. Create an empty stack 2. Determine whether a stack is empty 3. Add a new item to the stack (push) 4. Remove from the stack the item that was added most recently (pop) 5. Remove all items from the stack 6. Retrieve from the stack the item that was added most recently

4 Pseudocode for the ADT Stack Operations createStack() //Creates an empty stack isEmpty() //Determines whether a stack is empty push(newItem) throws StackException /*Adds newItem to the top of the stack. Throws StackException if the insertion is not successful */ pop() throws StackException /*Retrieves and then removes the top of the stack (the item that was added most recently). Throws StackException if the deletion is not successful */

5 Pseudocode for the ADT Stack Operations popAll() //Removes all items from the stack peek() throws StackException /* Retrieves the top of the stack. That is, peek retrieves the item that was added most recently. Retrieval does not change the stack. Throws StackException if the retrieval is not successful */

6 Simple Applications of the ADT Stack - Checking for Balanced Braces Curly braces, “{“ and “}” delimits groups of statements To balance, keep track of each unmatched “{“ and discard one each time you encounter a “}” Push each “{“ encountered onto a stack and pop one off each time you encounter a “}”

7 Draft solution - Checking for Balanced Braces while (not at the end of the string) { if (the next character is a ‘{‘ ) { stack.push( ‘{‘ ) } else if (the character is a ‘}’ ) { openBrace = stack.pop() } // end if } //end while Requirements for balanced braces: 1.Each time you encounter a “}”, it matches an already encountered “{“ 2.When you reach the end of the string, you have matched each “{“

8 Detailed solution – Check a string for Balanced Braces stack.createStack() bancedSoFar = true i = 0 while (balancedSoFar and i < length of aString) { i++ //push an open brace if (ch is ‘{‘ ) { stack.push( ‘{‘ ) } //close brace else if ( ch is ‘}’ ) { if (! stack.isEmpty() ) { openBrace = stack.pop() //pop a matching open brace } else { //no matching open brace balancedSoFar = false } // end if // ignore all characters other than braces } // end while

9 Detailed solution – Check a string for Balanced Braces if ( balancedSoFar and stack.isEmpty() ) { aString has balanced braces } else { aString does not have balanced braces } // end if Input string {a{b}c} { {{{{{ Stack as algorithm executes push “{“ 2.push “{“ 3.pop 4.pop Stack empty  balanced

10 Implementation of the ADT Stack public interface StackInterface { public boolean isEmpty(); //Determines whether the stack is empty. //Precondition: None. //Postcondition: Returns true if the stack is empty; //otherwise returns false public void popAll(); //Removes all the items from the stack. //Precondition: None //Postcondition: Stack is empty. public void push(Object newItem) throws StackException; //Adds an item to the top of a stack //Precondition: newItem is the item to be added //Postcondition: If insertion is successful, newItem is on //the top of the stack. //Exception: Some implementation may throw a //StackException when newItem cannot be placed on the //stack.

11 Implementation of the ADT Stack public object pop() throws StackException; //Removes the top of a stack //Precondition: None //Postcondition: If the stack is not empty, the item that was //added most recently is removed from the stack and returned //Exception: Throws StackException f the stack is empty public Object peek() throws StackException; //Retrieves the top of a stack //Precondition: None //Postcondition: If the stack is not empty, the item that was //added most recently is removed from the stack and returned //Exception: Throws StackException if the stack is empty }//end StackInterface

12 Class StackException public class StackException extends java.lang.RuntimeException { public StackException(String s) { super(s); }// end constructor }// end StackException

13 Implementations of the ADT stack that use (a) an array; (b) a linked list; (c) an ADT list Array top Linked list top ADT list top (a) (b) (c)

14 An Array-Based Implementation of the ADT Stack … items 012k k top MAX_STACK - 1 Array indexes public class StackArrayBased implements StackInterface { final int MAX_STACK = 50; //maximum size of stack private Object items[]; private int top; public StackArrayBased() { items = new Object[MAX_STACK]; top = -1; } // end default constructor

15 An Array-Based Implementation of the ADT Stack public boolean isEmpty() { return top < 0; }// end isEmpty public boolean isFull() { return top == MAX_STACK – 1; } // end isFull public void push(Object newItem) throws StackException { if ( ! isFull()) { items[++top] = newItem; } else { throw new StackException(“SatckException on push: stack full“); } // end if }// end push

16 public void popAll() { items = new Object[MAX_STACK]; top = -1; }// end popAll public Object pop() throws StackException { if (!isEmpty()) { return items[top--]; } else { throws new StackException(“StackException on pop: Stack empty”); } // end if } // end pop public Object peek() throws StackException { if (!isEmpty()) { return items[top]; } else { throws new StackException(“StackException on peek: Stack empty”); } // end if } // end peek }// end StackArrayBased

17 Using the ADT stack … public class StackTest { public static final int MAX_ITEMS = 15; public static void main(String[] args) { StackArrayBased stack = new StackArrayBased(); Integer items[] = new Integer[MAX_ITEMS]; for (int i = 0; i < MAX_ITEMS; i++){ items[i] = new Integer(i); if (!stack.isFull()){ stack.push(items[i]); }// end if }// end for while (!stack.isEmpty()){ //cast results of pop to Integer System.out.println((Integer)(stack.pop())); } // end while …

18 A Reference-Based Implementation of the ADT Stack public class StackReferenceBased implements StackInterface { private Node top; public StackReferenceBased() { top = null; }// end default constructor public boolean isEmpty() { return top == null; }// end isEmpty public void push(Object newItem){ top = new Node (newItem, top); }// end push public Object pop() throws StackException { if (!isEmpty()) { Node temp = top; top = top.getNext(); return temp.getItem(); }

19 A Reference-Based Implementation of the ADT Stack else { throw new StackException(“StackException on pop: Stack empty”); }// end if }// end pop public void popAll() { top = null; }// end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException(“StackException on peek: Stack empty”); }// end if }// end peek } // end StackReferenceBased

20 Your Turn Provide implementation of the ADT stack using the ADT list.