R. Inkulu http://www.iitg.ac.in/rinkulu/ 1 C code is developed on - - PowerPoint PPT Presentation

r inkulu http iitg ac in rinkulu
SMART_READER_LITE
LIVE PREVIEW

R. Inkulu http://www.iitg.ac.in/rinkulu/ 1 C code is developed on - - PowerPoint PPT Presentation

Queue 1 R. Inkulu http://www.iitg.ac.in/rinkulu/ 1 C code is developed on the board (and the same is not available in slides) (Queue) 1 / 10 Introduction Motivation for First-In-Fist-Out (FIFO) data structure job scheduling message


slide-1
SLIDE 1

Queue1

  • R. Inkulu

http://www.iitg.ac.in/rinkulu/

1C code is developed on the board (and the same is not available in slides) (Queue) 1 / 10

slide-2
SLIDE 2

Introduction

  • Motivation for First-In-Fist-Out (FIFO) data structure

job scheduling message buffers

  • Essential operations to be supported include:

enqueue an element to queue dequeue an element from queue

(Queue) 2 / 10

slide-3
SLIDE 3

Fixed-size queue

#define QUEUESIZE 1000 typedef struct { void *p[QUEUESIZE]; int head; //p[head] is the element to be dequeued int tail; //enqueued element is placed at p[tail] } Queue; int initialize(Queue *q); int enqueue(Queue *q, void *data); void *dequeue(Queue *q);

(Queue) 3 / 10

slide-4
SLIDE 4

Fixed-size queue

#define QUEUESIZE 1000 typedef struct { void *p[QUEUESIZE]; int head; //p[head] is the element to be dequeued int tail; //enqueued element is placed at p[tail] } Queue; int initialize(Queue *q); int enqueue(Queue *q, void *data); void *dequeue(Queue *q);

  • drawback in viewing p[] as a linear array: overflow may occur

while many entries in p[] are not utilized

(Queue) 3 / 10

slide-5
SLIDE 5

Fixed-size circular queue2

h t h t

head can precede tail, and vice versa

  • array p in the queue data structure (mentioned above) is viewed as

a circular

  • move head and tail over p[] using modular arithmatic

2considered to be the default queue: the word circular may not always be

mentioned

(Queue) 4 / 10

slide-6
SLIDE 6

Fixed-size circular queue (cont)

h h t

indicates queue is empty (reset tail to −1 and head to 0)

t h

indicates queue is full

(Queue) 5 / 10

slide-7
SLIDE 7

Worst-case time complexity

  • enqueue: O(1) time and O(1) space
  • dequeue: O(1) time and O(1) space

(Queue) 6 / 10

slide-8
SLIDE 8

Dynamic-sized circular queue

typedef struct { int capacity; int head; //p[head] is the element to be dequeued int tail; //enqueued element is placed at p[tail] void **p; //p is a pointer to an array of void *s } Queue; int initialize(Queue *q, int initialCapacity); int destroy(Queue *q); int enqueue(Queue *q, void *data); void *dequeue(Queue *q);

(Queue) 7 / 10

slide-9
SLIDE 9

Dynamic-sized circular queue

t h h t

resize when the queue is full

  • when the overflow occurs, double the size of buffer pointed by p
  • when the queue is one-quarter full, halve the size of buffer pointed

by p ← homework

  • in both of these cases, avoid using realloc (which implicitly frees
  • ld buffer) instead use malloc and explicitly free the old buffer

(Queue) 8 / 10

slide-10
SLIDE 10

Deque

data structure having provision for adding/removing at either of its ends is known as a doubly-ended queue (a.k.a. deque3) homework: implement the dynamic-sized deque

3’deque’ pronounced as ’deck’ (Queue) 9 / 10

slide-11
SLIDE 11

Stack using Queue, and vice versa

Homework:

  • Implement Stack using Queue and analyze the asymptotic time

and space complexities of the resultant push and pop

  • Implement Queue using Stack and analyze the asymptotic time

and space complexities of the resultant enqueue and dequeue

(Queue) 10 / 10