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.

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.
Stack & Queues COP 3502.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Chapter 12: Data Structures
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
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 Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
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.
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,
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
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.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
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.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
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.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
QueueStack CS1020.
Chapter 15 Lists Objectives
Chapter 12: Data Structures
Stacks and Queues.
Basic Data Types Queues
Building Java Programs
i206: Lecture 11: Stacks, Queues
structures and their relationships." - Linus Torvalds
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
i206: Lecture 10: Lists, Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Lecture 21 Stacks and Queues Richard Gesick.
Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we.
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.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures and Algorithms for Information Processing
Stacks and Queues.
Lecture 2: Stacks and Queues
Abstract Data Type (ADT)
structures and their relationships." - Linus Torvalds
Lecture 9: Stack and Queue
DATA STRUCTURES IN PYTHON
Presentation transcript:

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 make it clear how the list is to be used. A lot of languages even provide special kinds of variables for these special cases.

Stacks and Queues What we’re going to talk about in the following slides are not always going to be python things. We are going to talk about the idea of what a stack and a queue is. The way we actually implement them in python will be included separately.

Queues A queue is a special kind of list where we can only perform the following operations: enqueue(someItem) – puts some item at the end of a queue. dequeue() – removes and returns the item at the start of the queue. These two operations are what makes something a queue!

Queues What this means: The result of only having these two operations is that the whatever you remove is always the thing that’s been in the queue longest! Imagine a queue like a line at the grocery store—whoever has been in line the longest gets to go first. enqueue() is like someone new getting line. dequeue() is the person at the front of the line checking out. Queues are referred to as “First in first out,” or “FIFO”.

Queues In Python How to use a python list as a queue: In python, we don’t have functions named enqueue and dequeue. Instead, we have insert(0, myItem), which adds something to the beginning of the list, and a function called pop(), which removes and returns the endof the list. If these are the only two things we are using to modify the list, it’s a queue!

Stacks A stack supports two operations: push(someItem) – puts something at the end of the stack. pop() – returns the item at the end of the stack.

Stacks Having only these two functions gives us a behavior where whenever we remove something, it’s the thing we put on the stack most recently. Imagine a stack of plates. push() is analogous to putting a plate on the top of the stack, pop() is like taking that plate back off. Stacks are referred to as “Last in first out”, or “LIFO”.

Stacks in Python If we want to simulate a stack in python, we can use append() to add something to the stack, and pop() to remove something. pop() will always return exactly what append just added.