CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.

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 and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
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.
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.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Chapter 12: Data Structures
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
© 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.”
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.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
C++ STL CSCI 3110.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
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’
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,
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
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.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
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,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Programming Abstractions
STACKS & QUEUES for CLASS XII ( C++).
Chapter 18: Stacks and Queues.
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
Copy Constructor / Destructors Stacks and Queues
Chapter 12: Data Structures
Introduction to Data Structure
Data Structures and Database Applications Queues in C#
Building Java Programs
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
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.
Visit for more Learning Resources
Abstract Data Type (ADT)
Stacks, Queues, and Deques
Data Structures & Programming
Presentation transcript:

CMSC 202 Stacks and Queues

What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the end of the queue and items removed from the queue are removed from the front.

An Analogy Bank Teller Line 1.When you walk into the bank you go to the end of the line. 2.When the teller is ready to help the next person, the person at the beginning of the line is removed from the line. 3.Each person then moves one position closer to the beginning of the line. 4.Eventually, you will be the first person in the line and the teller will call you up causing you to be removed from the line.

What’s it used for? Items waiting to be printed Representing a waiting line in a simulation (traffic light, cafeteria, grocery store checkout, etc.) Passing information from one process to another in the order it arrived

The Queue ADT Description of the data –A linear collection of homogeneous data A description of the operations –Add an element to the end of the queue. This is known as enqueueing. –Remove an element from the front of the queue. This is known as dequeueing. –A queue is a First-In, First-Out (FIFO) data structure What other operations might be helpful?

Queue Implementation Since the Queue contains “homogeneous” data, it should be implemented as a class template. That way, we can create queues that contain any data type or object. Enqueueing seems to be a special kind of inserting. Dequeueing seems to be a special kind of removing. What run-time errors may occur?

Queue.H template class Queue { public: // constructor(s), destructor, operator= enqueue ( );// what is/are the parameter(s) and //return type? dequeue ( );// what is/are the parameter(s) and //return type? private: // data representation };

Using the Queue object main ( ) { Queue iQ; iQ.enqueue ( 7 ); iQ.enqueue ( 42 ); int x = iQ.dequeue ( ); // x = 7; cout << iQ.dequeue ( ) << endl;// prints 42; x = iQ.dequeue ( );// is an error }

What’s the data representation? A queue seems to be very much like a list (in the abstract sense), except with different (or maybe additional) insert and remove functionality.

What’s a stack A stack is a linear collection of homogeneous items, in which an item to be added to the stack must be placed on top of the stack and and an item that removed from the stack must be removed from the top.

Stack Analogy Cafeteria Trays When you go to the UC or dining hall for some scrumptious food, you probably use a tray. Which one do you pick-up? The one on the top of the pile. When the cafeteria work brings out new trays, where does he put them? On top of the pile. Trays are both added and and removed from the top of the pile.

What’s it used for?? Remembering where you were – function call return addresses Reversing the order of “stuff”

The Stack ADT Description of the data –A linear collection of homogeneous data A description of the operations –Add an element to the top (front) of the stack. This is known as pushing. –Remove an element from the top (front) of the stack. This is known as popping. –A stack is a Last-In, First-Out (LIFO) data structure What other operations might be helpful?

Stack Implementation Since the Stack contains “homogeneous” data, it should be implemented as a class template. That way, we can create stacks that contain any data type or object. pushing seems to be a special kind of inserting. poping seems to be a special kind of removing.. What run-time errors may occur?

Stack.H template class Stack { public: // constructor(s), destructor, operator= push ( );// what is/are the parameter(s) and //return type? pop ( );// what is/are the parameter(s) and //return type? private: // data representation };

Using the Stack object main ( ) { Stack iS; iS.push ( 7 ); iS.push ( 42 ); int x = iS.pop ( ); // x = 42; cout << iS.pop ( ) << endl;// prints 7; x = iS.pop ( );// is an error }

What’s the data representation? A stack seems to be very much like a list (in the abstract sense), except with different (or maybe additional) insert and remove functionality.

Stack Example Matching parenthesis, braces and brackets in a C++ program.

A challenge Explain how to simulate a Queue (ie. A FIFO data structure) using two stacks.