3/3/20161 Stacks and Queues 15-121 Introduction to Data Structures Ananda Gunawardena.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Data Structures and Algorithms (60-254)
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Stacks.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks, Queues & Deques CSC212.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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. 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.
TCSS 342, Winter 2005 Lecture Notes
Implementing and Using Stacks
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.”
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
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.
Stacks and Queues Introduction to Computing Science and Programming I.
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 AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
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’
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Introduction to Algorithm Analysis Concepts Fundamental Data Structures and Algorithms Peter Lee January 21, 2003.
April 27, 2017 COSC Data Structures I Review & Final Exam
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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 and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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,
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
G64ADS Advanced Data Structures
COSC160: Data Structures: Lists and Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 15 Lists Objectives
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
ITEC 2620M Introduction to Data Structures
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
Lecture 16 Stacks and Queues CSE /26/2018.
Lecture 9: Stack and Queue
Presentation transcript:

3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena

Stack and Heap A stack is a first-in-last-out structure 3/3/20162 When a method is called computer stores all related data in a place called stack When the method returns, stack is used generate the return parameters

Basic Architecture 3/3/20163 Stacks retain Information About where to return Stacks can be used (implicitly) to implement a technique called recursion

3/3/20164 A Stack interface public interface Stack { public void push(Object x); public void pop(); public Object top(); public boolean isEmpty(); public void clear(); }

3/3/20165 Stacks are LIFO a b c d e Push operations:

3/3/20166 Stacks are LIFO a b c d e Pop operation: Last element that was pushed is the first to be popped.

3/3/20167 Queues Behavior –add item only from the back (enqueue) –remove items only from the front (dequeue) Many Applications – printer queue – network queues – common resource sharing Queue Operations –enqueue –dequeue –clear –empty –full

3/3/20168 A Queue interface public interface Queue { public void enqueue(Object x); public Object dequeue(); public boolean isEmpty(); public void clear(); }

3/3/20169 Queues are FIFO front back krqcm

3/3/ Queues are FIFO front back krqcm y Enqueue operation:

3/3/ Queues are FIFO frontback krqcmy Enqueue operation:

3/3/ Queues are FIFO frontback krqcmy Dequeue operation:

Implementation 3/3/201613

3/3/ Implementing stacks, 1 abc Linked representation. All operations constant time, or O(1).

3/3/ Implementing stacks, 2 An alternative is to use an array-based representation. What are some advantages and disadvantages of an array-based representation? abc top

3/3/ A queue from two stacks f g h i j Enqueue: e d c b a Dequeue: What happens when the stack on the right becomes empty?

Implementing a Queue with an array 3/3/201617

Applications 3/3/201618

3/3/ Applications of Stacks Balancing Symbols Problem: Write a program that will check the validity of a statement Eg: (2 + 3 ( 4 – 5 )) is valid (2 + 3 (4 – 5 ) is not valid Discuss an algorithm using stacks

3/3/ Applications of Stacks ctd.. Evaluating Postfix expressions Consider the expression ( 2+ 3*(5 – 2) + 3*2) How do we write a program to find the answer to the above expression? Write the expression in postfix notation * * + Use a stack to evaluate it.

3/3/ Stack Application - Maze Think about a grid of rooms separated by walls. Each room can be given a name. abcd hgfe ijkl ponm Find a path thru the maze from a to p.

3/3/ Applications of Queue Breadth First Traversal

3/3/ Applications of Queue Snake Game

Homework Read notes on recursion Think of more applications where a stack or queue can be used Read the assignment on Mazes 3/3/201624