1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

§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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
queues1 Queues Data structures that wait their turn.
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.
Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,
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.”
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 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.
TCSS 342, Winter 2005 Lecture Notes
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.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
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.”
Objectives of these slides:
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Chapter 7 Queues Introduction Queue applications Implementations.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
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.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
Stacks and Queues.
Stacks, Queues, and Deques
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.
Data Structures and Algorithms for Information Processing
Stacks and Queues CSE 373 Data Structures.
Stacks, Queues, and Deques
Stacks.
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

1 Lecture 9: Stack and Queue

What is a Stack Stack of Books 2

Stacks What is a Stack? –A stack is a data structure of ordered items such that items can be inserted and removed only at one end. 3

4 Stacks What can we do with a stack? –push - place an item on the stack –peek - Look at the item on top of the stack, but do not remove it –pop - Look at the item on top of the stack and remove it

5 Stacks A stack is a LIFO (Last-In/First-Out) data structure A stack is sometimes also called a pushdown store. What are some applications of stacks? –Program execution –Parsing –Evaluating postfix expressions

6 Stacks Problem: –What happens if we try to pop an item off the stack when the stack is empty? This is called a stack underflow. The pop method needs some way of telling us that this has happened. In java we use the java.util.EmptyStackException

Interface IStack Interface Istack { boolean empty(); void push(char c); char pop(); char peek(); }

Using a IStack A balance of braces. –(()) balanced braces –()(()()))) not balanced braces How can you use Istack to check a brace is balanced or not? When you implement the above requirement, you ignore the implementation details of Istack.

9 Implementing a Stack There are two ways we can implement a stack: –Using an array –Using a linked list

10 Implementing a Stack Implementing a stack using an array is fairly easy. –The bottom of the stack is at data[0] –The top of the stack is at data[numItems-1] –push onto the stack at data[numItems] –pop off of the stack at data[numItems-1]

11 Implementing a Stack Implementing a stack using a linked list isn’t that bad either… –Store the items in the stack in a linked list –The top of the stack is the head node, the bottom of the stack is the end of the list –push by adding to the front of the list –pop by removing from the front of the list

12 Reversing a Word We can use a stack to reverse the letters in a word. How?

13 Reversing a Word Read each letter in the word and push it onto the stack When you reach the end of the word, pop the letters off the stack and print them out.

What is a Queue? 14

15 Queues What is a queue? –A data structure of ordered items such that items can be inserted only at one end and removed at the other end. Example –A line at the supermarket

16 Queues What can we do with a queue? –Enqueue - Add an item to the queue –Dequeue - Remove an item from the queue These ops are also called insert and getFront in order to simplify things.

Queues A queue is called a FIFO (First in-First out) data structure. What are some applications of queues? –Round-robin scheduling in processors –Input/Output processing –Queueing of packets for delivery in networks 17

18 Implementing a Queue Just like a stack, we can implementing a queue in two ways: –Using an array –Using a linked list

19 Implementing a Queue Using an array to implement a queue is significantly harder than using an array to implement a stack. Why? –Unlike a stack, where we add and remove at the same end, in a queue we add to one end and remove from the other.

20 Implementing a Queue There are two options for implementing a queue using an array: Option 1: –Enqueue at data[0] and shift all of the rest of the items in the array down to make room. –Dequeue from data[numItems-1]

21 Implementing a Queue Option 2 –Enqueue at data[rear+1] –Dequeue at data[front] –The rear variable always contains the index of the last item in the queue. –The front variable always contains the index of the first item in the queue. –When we reach the end of the array, wrap around to the front again.

22 Implementing a Queue // option 2 sketch of insert insert(Object item) { if(manyItems == 0) front = rear = 0; else rear = (rear + 1) mod size; data[rear] = item; manyItems++; }

23 Implementing a Queue // option 2 sketch of getFront Object getFront() { answer = data[front]; front = (front + 1) mod size; manyItems--; return answer }

24 Implementing a Queue Implementing a queue using a linked list is still easy: –Front of the queue is stored as the head node of the linked list, rear of the queue is stored as the tail node. –Enqueue by adding to the end of the list –Dequeue by removing from the front of the list.