Data Structures - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Data Structures

Description:

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT) – PowerPoint PPT presentation

Number of Views:184
Avg rating:3.0/5.0
Slides: 33
Provided by: Muha8
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
  • Lecture 13 QUEUES
  • Azhar Maqsood
  • NUST Institute of Information Technology (NIIT)

2
  • A queue is an ordered collection of items from
    which items may be deleted at one end (called the
    front of the queue) and into which items may be
    inserted at the other end (called the rear of
    the queue)

3
Definitions
  • Data structure which implements a first-in,
    first-out list e.g. print queue, which contains
    a list of jobs to be printed in order.
  • In programming, a queue is a data structure in
    which elements are removed in the same order they
    were entered. This is often referred to as FIFO
    (first in, first out).
  • In contrast, a stack is a data structure in which
    elements are removed in the reverse order from
    which they were entered. This is referred to as
    LIFO (last in, first out).

4
Introduction to Queues
  • The queue data structure is very similar to the
    stack.
  • In a stack, all insertions and deletions occur at
    one end, the top, of the list.
  • In the queue, as in the stack, all deletions
    occur at the head of the list.
  • However, all insertions to the queue occur at the
    tail of the list.

5
Introduction to Queues
  • Basically, data enters the queue at one end and
    exits at the other end.

6
A B C
Front
Rear
B C
Front
Rear
7
Introduction to Queues
  • You get in line at the end and get serviced when
    you get to the front of the line.
  • This characteristic gives a queue its first-in,
    first-out (FIFO) behavior.

8
Applications
  • Ticketing counter
  • Bus stop line
  • Bank Customers
  • Printer SPOOL
  • CPU Scheduling (at times)
  • Etc etc etc

9
Queue Operations
  • Initialize the queue, Q, to be the empty queue.
  • Determine whether or not if the queue Q is empty.
  • Determine whether or not if the queue Q is full.
  • Insert (enqueue) a new item onto the rear of the
    queue Q.
  • Remove (dequeue) an item from the front of Q,
    provided Q is nonempty.

10
Queue Operations
  • isEmpty() check to see if the queue is empty.
  • isFull() check to see if the queue is full.
  • enqueue(element) - put the element at the end of
    the queue.
  • dequeue() take the first element from the
    queue.
  • first() return the first element in the queue
    without removing it.

11
Enqueue
  • The queue insert is known as enqueue.
  • After the data has been inserted, this new
    element becomes the rear of the queue.

12
Dequeue
  • The queue delete operation is known as dequeue.
  • The data at the front of the queue is returned to
    the user and deleted from the queue.

13
Array Implementation
Any implementation of a queue requires storage
for the data as well as markers (pointers) for
the front and for the back of the queue. An
array-based implementation would need structures
like items, an array to store the elements of
the queue Front, an index to track the front
queue element Rear, an index to track the
position following last queue element Additions
to the queue would result in incrementing Rear.
Deletions from the queue would result in
incrementing Front. Clearly, wed run out of
space soon!
14
Queue using Arrays
  • define length 10
  • Struct queue
  • int itemslength
  • int front, rear
  • Insert(q,x)
  • q.itemsq.rearx
  • Xremove(q)
  • xq.itemsq.front

15
4
4
3
3
2
C
q.rear2
2
1
1
B
0
0
A
q.front0
q.front0
q.rear-1
  • The queue is empty whenever q.rearltq.front
  • The number of elements in the queue at any time
    is equal to the value of q.rear q.front 1

16
4
4
E
q.rear4
D
3
3
C
C
q.front2
2
2
q.front
q.rear2
1
1
0
0
  • Now there are 3 elements the queue but there is
    room for 5
  • If to insert F in the queue the q.rear must be
    increased by 1 to 5 and q. items5 must be set
    to the value F. but q.items is an array of only 5
    elements so this insertion cannot be made

17
Solution to Problem
  • Clearly, wehave run out of space
  • Solutions include
  • Shifting the elements downward with each
    deletion
  • Viewing array as a circular buffer, i.e. wrapping
    the end to the front

18
Solution 1
  • For arrays there are two methods
  • First is do it as we do in real world
  • Check if array is not empty
  • Simply dequeue from the first location of array
    say array0 i.e. the zeroth index
  • After dequeue shift all the elements of the array
    from arrayindex to arrayindex-1

