Click to edit Master text styles Stacks Data Structure.

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 & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks.
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.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
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?
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks.
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 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.
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.
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)
Building Java Programs
Topic 3 The Stack ADT.
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.
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.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
Information and Computer Sciences University of Hawaii, Manoa
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.
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.
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.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
Data Structures & Algorithms
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.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Stacks and Queues MR. Mohammad Alqahtani.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Stacks Chapter 5.
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.
CSE 373: Data Structures and Algorithms
Building Java Programs
Stacks.
CSC 205 – Java Programming II
Stacks.
CSC 1052 Stacks 1/11/2019.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Presentation transcript:

Click to edit Master text styles Stacks Data Structure

Click to edit Master text styles Stack Collection A stack is a sequence of items that are accessible only from the top of the stack. 1 A Stack (of plates) push pop Other operations: is Empty peek size

Click to edit Master text styles Stack Operation An item popped from the stack is the last element that was pushed. A stack has LIFO (last-in-first-out) ordering.

Click to edit Master text styles Stack Operation

Click to edit Master text styles Stack Abstract Data Type push(e) : Adds element e to the top of the stack. pop( ) : Removes and returns the top element from the stack (or null if the stack is empty). Additionally, a stack supports the following accessor methods for convenience: top( ) : Returns the top element of the stack, without removing it (or null if the stack is empty). size( ) : Returns the number of elements in the stack. isEmpty( ) : Returns a Boolean indicating whether the stack is empty.

Click to edit Master text styles Example: The following table shows a series of stack operations and their effects on an initially empty stack S of integers.

Click to edit Master text styles The java.util.Stack Class Provides a side-by-side comparison of the interface for out stack ADT and the java.util.Stack class. Methods pop and peek of the java.util.Stack class throw a custom EmptyStackException (StackX.java)

Click to edit Master text styles The Array List Stack Class (ALStack) A stack can be implemented using a List class the ALStack class uses an ArrayList to implement the Stack interface.

Click to edit Master text styles A Simple Array-Based Stack Implement Arrays start at index 0 in Java, when the stack holds elements from data[0] to data[t] inclusive, it has size t+1. When the stack is empty it will have t equal to -1 (and thus has size t+1, which is 0). (StackApp.java)

Click to edit Master text styles StackX.java Class Methods The constructor creates a new stack of a size specified in its argument. The push() method increments top so it points to the space just above the previous top, and stores a data item there. The pop() method returns the value at top. The peek() method simply returns the value at top. The isEmpty() and isFull() methods return true if the stack is empty or full, The top variable is at -1 if the stack is empty and maxSize-1 if the stack is full.

Click to edit Master text styles Stack Example 1 : Reversing a Word First the characters are extracted one by one from the input string and pushed onto the stack. Then they’re poped off the stack and displayed. The stack reverses the order of the characters. Shows the code for the Reverser.java program.

Click to edit Master text styles Stack Example 2: Delimiter Matching Typically the strings are lines of code in a computer language, and the programs parsing them are compilers. The delimiters are the braces ‘{‘and’}’, brackets ‘[‘and’]’, and parentheses ‘(‘and’)’. Each opening or left delimiter should be matched by a closing or right delimiter.

Click to edit Master text styles Stack Example 2: Delimiter Matching Typically the strings are lines of code in a computer language, and the programs parsing them are compilers. The delimiters are the braces ‘{‘and’}’, brackets ‘[‘and’]’, and parentheses ‘(‘and’)’. Each opening or left delimiter should be matched by a closing or right delimiter.