6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.

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

Stacks.
The ADT Stack Definition
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays.
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
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.
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,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks, Queues & Deques CSC212.
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.
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.
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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,
Data Structures and Algorithms
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks  Introduction  Applications  Implementations  Complex Applications.
7.2 Priority Queue ADTs Priority queue concepts
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.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
9 Set ADTs  Set concepts  Set applications  A set ADT: requirements, contract  Implementations of sets: using member arrays, linked lists, boolean.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Linked Lists, Queues, Stacks
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
11 Map ADTs Map concepts. Map applications.
Recap: Solution of Last Lecture Activity
Stacks.
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Stacks.
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Introduction to Stacks
8 List ADTs List concepts. List applications.
Presentation transcript:

6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks in the Java class library. © 2001, D.A. Watt and D.F. Brown

6-2 Stack concepts (1) A stack is a last-in-first-out sequence of elements. Elements can added and removed only at one end (the top of the stack). The depth of stack is the number of elements it contains. An empty stack has depth zero.

6-3 Stack concepts (2) Illustration (stack of books): Moby Dick War & Peace Rob Roy Initially: Moby Dick War & Peace After remov- ing a book: Moby Dick War & Peace Misérables After adding “Misérables”: Moby Dick War & Peace Misérables 2001 After adding “2001”:

6-4 Stack applications Interpreter (e.g., the Java Virtual Machine)  maintains a stack containing intermediate results during evaluation of complicated expressions  also containing arguments and return addresses for method calls and returns. Parser (e.g., a component of the Java compiler)  maintains a stack containing symbols encountered during parsing.

6-5 Example 1: text-file reversal A text file is a sequence of (zero or more) lines. To reverse the order of these lines, we must store them in a first-in-last-out sequence. Text-file reversal algorithm: To output the lines of file in reverse order: 1.Make line-stack empty. 2.For each line read from file, repeat: 2.1.Add line to the top of line-stack. 3.While line-stack is not empty, repeat: 3.1.Remove a line from the top of line-stack into line. 3.2.Output line. 4.Terminate.

6-6 Example 2: bracketing (1) A phrase is well-bracketed if:  for every left bracket, there is a later matching right bracket  for every right bracket, there is an earlier matching left bracket  the subphrase between a pair of matching brackets is itself well- bracketed. Examples (mathematical expressions): s  (s – a)  (s – b)  (s – c) (– b +  [b 2 – 4ac]) / 2a s  (s – a)  (s – b  (s – c) s  (s – a)  s – b)  (s – c) (– b +  [b 2 – 4ac)] / 2a well-bracketed ill-bracketed

6-7 Example 2 (2) Bracket matching algorithm: To test whether phrase is well-bracketed: 1.Make bracket-stack empty. 2.For each symbol sym in phrase (scanning from left to right), repeat: 2.1.If sym is a left bracket: Add sym to the top of bracket-stack. 2.2.If sym is a right bracket: If bracket-stack is empty, terminate with false Remove a bracket from the top of bracket-stack into left If left and sym are not matched brackets, terminate with false. 3.Terminate with true if bracket-stack is empty, or false otherwise.

6-8 Stack ADT: requirements Requirements: 1)It must be possible to make a stack empty. 2)It must be possible to add (‘push’) an element to the top of a stack. 3)It must be possible to remove (‘pop’) the topmost element from a stack. 4)It must be possible to test whether a stack is empty. 5)It should be possible to access the topmost element in a stack without removing it.

6-9 Stack ADT: contract (1) Possible contract, expressed as a Java interface: public interface Stack { // Each Stack object is a stack whose elements are objects. /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this stack is empty. public Object getLast (); // Return the element at the top of this stack.

6-10 Stack ADT: contract (2) Possible contract (continued): /////////////// Transformers /////////////// public void clear (); // Make this stack empty. public void addLast (Object elem); // Add elem as the top element of this stack. public Object removeLast (); // Remove and return the element at the top of this stack. }

6-11 Implementation of stacks using arrays (1) Represent a bounded stack (depth  maxdepth) by:  variable depth, containing the current depth  array elems of length maxdepth, containing the stacked elements in elems[0… depth–1]. Invariant: element 01 depth–1depthmaxdepth–1 Empty stack: 1 length=0 maxdepth–1 Illustration (maxdepth = 6): Moby Dick War & Peace Rob Roy depth=3 4 5 topmost elementunoccupied

6-12 Implementation using arrays (2) Java implementation: public class ArrayStack implements Stack { private Object[] elems; private int depth; /////////////// Constructor /////////////// public ArrayStack (int maxDepth) { elems = new Object[maxDepth]; depth = 0; }

6-13 Implementation using arrays (3) Java implementation (continued): /////////////// Accessors /////////////// public boolean isEmpty () { return (depth == 0); } public Object getLast () { if (depth == 0) throw … ; return elems[depth-1]; }

6-14 Implementation using arrays (4) Java implementation (continued): /////////////// Transformers /////////////// public void clear () { for (int i = 0; i < depth; i++) elems[i] = null; depth = 0; } public void addLast (Object elem) { if (depth == elems.length) throw … ; elems[depth++] = elem; }

6-15 Implementation using arrays (5) Java implementation (continued): public Object removeLast () { if (depth == 0) throw … ; Object topElem = elems[--depth]; elems[depth] = null; return topElem; } } Analysis:  Operations isEmpty, getLast, addLast, removeLast have time complexity O(1).  Operation clear has time complexity O(n).

6-16 Implementation of stacks using SLLs (1) Represent an (unbounded) stack by an SLL, such that the first node contains the topmost element. Invariant: element Empty stack: Illustration: Rob Roy War & Peace Moby Dick topmost element

6-17 Implementation using SLLs (2) Java implementation: public class LinkedStack implements Stack { private SLLNode top; /////////////// Constructor /////////////// public LinkedStack () { top = null; }

6-18 Implementation using SLLs (3) Java implementation (continued): /////////////// Accessors /////////////// public boolean isEmpty () { return (top == null); } public Object getLast () { if (top == null) throw … ; return top.element; }

6-19 Implementation using SLLs (4) Java implementation (continued): /////////////// Transformers /////////////// public void clear () { top = null; } public void addLast (Object elem) { top = new SLLNode(elem, top); }

6-20 Implementation using SLLs (5) Java implementation (continued): public Object removeLast () { if (top == null) throw … ; Object topElem = top.element; top = top.succ; return topElem; } } Analysis:  All operations have time complexity O(1).

6-21 Stacks in the Java class library Java provides no Stack interface or class as such. However, the java.util.LinkedList class provides all the above Stack operations. Illustration: import java.util.LinkedList; LinkedList bookStack = new LinkedList(); bookStack.addLast("Moby Dick"); bookStack.addLast("War & Peace"); bookStack.addLast("Rob Roy"); System.out.println(bookStack.removeLast());

6-22 Example 3: text-file reversal revisited (1) Recall the text-file reversal algorithm of Example 1. Implementation: public static void reverse ( BufferedReader input, BufferedWriter output) throws IOException { LinkedList lineStack = new LinkedList(); for (;;) { String line = input.readLine(); if (line == null) break; // end of input lineStack.addLast(line); } input.close();

6-23 Example 3 (2) Implementation (continued): while (! lineStack.isEmpty()) { String line = lineStack.removeLast(); output.write(line + "\n"); } output.close(); }