Stacks, Queues Bryce Boe 2013/07/30 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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  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.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
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 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Sorting, Stacks, Queues Bryce Boe 2013/11/06 CS24, Fall 2013.
Data Structures - Queues
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
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.
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’
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
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.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
April 27, 2017 COSC Data Structures I Review & Final Exam
Linear Data Structures
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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,
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Implementing Queues Eric Roberts CS 106B February 13, 2013.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Review Array Array Elements Accessing array elements
COSC160: Data Structures: Lists and Queues
Queues Queues Queues.
Stack and Queue APURBO DATTA.
HW-6 Deadline Extended to April 27th
Basic Data Types Queues
CMSC 341 Lecture 5 Stacks, Queues
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 2: Stacks and Queues
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Stacks, Queues Bryce Boe 2013/07/30 CS24, Summer 2013 C

Outline Stacks Queues

STACKS

Stack A First-in Last-out data structure (FILO) – Operates like a stack of papers Operations – void push(T item) Add an item to the stack – T pop() Remove and return the most recently added item from the stack

Linked-List Implementation push(item) – Use insert(0, item) for a O(1) pop(item) – Use remove_at(0) for a O(1)

Array-based implementation push(item) – Use insert_at(-1, item) for an O(1) insertion – O(n) when the array must expand pop() – Use remove_at(-1) for an O(1) removal

QUEUES

Queues What is a queue? – A data structure that allows access to items in a first in, first out manor (FIFO) What are its operations? – enqueue (add to the queue) – dequeue (remove the oldest item in the queue) What are some example queues? – Waiting in line, task scheduling, data buffering

Linked List Implementation Stacks add and remove from the same side, thus it makes sense to add an remove from opposite sides BUT, adding and removing from the end of the list is O(n)

Make the linked list smarter Add a tail pointer – Gives us immediate access to the end of the list – Can we improve these functions’ efficiency? insert_at(-1, item)? remove_at(-1)? YES NO

Linked-List Implementation enqueue(item) – Use insert(-1, item) for a O(1) Assumes we have a working tail pointer in the list dequeue(item) – Use remove_at(0) for a O(1)

Array-based implementation To implement an unbounded queue on top of the array-based implementation of a list requires treating the array as circular Rather than using 0 as a base index, the queue needs to keep track of which index should be the base, and use modular arithmetic to wrap around When the array needs to grow, the values must be manually “reset” such that the base index is at the zero position

Array-based implementation enqueue(item) – Use insert_at((BASE + size) % allocated, item) for an O(1) operation dequeue(item) – Use remove_at(BASE) for an O(1) operation and make sure to increment the BASE

Problems we can now solve Write a program to determine if a given text is a palindrome: – racecar, stats – poordanisinadroop Take a few minutes to solve it with your neighbor

Palindrome Solution bool is_palindrome(char *word) { Queue queue; Stack stack; int index = 0; //iterate through the word adding to the queue while(word[index] != ‘\0’) { stack.push(word[index]); queue.enqueue(word[index++]); } while(!queue.is_empty()) if (stack.pop() != queue.dequeue() return false; return true; }