INFSCI 0015 Data Structures Lecture 18: Queues - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 18: Queues

Description:

... Add an item to the end of queue. Overflow. Dequeue - Remove an ... printf('4. Rearn'); printf('5. Fulltt'); printf('6. Emptyn'); printf('7. Endnn' ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 18: Queues


1
INFSCI 0015 - Data StructuresLecture 18 Queues
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Figure 5-1
Queues in Our Life
  • A queue is a FIFO structure Fast In First Out

3
Basic Operations with Queues
  • Enqueue - Add an item to the end of queue
  • Overflow
  • Dequeue - Remove an item from the front
  • Could be empty
  • Queue Front - Who is first?
  • Could be empty
  • Queue End - Who is last?
  • Could be empty

4
Figure 5-2
Enqueue
5
Figure 5-3
Dequeue
6
Figure 5-4
Queue Front
7
Figure 5-5
Queue Rear
8
Figure 5-6
9
Figure 5-15
Queue implmentation with arrays
10
Figure 5-16
Queue will overgrow the array
  • Should we use VERY L A R G E ARRAYS?

11
(No Transcript)
12
Array implementation of queues
queueAry
maxsize
count
front
rear
7
4
1
5
front
rear
11
37
22
15
3
-7
1
13
Storage for a queue
  • struct intqueue
  • int queueAry
  • int maxSize
  • int count
  • int front
  • int rear

14
Create Queue
  • struct intqueue createQueue (int maxSize)
  • struct intqueue queue
  • queue (struct intqueue ) malloc (sizeof
    (struct intqueue))
  • if (queue ! NULL)
  • / head structure created. Now allocate queue /
  • queue-gtqueueAry (int )malloc(maxSize
    sizeof(int))
  • queue-gtfront -1
  • queue-gtrear -1
  • queue-gtcount 0
  • queue-gtmaxSize maxSize
  • return queue
  • / createQueue /

15
Enqueue
  • int enqueue (struct intqueue queue, int
    datain)
  • if (queue-gtcount queue-gtmaxSize)
  • return 0 / queue is full /
  • (queue-gtrear)
  • if (queue-gtrear queue-gtmaxSize)
  • / Queue wraps to element 0 /
  • queue-gtrear 0
  • queue-gtqueueAryqueue-gtrear datain
  • if (queue-gtcount 0)
  • / Inserting into null queue /
  • queue-gtfront 0
  • queue-gtcount 1
  • / if /
  • else (queue-gtcount)
  • return 1
  • / enqueue /

16
Dequeue
  • int dequeue (struct intqueue queue, int
    dataOutPtr)
  • if (!queue-gtcount)
  • return 0
  • dataOutPtr queue-gtqueueAryqueue-gtfront
  • (queue-gtfront)
  • if (queue-gtfront queue-gtmaxSize)
  • / queue front has wrapped to element 0 /
  • queue-gtfront 0
  • if (queue-gtcount 1)
  • / Deleted only item in queue /
  • queue-gtrear queue-gtfront -1
  • (queue-gtcount)--
  • return 1
  • / dequeue /

17
queueFront
  • int queueFront (struct intqueue queue,
  • int dataOutPtr)
  • if (!queue-gtcount)
  • return 0
  • else
  • dataOutPtr queue-gtqueueAryqueue-gtfront
  • return 1
  • / else /
  • / queueFront /

18
queueRear
  • int queueRear (struct intqueue queue,
  • int dataOutPtr)
  • if (!queue-gtcount)
  • return 0
  • else
  • dataOutPtr queue-gtqueueAryqueue-gtrear
  • return 1
  • / else /
  • / queueRear /

19
emptyQueue and fullQueue
  • int emptyQueue (struct intqueue queue)
  • / Statements /
  • return (queue-gtcount 0)
  • / emptyQueue /
  • int fullQueue (struct intqueue queue )
  • return ( queue-gtcount queue-gtmaxSize)
  • / fullQueue /

20
destroyQueue
  • struct intqueue destroyQueue (struct intqueue
    queue)
  • / Statements /
  • if (queue)
  • free (queue-gtqueueAry)
  • free (queue)
  • / if /
  • return NULL
  • / destroyQueue /

21
Example 18.1 Driver (1)
  • include "QueueIntAr.h"
  • include ltstdio.hgt
  • void main()
  • struct intqueue myqueue
  • int i
  • int value
  • myqueue createQueue(5)
  • while(1)
  • printf("Queue manipulation\n\n")
  • printf("1. Enqueue\t")
  • printf("2. Dequeue\n")
  • printf("3. Front\t")
  • printf("4. Rear\n")
  • printf("5. Full\t\t")
  • printf("6. Empty\n")
  • printf("7. End\n\n")

22
Example 18.1 Driver (2)
  • if(i 1)
  • printf("Value? ")
  • scanf("d",value)
  • if(enqueue(myqueue, value))
  • printf("Value d enqueued in\n", value)
  • else
  • printf("Queue is Full\n")
  • else if (i 2)
  • if(dequeue(myqueue, value))
  • printf("Value d dequeued\n", value)
  • else
  • printf("Queue is Empty\n")
  • else if (i 3)
  • if (queueFront(myqueue, value))
  • printf("Front Value is d\n", value)
  • else
  • printf("Queue is Empty\n")

23
Example 18.1 Driver (3)
  • else if (i 4)
  • if (queueRear(myqueue, value))
  • printf("End Value is d\n", value)
  • else
  • printf("Queue is Empty\n")
  • else if (i 5)
  • if (fullQueue(myqueue))
  • printf("Queue is Full\n")
  • else
  • printf("Queue is Not Full\n")
  • else if (i 6)
  • if (emptyQueue(myqueue))
  • printf("Queue is Empty\n")
  • else
  • printf("Queue is Not Empty\n")
  • else
  • destroyQueue(myqueue)
  • return

24
Example 19.2 Using Queues
  • include "QueueIntAr.h"
  • include ltstdio.hgt
  • define N 10 / dimension of the array /
  • void readarray(int ar, int n)
  • void filter_array(int ar, int n)
  • void printarray(int ar, int n)
  • void main()
  • int testarrayN / elements from ar0 to
    arN-1 /
  • readarray(testarray, N)
  • printf("Before filtering, numbers are ")
  • printarray(testarray, N)
  • filter_array(testarray, N)
  • printf("After filtering, numbers are ")
  • printarray(testarray, N)

25
Example 19.2
  • / filter integer array by removing negative
    numbers /
  • void filter_array(int ar, int n)
  • int i
  • struct intqueue q
  • q createQueue(N)
  • for (i 0 i lt n i)
  • if (ari gt 0)
  • enqueue(q, ari)
  • for (i 0 i lt n i)
  • if (emptyQueue(q))
  • ari 0
  • else dequeue(q, ari)
  • destroyQueue(q)
  • return

26
How do we work with queues?
  • We know a conceptual idea of a queue
  • We can use a set of operations
  • We do not really care how it is implemented

27
Abstract Data Types
  • We know a conceptual idea of a data type
  • We can work with objects of this type by using a
    set of predefined operations
  • We do not really care (and know) how it is
    implemented
  • Implementation can be changed - not even a bit in
    the application program will be changed
  • Different implementations are possible (could be
    used in different cases)
Write a Comment
User Comments (0)
About PowerShow.com