19
Dequeue
0
1
2
3
4
A B C D
Rear
0
1
2
3
4
B C D
DE-QUEUE
Rear
20
Dequeue Operation
  • xq.items0
  • for(i0 iltq.rear i)
  • q.itemsiq.itemsi1
  • q.rear--
  • Method is costly as we have to move all the
    elements of the array
  • Do we need Front Index in this case?
  • No, Because we are always dequeue(ing) from the
    first index

21
Solution2 Circular Queue
  • To avoid the costly operation of coping all the
    elements again we employ another method called
    circular queue
  • Simply dequeue the element and move the front
    pointer to next index
  • If q.rearq.front queue is empty
  • q.frontq.rearmax_size-1

MAX_SIZE 6
q3
q4
q5
q0
q1
q2
?
?
?
?
?
?
22
Implementation
  • Struct queue
  • int itemslength
  • int front, rear
  • Struct queue q
  • q.frontq.rearMax_size 1
  • bool IsEmpty(struct queue pq)
  • return(pq-gtfrontpq-gtrear)
  • int remove (struct queue pq)
  • if (empty(pq))
  • coutltltqueue underflow
  • return 0
  • If (pq-gtfrontMax_size-1)
  • pq-gtfront0
  • else
  • (pq-gtfront)
  • Return(pq-gtitemspq-gtfront)

23
Implementation cont.
  • int insert(struct queue pq, int x)
  • //make room for new element
  • If (pq-gtrearMax_size-1)
  • pq-gtrear0
  • else
  • (pq-gtrear)
  • //Check for overflow
  • If (pq-gtrearpq-gtfront)
  • coutltltqueue overflow
  • exit(1)
  • pq-gtitemspq-gtrearx
  • return

24
Queue Operations
q3
q4
q5
q0
q1
q2
q3
q4
q5
q0
q1
q2
?
?
?
?
?
?
?
?
?
A
?
?
q3
q4
q5
q0
q1
q2
q3
q4
q5
q0
q1
q2
?
?
?
A
D
?
?
?
?
A
D
T
25
Queue Operations cont.
q3
q4
q5
q0
q1
q2
q3
q4
q5
q0
q1
q2
E
?
?
A
D
T
E
?
?
A
D
T
q3
q4
q5
q0
q1
q2
q3
q4
q5
q0
q1
q2
E
?
?
A
D
T
E
?
?
A
D
T
26
Queue Operations cont.
q3
q4
q5
q0
q1
q2
q3
q4
q5
q0
q1
q2
E
X
?
A
D
T
E
X
A
A
D
T
wrap-around
q3
q4
q5
q0
q1
q2
E
X
A
M
D
T
27
Circular Queue
3 2
3
2
J2
J3
1 4
1 4
J1
0 5
0 5

Can be seen as a circular queue
28
Problems with above solution
  • How to know if the queue is empty
  • How to know if the queue is full
  • What is the relation between front and back when
    queue is full?
  • Solution
  • Keep it simple add counter to the queue
  • Keep an empty slot between Front and Rear i.e.,
    items array uses QUEUE_CAPACITY - 1 elements

29
Solution (a)
  • define MAXQUEUESIZE 100
  • struct
  • char personName25
  • int personNIC //lets assume NIC to be integer
    type.
  • Person
  • struct
  • int Count / number of queue items /
  • int Head / head of queue /
  • int Tail / tail of queue /
  • Person ItemsMAXQUEUESIZE
  • Queue

30
Solution (a) cont.
  • void InitializeQueue(Queue Q)
  • Q-gtCount 0 / zero count of items /
  • Q-gtHead MAXQUEUESIZE-1
  • Q-gtTail MAXQUEUESIZE-1
  • int Empty(Queue Q)
  • return (Q-gtCount 0)
  • int Full(Queue Q)
  • return (Q-gtCount MAXQUEUESIZE)

31
Solution bLeave one empty space when queue is
full
FULL QUEUE FULL QUEUE
2 3
2 3
J8 J9
J2 J3
J7
1
41 4
J1 J4
J6 J5
J5
0 5 0
5
front 0 rear 5
front 4 rear 3
32
EMPTY QUEUE
3 2
3
2
J2
J3
1 4
1 4
J1
0 5
0 5
front 0
front 0 rear
0
rear 3
Write a Comment
User Comments (0)
About PowerShow.com