C programming program design including data structures fourth edition
Download
1 / 37

C++ Programming: Program Design Including Data Structures, Fourth Edition - PowerPoint PPT Presentation


  • 239 Views
  • Uploaded on

C++ Programming: Program Design Including Data Structures, Fourth Edition. Chapter 18: Stacks and Queues (part 3). Queues. Queue : group of homogeneous elements Elements are: Added at one end (the back or rear) Deleted from the other end (the front)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C++ Programming: Program Design Including Data Structures, Fourth Edition' - lida


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
C programming program design including data structures fourth edition l.jpg

C++ Programming: Program Design Including Data Structures, Fourth Edition

Chapter 18: Stacks and Queues

(part 3)


Queues l.jpg
Queues

  • Queue: group 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Queue operations l.jpg
Queue Operations

  • Some of the queue operations are:

    • initializeQueue

    • isEmptyQueue

    • isFullQueue

    • front

    • back

    • addQueue

    • deleteQueue

  • Abstractclass queueADT defines these operations

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays l.jpg
Implementation of Queues as Arrays

  • You need at least four (member) 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued l.jpg
Implementation of Queues as Arrays (continued)

  • 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued6 l.jpg
Implementation of Queues as Arrays (continued)

  • To delete an element from the queue:

    • Retrieve element pointed to by queueFront

    • Advance queueFront to next queue element

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued7 l.jpg
Implementation of Queues as Arrays (continued)

  • 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...

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued8 l.jpg
Implementation of Queues as Arrays (continued)

  • The sequence AAADADADADADADADA... would eventually set queueRear to point to the last array position

    • Giving the impression that the queue is full

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued9 l.jpg
Implementation of Queues as Arrays (continued)

  • 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued10 l.jpg
Implementation of Queues as Arrays (continued)

  • To advance the index in a (logically) circular array:

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued11 l.jpg
Implementation of Queues as Arrays (continued)

  • Case 1:

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued12 l.jpg
Implementation of Queues as Arrays (continued)

  • Case 2:

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued13 l.jpg
Implementation of Queues as Arrays (continued)

  • Problem:

    • Figures 18-47 and 18-49 have identical values for queueFront and queueRear

    • However, the former represents an empty queue, whereas the latter shows a full queue

  • Solution?

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued14 l.jpg
Implementation of Queues as Arrays (continued)

  • 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Implementation of queues as arrays continued15 l.jpg
Implementation of Queues as Arrays (continued)

  • 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

C++ Programming: Program Design Including Data Structures, Fourth Edition


Empty queue and full queue l.jpg
Empty Queue and Full Queue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Initialize queue l.jpg
Initialize Queue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Front l.jpg
Front

  • Returns the first element of the queue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Slide21 l.jpg
Back

  • Returns the last element of the queue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Addqueue l.jpg
addQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Deletequeue l.jpg
deleteQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Constructors and destructors l.jpg
Constructors and Destructors

C++ Programming: Program Design Including Data Structures, Fourth Edition


Constructors and destructors continued l.jpg
Constructors and Destructors (continued)

  • The array to store the queue elements is created dynamically

    • When the queue object goes out of scope, the destructor simply deallocates the memory occupied by the array

C++ Programming: Program Design Including Data Structures, Fourth Edition


Linked implementation of queues l.jpg
Linked Implementation of Queues

  • Array size is fixed:only a finite number of queue elements can be stored in it

  • The array implementation of the queue requires array to be treated in a special way

    • Together with queueFront and queueRear

  • The linked implementation of a queue simplifies many of the special cases of the array implementation

    • In addition, the queue is never full

C++ Programming: Program Design Including Data Structures, Fourth Edition


Linked implementation of queues continued l.jpg
Linked Implementation of Queues (continued)

  • Elements are added at one end and removed from the other

    • We need to know the front of the queue and the rear of the queue

      • Two pointers: queueFront and queueRear

C++ Programming: Program Design Including Data Structures, Fourth Edition


Empty and full queue l.jpg
Empty and Full Queue

  • The queue is empty if queueFront is NULL

  • The queue is never full

C++ Programming: Program Design Including Data Structures, Fourth Edition


Initialize queue31 l.jpg
Initialize Queue

  • Initializes queue to an empty state

    • Must remove all the elements, if any

C++ Programming: Program Design Including Data Structures, Fourth Edition


Addqueue32 l.jpg
addQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Front and back operations l.jpg
front and back Operations

C++ Programming: Program Design Including Data Structures, Fourth Edition


Deletequeue34 l.jpg
deleteQueue

C++ Programming: Program Design Including Data Structures, Fourth Edition


Default constructor l.jpg
Default Constructor

C++ Programming: Program Design Including Data Structures, Fourth Edition


Summary l.jpg
Summary

  • Stack: items are added/deleted from one end

    • Last In First Out (LIFO) data structure

    • Operations: push, pop, initialize, destroy, check for empty/full stack

    • Can be implemented as array or linked list

    • Middle elements should not be accessed

  • Postfix notation: operators are written after the operands (no parentheses needed)

C++ Programming: Program Design Including Data Structures, Fourth Edition


Summary continued l.jpg
Summary (continued)

  • Queue: items are added at one end and removed from the other end

    • First In First Out (FIFO) data structure

    • Operations: add, remove, initialize, destroy, check if queue is empty/full

    • Can be implemented as array or linked list

    • Middle elements should not be accessed

    • Restricted versions of arrays and linked lists

C++ Programming: Program Design Including Data Structures, Fourth Edition


ad