meljun cortes data structures queues

21
Data Structures Queue - an ordered list Top/rear – end of the list where all insertion takes place Bottom/front – other end of the list where all deletions take place This type of processing behavior is called FIFO With stack, only one element is "visible" at a given time, while on queue, the "visible” element Queue Queues *Property of STI Page 1 of21 given time, while on queue, the "visible” element will always be the one at the bottom of the queue

Upload: meljun-cortes-mbampa

Post on 17-Jul-2016

225 views

Category:

Documents


3 download

DESCRIPTION

Meljun Cortes Data Structures Queues

TRANSCRIPT

Page 1: Meljun Cortes Data Structures Queues

Data Structures

� Queue - an ordered list

� Top/rear – end of the list where all insertion

takes place

� Bottom/front – other end of the list where all

deletions take place

� This type of processing behavior is called FIFO

� With stack, only one element is "visible" at a

given time, while on queue, the "visible” element

Queue

Queues *Property of STIPage 1 of21

given time, while on queue, the "visible” element

will always be the one at the bottom of the

queue

Page 2: Meljun Cortes Data Structures Queues

Data Structures

� Queue as one-dimensional array

Representation of Queues

Queues *Property of STIPage 2 of21

� Queues are represented as one-dimensional

arrays when:

� The maximum size of the queue is known.

� The amount of time wasted in shifting the

elements after deletion is negligible.

Page 3: Meljun Cortes Data Structures Queues

Data Structures

� Queue as Singly-linked list

� when the maximum size of the queue is

unknown.

Representation of Queues

Queues *Property of STIPage 3 of21

Page 4: Meljun Cortes Data Structures Queues

Data Structures

Operations on Queue

Arrays Stacks Queues

Insert Push Put, add, enqueue

Delete POP Delete, get, dequeue

Search Peek Peek, retrieve

Queues *Property of STIPage 4 of21

� Enqueue - always at the top of the list

Page 5: Meljun Cortes Data Structures Queues

Data Structures

� Dequeue - delete at the bottom of the list

� Peek or Retrieve - print or display the element

at the bottom of the list

� being viewed or retrieve is what you are

about to delete or remove and not what you

have inserted

� Empty and Full

Operations on Queue

Queues *Property of STIPage 5 of21

� Empty and Full

� return you an error because the queue is

empty

� a message that the queue is full when trying

to insert another element

Page 6: Meljun Cortes Data Structures Queues

Data Structures

� Sometimes called as ring buffer

� elements of the array are “arranged” in a

circular manner

Circular Queue

Queues *Property of STIPage 6 of21

Page 7: Meljun Cortes Data Structures Queues

Data Structures

� Initially, top and bottom contain the value zero

(0)

� Insert element “A”, top and bottom will be = 1

Circular Queue

Queues *Property of STIPage 7 of21

Page 8: Meljun Cortes Data Structures Queues

Data Structures

� Insert element “B”, bottom = 1 and top = 2

Circular Queue

Queues *Property of STIPage 8 of21

Page 9: Meljun Cortes Data Structures Queues

Data Structures

� Delete bottom element “A”

Circular Queue

Queues *Property of STIPage 9 of21

Page 10: Meljun Cortes Data Structures Queues

Data Structures

Circular Queue

� Algorithm for inserting element in a circular

queue:

1. If bottom of the queue is pointing to the last

position then proceed to second or else

third step

2. make the bottom value as 0

3. increment the bottom value by one

4. a. if the top points where bottom is pointing

and the queue holds a not NULL value for

Queues *Property of STIPage 10 of21

and the queue holds a not NULL value for

it, then its a "queue overflow" state, so quit;

else go to step-b

b. insert the new value for the queue

position pointed by the bottom

Page 11: Meljun Cortes Data Structures Queues

Data Structures

Circular Queue

{

if(bottom == maxSize -1)

bottom = -1;

queArray[++bottom] = j;

nItems++;

}

� Top = Bottom = 0

