Queues l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Queues PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

Queues. Chapter 4. 4.2 Queues . It is a data structure in which the elements are added at one end, called the rear , and deleted from the other end, called the front or first . A queue is simply a waiting line that grows or shrinks by adding or taking elements form it.

Download Presentation

Queues

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 l.jpg

Queues

Chapter 4


4 2 queues l.jpg

4.2 Queues

  • It is a data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front or first.

  • A queue is simply a waiting line that grows or shrinks by adding or taking elements form it.

  • Unlike stack, it’s a structure in which both ends are used.

  • A queue is an FIFO structure.

  • One possible queue implementation is an array(circular array).

  • A more natural queue implementation is a doubly linked list.


Slide3 l.jpg

  • Operations to manage the queue are:

    • Clear()

    • isEmpty()

    • enqueue(el)

    • dequeue()

    • firstEl()


Basic operations on a queue l.jpg

Basic Operations on a Queue

  • InitializeQueue:

    • Initializes the queue to an empty state

  • DestroyQueue:

    • Removes all the elements from the queue, leaving the queue empty

  • IsEmptyQueue:

    • Checks whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value false

  • IsFullQueue:

    • Checks whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false


Basic operations on a queue5 l.jpg

Basic Operations on a Queue

  • Front:

    • Returns the front (first) element of the queue; the queue must exist

  • Back:

    • Returns the last (rear) element of the queue; the queue must exist

  • AddQueue:

    • Adds a new element to the rear of the queue; the queue must exist and must not be full.

  • DeleteQueue:

    • Removes the front element of the queue; the queue must exist and must not be empty.


Slide6 l.jpg

  • A queue is a linear list in which data can be inserted at one end, called rear, and deleted from the other end, called the front. It is a first in-first out (FIFO) data structure.

    • no search,

    • no adding in arbitrary positions,

    • no sorting,

    • no access to anything beyond the front and rear elements.


Basic queue operations l.jpg

Basic Queue Operations

  • Enqueue:

    • inserts an element at the rear of the queue.

grape

Data

Enqueue

kiwi

kiwi

grape

apple

apple

rear

front

rear

front

queue

queue

operation


Basic queue operations cont l.jpg

Basic Queue Operationscont’

  • Dequeue:

    • deletes element at the front of the queue.

apple

Data

Dequeue

kiwi

grape

kiwi

grape

apple

rear

front

front

rear

queue

queue

operation


Basic queue operations cont9 l.jpg

Basic Queue Operationscont’

  • Queue Front:

    • Examines the element at the front of the queue.

apple

Data

Queue Front

kiwi

grape

kiwi

grape

apple

apple

rear

rear

front

front

queue

operation


Basic queue operations cont10 l.jpg

Basic Queue Operationscont’

  • Queue Rear:

    • Examines the element at the rear of the queue.

grape

Data

Queue Rear

kiwi

grape

kiwi

grape

apple

apple

rear

rear

front

front

queue

operation


Queue linked list design l.jpg

Queue Linked List Design

  • For a linked list implementation of a queue, we use two types of structures: a head and a node.

kiwi

grape

fig

apple

rear

front

Conceptual queue

4

count

front

rear

kiwi

fig

apple

grape

Physical queue


Queue linked list design cont l.jpg

Queue Linked List Design cont’

queueHead

front<node pointer>count<integer>

rear<node pointer>

end queueHead

node

data<datatype>

next<node pointer>

end node

4

count

front

rear

Queue head structure

data

next

node structure


Queue algorithms l.jpg

Queue Algorithms

  • Create Queue

algorithm createQueue

  • queue.front = null

  • queue.rear = null

  • queue.count = 0

    end createQueue

0

count

front

rear

Queue head structure


Slide14 l.jpg

Enqueue

count

count

front

rear

front

rear

Queue

Queue

0

1

plum

plum

data

next

newPtr

data

next

newPtr

Before

After

Case 1: insert into null queue

count

count

front

rear

front

rear

Queue

Queue

1

2

newPtr

plum

kiwi

