Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

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.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
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.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Data Structure Dr. Mohamed Khafagy.
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
DATA STRUCTURE & ALGORITHMS
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
Linear Data Structures
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Queues Chapter 4.
QUEUES.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
CMSC 341 Lecture 5 Stacks, Queues
DATA STRUCTURE QUEUE.
CE 221 Data Structures and Algorithms
CSC 248 – fundamentals of Data structure
Stacks, Queues, and Deques
Data Structures Using C++ 2E
Getting queues right … finally (?)
Presentation transcript:

scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Topics Stacks Queues Final Exam Pointers = Punteros 2

Key Terms Stack – Pilas ▫Push – Insertar ▫Pop - Quitar Queue – Colas ▫Enque – Insertar ▫Dequeue - Eliminar 3

Stacks 4

Stack with Arrays Element 1 can go in first array position, the second in the second position, etc. The top of the stack is the index of the last element added to the stack Stack elements are stored in an array Stack element is accessed only through top To keep track of the top position, use a variable called Top 5

Stack using an Array 6

Stacks If Top = 0, the stack is empty If Top = MAX, stack is full Store the newItem in the array component indicated by Top Increment Top Must avoid an overflow (or underflow) 7

Queue Queue: list of homogeneous elements Elements are: ▫Added at one end (the back or rear) ▫Deleted from the other end (the front) First In First Out (FIFO) data structure ▫Middle elements are inaccessible Example: ▫Waiting line in a bank 8

Queue Operations ▫ initializeQueue ▫ isEmptyQueue ▫ isFullQueue ▫ front ▫ back ▫ addQueue ▫ deleteQueue 9

Queue implementation You need at least four variables: ▫An array to store the queue elements ▫ queueFront and queueRear  To keep track of first and last elements ▫ maxQueueSize  To specify the maximum size of the queue 10

Adding elements 11 To add an element to the queue: ▫Advance queueRear to next array position ▫Add element to position pointed by queueRear Example: array size is 100; originally empty

Deleting Elements 12 To delete an element from the queue: ▫Retrieve element pointed to by queueFront ▫Advance queueFront to next queue element

Implementation of Queues as Arrays (cont'd.) Will this queue design work? ▫Suppose A stands for adding an element to the queue ▫And D stands for deleting an element from the queue ▫Consider the following sequence of operations:  AAADADADADADADADA...

Implementation of Queues as Arrays (cont'd.) The sequence AAADADADADADADADA... would eventually set queueRear to point to the last array position ▫Giving the impression that the queue is full

Implementation of Queues as Arrays (cont'd.) Solution 1: ▫When the queue overflows to the rear (i.e., queueRear points to the last array position):  Check value of queueFront  If value of queueFront indicates that there is room in the front of the array, slide all of the queue elements toward the first array position Problem: too slow for large queues Solution 2: assume that the array is circular

Implementation of Queues as Arrays (cont'd.) To advance the index in a (logically) circular array:

Implementation of Queues as Arrays (cont'd.) 17

Implementation of Queues as Arrays (cont'd.) Case 1:

Implementation of Queues as Arrays (cont'd.) Case 2:

Implementation of Queues as Arrays (cont'd.) Problem: ▫Figures 19-32b and 19-33b have identical values for queueFront and queueRear ▫However, the former represents an empty queue, whereas the latter shows a full queue Solution?

Implementation of Queues as Arrays (cont'd.) Solution 1: keep a count ▫Incremented when a new element is added to the queue ▫Decremented when an element is removed ▫Initially, set to 0 ▫Very useful if user (of queue) frequently needs to know the number of elements in the queue We will implement this solution

Implementation of Queues as Arrays (cont'd.) Solution 2: let queueFront indicate index of the array position preceding the first element ▫ queueRear still indicates index of last one ▫Queue empty if:  queueFront == queueRear ▫Slot indicated by queueFront is reserved  Queue can hold 99 (not 100) elements ▫Queue full if the next available space is the reserved slot indicated by queueFront

Implementation of Queues as Arrays (cont'd.) 23

Summary Stacks and Queues are important data structures Can be implemented as arrays or linked lists We will see in OOP that we can create Abstract Data Types to protect the integrity of the data 24

Final Exam 8 questions from material covered since the mid term Answer all 8 questions; equal weight Submit to WorldClass week 8 Due by Monday midnight (12/16/13) Must have all late submissions by Sunday midnight to get credit 25