Queues *Property of STIPage 11 of21

� insert “A” into the queue

� Top = Bottom = 1

� additional 9 insertions with no deletion is done

� Top = 10

� Bottom = 1

Page 12: Meljun Cortes Data Structures Queues

Data Structures

Circular Queue

� algorithm to delete the element at the top of a

circular queue

1. If the queue is empty then say "empty queue"

and quit; else continue

2. Delete the top element

3. If the top is pointing to the last position of the

queue then step-4 else step-5

4. Make the top point to the first position in the

queue and quit

5. Increment the top position by one

Queues *Property of STIPage 12 of21

5. Increment the top position by one

{

int temp = queArray[top++];

if(top== maxSize)

Top = 0;

nItems--;

return temp;

}

Page 13: Meljun Cortes Data Structures Queues

Data Structures

Java Code for Queue

public void insert(int j)

{

if(rear == maxSize -1)

rear = -1;

queArray[++rear] = j;

nItems++;

}

------------------------------------

public int remove()

Queues *Property of STIPage 13 of21

public int remove()

{

int temp = queArray[front++];

if(front == maxSize)

front = 0;

nItems--;

return temp;

}

-----------------------------------

public int peekFront()

{

return queArray[front];

}

Page 14: Meljun Cortes Data Structures Queues

Data Structures

Priority Queue

� special type of queue where elements in this

queue are ordered, in an ascending or

descending manner

� Operations on Priority Queue

� Insert

� Delete

� Peek and new

Queues *Property of STIPage 14 of21

Priority queue insertion

Priority queue deletion

Page 15: Meljun Cortes Data Structures Queues

Data Structures

Priority Queue

� inserting an item

public void insert(double item)

{

int j;

if(nItems==0)

queArray[nItems++] = item;

else

{

Queues *Property of STIPage 15 of21

{

for(j=nItems-1; j>=0; j--)

{

if(item>queArray[j])

queArray[j+1] =

queArray[j];

else

break;

}

queArray[j+1] = item;

nItems++;

}

}

Page 16: Meljun Cortes Data Structures Queues

Data Structures

Priority Queue

� model of the memory allocation is described in

terms of three (3) queues: the Arrival Queue,

the Execution Queue, and the Memory Queue

� Arrival Queue is a strict FIFO queue

� Data field of each node in the Memory Queue

will be subdivided into a pair of fields

� MemAddr, is a memory address

� MemSize, is the size of contiguous memory

Queues *Property of STIPage 16 of21

� MemSize, is the size of contiguous memory

� Execution Queue, will be used to monitor the

execution of jobs

Page 17: Meljun Cortes Data Structures Queues

Data Structures

Priority Queue

� Data field of each node in the Execution Queue

� For Arrival queue

Queues *Property of STIPage 17 of21

� Represented as Singly-Linked List Nodes:

� As One-Dimensional Array Elements:

Page 18: Meljun Cortes Data Structures Queues

Data Structures

Simulation of Model

� Memory Queue

� Execution Queue

Queues *Property of STIPage 18 of21

� Arrival Queue

Page 19: Meljun Cortes Data Structures Queues

Data Structures

Simulation of Model

� Arrival Queue

� Top = Bottom = 1

Queues *Property of STIPage 19 of21

� Memory Queue

Page 20: Meljun Cortes Data Structures Queues

Data Structures

Simulation of Model

� Execution Queue

Queues *Property of STIPage 20 of21

� Contents of Queue

Page 21: Meljun Cortes Data Structures Queues

Data Structures

Abstract Data Type

� way of looking a data structure on what it does

in a program and not how it does it

� Data Type

� int and double which is applied to built-in

types

� Abstraction

a mechanism and practice to reduce and

Queues *Property of STIPage 21 of21

� a mechanism and practice to reduce and

factor out details so that one can focus on a

few concepts at a time

� Interface

� specification of ADT

� Abstract Data Type

� kind of data abstraction where a type's

internal form is hidden behind a set of

access functions