plum

data

next

data

next

data

next

kiwi

Before

After

data

next

newPtr

Case 2: insert into queue


Slide15 l.jpg

algorithm enqueue

Insert (push) data into a queue.

PostdataIn has been inserted

Return true if successful, false if overflow

  • If (queue full)

    • return false

  • end if

  • allocate (newptr)

  • newptr->data = dataIn

  • newptr->next = null pointer

  • if (queue.count = zero)

    • queue.front = newPtr

  • else

    • queue.rear->next = newPtr

  • end if

  • queue.rear = newptr

  • queue.count = queue.count + 1

  • return true

  • end enqueue

  • Enqueue


Slide16 l.jpg

Dequeue

count

front

rear

count

front

rear

Queue

Queue

1

0

plum

plum

(Recycled)

data

next

data

next

deleteLoc

After

Before

Case 1: delete only item in queue

count

count

front

rear

front

rear

Queue

Queue

2

1

plum

plum

kiwi

kiwi

(Recycled)

data

next

data

next

data

next

data

next

deleteLoc

Before

After

Case 2: delete item at front of queue


Slide17 l.jpg

algorithm dequeue

This algorithm deletes a node from a queue.

Postdata at front of queue returned to user through item and front element deleted and recycled

Return true if successful, false if overflow

  • If (queue.count is 0)

    • return false

  • end if

  • Item= queue.front->data

  • deleteLoc = queue.front

  • if (queue.count is 1)

    • queue.rear = null pointer

  • end if

  • queue.front = queue.front->next

  • queue.count = queue.count – 1

  • recycle (deleteLoc)

  • return true

  • end dequeue

  • Dequeue


Slide18 l.jpg

algorithm QueueFront

This algorithm receives the data at the front of the queue without changing the queue contents.

Postdata passed back to caller

Return true if successful, false if underflow

  • if (queue.count is 0)

    • return false

  • end if

  • dataout = queue.front->data

  • return true

  • end QueueFront

  • Queue Front


Slide19 l.jpg

  • Empty Queue

algorithm emptyQueue

This algorithm checks to see if a queue is empty.

Return true if empty, false if queue has data

  • return (if queue.countequal 0)

  • end emptyQueue


Slide20 l.jpg

algorithm fullQueue

This algorithm checks to see if a queue is full. The queue is full if memory cannot be allocated for another node.

Return true if full, false if there is room for another node

  • allocate (tempPtr)

  • If (allocation successful)

    • recycle(tempPtr)

    • return false

  • else

    • return true

  • end if

  • end fullQueue

  • Full Queue


Queue count l.jpg

Queue Count

algorithm Queuecount

Returns the number of elements currently in queue.

  • return queue.count

  • end Queuecount


Destroy queue l.jpg

Destroy Queue

algorithm destroyQueue

This algorithm deletes all data from a queue.

Post all data have been deleted and recycled

  • ptr= queue.front

  • Loop (ptrnot null)

    • deletePtr= ptr

    • ptr= ptr->next

    • recycle (deletePtr)

  • end loop

  • queue.front = null

  • queue.rear = null

  • queue.count = 0

  • return

  • end destroyQueue


Priority queues l.jpg

Priority Queues

  • A priority queue is an ADT with an inserting accessing protocol: only the highest-priority element can be accessed.

  • It is arranged to support access to the highest priority.

  • A priority queue stores collection of entries. Each entry is a (key, value) pair.

  • Applications:

    • Hospital Emergency Rooms

    • Stock market

  • Keys in a priority queue can be arbitrary objects on which an order is defined.

  • Two distinct items in a priority queue can have the same key.


Priority queues methods l.jpg

Priority Queues Methods

  • Main methods of the Priority Queue ADT

    • insert(k, x) inserts an entry with key k and value x

    • removeMin() removes and returns the entry with smallest key

  • Additional methods

    • minKey(k, x) returns, but does not remove, an entry with smallest key

    • minElement() returns, but does not remove, the element of an item with smallest key

    • size()

    • isEmpty()


  • Login