ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

ICOM 4035 – Data Structures Lecture 15 – Graph ADT
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
ICOM 4035 – Data Structures Lecture 8 – Doubly Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
Topic 15 Implementing and Using 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 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.
Stacks.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
ICOM 4035 – Data Structures Lecture 2 – ADT and Interfaces Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
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,
Stack and Queue.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ICOM 4035 – Data Structures Lecture 5 – List ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 6 – Big-O Notation Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 1 - Introduction Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 7 – Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Click to edit Master text styles Stacks Data Structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
CSE 373: Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Topic 15 Implementing and Using Stacks
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved

Lecture Organization Part I – Introduction to the Stack ADT Part II – Design and implementation of stack using arrays Part III – Design and implementation of stack using a linked list M. Rodriguez-MartinezICOM

Objectives Introduce the concept of a Stack – Last-In First-Out (LIFO) list Discuss the application of stacks Understand the design and implementation of a stack using arrays and linked lists Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

Companion videos Lecture9 videos – Contains the coding process associated with this lecture – Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-MartinezICOM

Part I Introduction to the Stack ADT M. Rodriguez-MartinezICOM

Stack ADT Stack: – collection of things with restriction on access Element are added/removed at the top Last element in must be first element out No notion of specific position for element other than element at the top – repetitions are allowed M. Rodriguez-MartinezICOM Stack of books Stack of coins Stack of pancakes Apu Ned Ron Stack of names

Structure of a Stack M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack Apu Ned Ron Top of Stack Ned Ron Top of Stack Amy Element are always: Added at the top Removed from top Stack: Last-In First-Out (LIFO) List Adding Amy to stack Removing Apu from stack

Stack ADT Operations size() – number of elements in stack isEmpty() – is the stack empty top() – inspect element at the top of stack without removing it push() – add a new element at the top of the stack pop() – remove and returns element at the top of the stack makeEmpty() –remove all elements from stack M. Rodriguez-MartinezICOM

Stack operations: Top() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.top() Apu Ned Ron Top of Stack S= Returns: “Apu” Top() – inspects and returns the element at the top of the stack. No modification is made on stack.

Stack operations: Pop() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.pop() Ned Ron Top of Stack S= Returns: “Apu” Pop() – removes and returns the element at the top of the stack. Stack is shrunk.

Stack operations: Push() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.push(“Jil”) Apu Ned Ron Top of Stack S= Returns: nothing Push() – adds a new element at the top of the stack. Stack grows. Jil

Uses of Stack (1) M. Rodriguez-MartinezICOM ( ((5 + 4)*1) (5 + 4)*1) ( ( 5 + 4)*1) ( ( + 4)*1) ( ( 4)*1) ( ( )*1) ( *1) ( 1) ( ) S= Checking if parenthesis in an expression are balanced Empty stack S Idea: ( is added to stack, when ) is read, element at top is removed If stack is empty after reading the input the expression has balanced parenthesis

Uses of Stack (2) M. Rodriguez-MartinezICOM (equivalent 4+5) 45+7* (equivalent to (4+5)*7) Evaluation of expressions in postfix notation 45+ S= * S= 4 5+7* * 7* 7 * 9 63

Part II Design and implementation of stack using arrays M. Rodriguez-MartinezICOM

Using array to implement stacks M. Rodriguez-MartinezICOM Apu Ned Ron S= Ron NedApu unused in use top: 4 Top of Stack Variable top gives the next available top of the stack position. Also gives size of stack Current top of the stack is equal to top-1 Top of Stack

Stack operations: Top() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.top() Apu Ned Ron Top of Stack S= Returns: “Apu” Top() – inspects and returns the element at the top of the stack. No modification is made on stack.

Stack operations: Top() (2) If not empty, simply return element at top-1 Complexity: O(1) – Random access to last element M. Rodriguez-MartinezICOM Ron NedApu unused in use top: 4 Top of Stack

Stack operations: Pop() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.pop() Ned Ron Top of Stack S= Returns: “Apu” Pop() – removes and returns the element at the top of the stack. Stack is shrunk.

Stack operations: Pop() (2) If not empty, simply return element at top-1 – Apu Decrement top by 1 – --top Complexity: O(1) – Random access to last element M. Rodriguez-MartinezICOM Ron NedApu unused in use top: 4 Top of Stack Ron Ned unused in use top: 3 Top of Stack

Stack operations: Push() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.push(“Jil”) Apu Ned Ron Top of Stack S= Returns: nothing Push() – adds a new element at the top of the stack. Stack grows. Jil

Stack operations: Push() (2) Add new element at position top Increment top by 1 – ++top Complexity: O(n) – Worst case: reallocate array when full M. Rodriguez-MartinezICOM Ron NedApu unused in use top: 4 Top of Stack Ron NedApuJil unused in use top: 5 Top of Stack

Easy operations size() – Return value of variable top – Complexity: O(1) isEmpty() – Return size() == 0 – Complexity: O(1) makeEmpty() – Call pop() while stack is not empty – Complexity: O(n), n = S.size() M. Rodriguez-MartinezICOM

Part III Design and implementation of stack using linked list M. Rodriguez-MartinezICOM

Using linked list to implement stacks M. Rodriguez-MartinezICOM Apu Ned Ron S= Top of Stack Header of linked list point to the top of the stack current size gives you number of elements ApuNedRon header current size: 4 Top of Stack

Stack operations: Top() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.top() Apu Ned Ron Top of Stack S= Returns: “Apu” Top() – inspects and returns the element at the top of the stack. No modification is made on stack.

Stack operations: Top() (2) If not empty, return element at header.getNext().getValue() Complexity: O(1) – Just look value at first node M. Rodriguez-MartinezICOM ApuNedRon header current size: 4 Top of Stack

Stack operations: Pop() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.pop() Ned Ron Top of Stack S= Returns: “Apu” Pop() – removes and returns the element at the top of the stack. Stack is shrunk.

Stack operations: pop() M. Rodriguez-MartinezICOM ApuNedRon header current size: 4 Top of Stack ApuNedRon header current size: 3 Top of Stack

Stack operations: pop() (3) Simply move header.getNext() to header.getNext().getNext() Decrement current size by 1 : --currentSize Complexity: O(1) – Just remove value at first node M. Rodriguez-MartinezICOM NedRon header current size: 3 Top of Stack

Stack operations: Push() M. Rodriguez-MartinezICOM Apu Ned Ron Top of Stack S= S.push(“Jil”) Apu Ned Ron Top of Stack S= Returns: nothing Push() – adds a new element at the top of the stack. Stack grows. Jil

Stack operations: Push() (2) M. Rodriguez-MartinezICOM ApuNedRon header current size: 4 Top of Stack ApuNedRon header current size: 5 Top of Stack Jil

Stack operations: push() (3) Simply add new node after header Increment current size by 1 : ++currentSize Complexity: O(1) – Just add value after header node M. Rodriguez-MartinezICOM NedRon header current size: 4 Top of Stack Jil

Easy operations size() – Return value of variable currentSize – Complexity: O(1) isEmpty() – Return size() == 0 – Complexity: O(1) makeEmpty() – Call pop() while stack is not empty – Complexity: O(n), n = S.size() M. Rodriguez-MartinezICOM

Summary Introduced the concept of a Stack – Last-In First-Out (LIFO) list – Elements are added/removed from top Discuss the application of stacks – Match parenthesis – Postfix calculator Describe the design and implementation with – arrays – linked lists M. Rodriguez-MartinezICOM

Questions? – ICOM 4035M. Rodriguez-Martinez 35