queues n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Queues PowerPoint Presentation
Download Presentation
Queues

Loading in 2 Seconds...

play fullscreen
1 / 15

Queues - PowerPoint PPT Presentation


  • 116 Views
  • Uploaded on

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 Network packets Unlike a stack, both ends are used One for adding new elements One for removing elements

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 'Queues' - royce


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
queues
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

Network packets

  • Unlike a stack, both ends are used
    • One for adding new elements
    • One for removing elements
  • The last element has to wait until all elements preceding it on the queue are removed
    • For this reason, it is a First in/First out (FIFO) structure
queues as adts
Queues as ADTs
  • Data
    • A collection of elements of the same type
  • Operations
    • clear()
    • isEmpty()
    • enqueue(el)
    • dequeue()
    • firstEl()
queues as adt s cont d
Queues as ADT’s (cont’d)
  • Implementation
    • With a doubly linked list, similar to stack, code in book
priority queues
Priority Queues
  • In many situations, FIFO scheduling must be overruled using some priority criteria
    • E.g., a handicapped person that arrives to a post office may have priority over others and may not have to wait in line
    • In a priority queue, elements are dequeued according to their priority and their current queue position
implementation of a priority queue
Implementation of a priority queue
  • Linked list implementation of a priority queue
    • If list is ordered, adding an element is O(n), searching for highest/lowest-priority element is O(1)
    • Can also have list of list
    • If list is unordered, adding an element is O(1), searching for highest/lowest-priority element is O(n)
implementing the stack queue as an adt
Implementing the Stack, Queue As an ADT
  • there are different ways for the Stack Queue Implementation, the pros and cons?
  • Possible Ways of Implementation.
    • Dynamic array based implementation.
      • Dynamic Array (1)
      • Circular Array (2)
    • Linked list implementation
      • The implementation is like a linked list but with only the necessary methods (3)
      • Reuse a Linked List object (book) (4)
      • Inheritance from a Linked List
queue dynamic array implementation
Queue: Dynamic Array Implementation

#pragma once

#include <iostream>

#include <cassert>

template <class T>

class Queue

{

public:

Queue(size_t capacity = 1000);

~Queue();

bool IsEmpty() const;

bool IsFull() const;

// Add a value to the end of the Queue.

void Enqueue(const T& value);

// Remove and return value at front of Queue.

T Dequeue();

// Retrieve value at front of Queue without removing it.

T Front() const;

queue h
Queue.h

// Display Queue contents.

void Display(std::ostream& out) const;

void Clear();

private:

T* data;

int size;

int head;

int tail;

};

dynamic array implementation 1
Dynamic Array Implementation(1)
  • When new jobs enter queue, may run out of space.
  • The array may not really be full though, if items have been removed from queue.

Possible Solution: When tail = (maxSize – 1) attempt to shift data forwards into empty spaces and then do Add.

worst case queue shift
Worst Case Queue Shift
  • Worst Case:
    • Shift entire queue: Cost of O(n-1)
    • Do every time perform an add
    • Too expensive to be useful

Worst case is not that unlikely, so this suggests finding an alternative implementation.

circular array implementation 2
‘Circular’ Array Implementation (2)

Basic Idea: Allow the queue to wrap-around

Implement with addition mod size:

tail = (tail + 1) % queueSize;

4

4

J4

3

3

J3

J2

J1

2

2

N-2

J1

J2

N-2

J3

1

N-1

1

N-1

0

0

linked queues 3
Linked Queues (3)

Class QueueNode{

public:

QueueNode(int d, QueueNode * l);

int data;

QueueNode *link;

};

class Queue

{

public:

Queue();

~Queue();

void enqueue(const int);

int* dequeue(int&);

bool isEmpty();

private:

QueueNode* head;

QueueNode* tail;

void QueueEmpty();

}

Implementation is similar to linked-list, addToTail, deleteFromHead

queues as adt s 4
Queues as ADT’s (4)
  • Reuse a doubly linked list, similar to stack, code in book

class Queue

{

public:

Queue();

~Queue();

void enqueue(const int el) {lst.addToTail(el);}

int* dequeue(int&);

bool isEmpty();

private:

DLList lst;

}

queue adt implementations
Queue ADT Implementations
  • No matter how queue is implemented,

-different internal representation,

- different implementation of the methods

  • the users that use the Queue class will see the same functionalities
  • However, the efficiency of different operation might be different
  • Analyze them. What about priority queue?