Queues and Linked List-Data Structures-Lecture Slides, Slides of Data Structures and Algorithms

Prof. Gangesh Charu delivered this lecture at Alagappa University for Data Structures course. Its main points are: Queues, Linked, List, Array, Implementation, FIFO, Usage, Message, Print, Buffer, Circular

Typology: Slides

2011/2012

Uploaded on 07/15/2012

sahill
sahill 🇮🇳

4.8

(4)

46 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chap 4. Queues and Linked List
MCCS170
ABC
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Queues and Linked List-Data Structures-Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chap 4. Queues and Linked List

MCCS

A

B

C

docsity.com

  1. Queues

Overview

• Queue. Usage of Queue• Implementation of Queue:

– Array– Linked List

docsity.com

  1. Queues

First-in First-out (FIFO)

When we enqueue entriesin the queue and thendequeue them one by one,we will get the entries inthe same order.

A

B

C

A A

B

B

C

C

The first oneenqueued is thefirst onedequeued. (FIFO)

docsity.com

  1. Queues

Usage of Queue

• Queues in the real world

– Queue of persons in front of counter– Queue of jobs to be done

• Message queue in Windows• Print queue• I/O buffer

docsity.com

  1. Queues

Example

#include <stdio.h>#include "queue.h"int main () {

Queue Q; QueueEntry x;CreateQueue(&Q);Enqueue('a', &Q);Enqueue('b', &Q);Enqueue('c', &Q);PrintQueue(&Q);Dequeue(&x, &Q);PrintQueue(&Q);ClearQueue(&Q);return 0; }

The type Queue and thefunctions CreateQueue,Enqueue and Dequeue aredefined in the queue.h andqueue.cpp.

Remember, call CreateQueuebefore using the queue, andcall ClearQueue when thequeue is no longer used.

docsity.com

  1. Queues

Exercise

#include <stdio.h>#include "stack.h" /* stack of char /#include "queue.h" / queue of char */int main ( ) {

char x;Queue Q; Stack S;CreateQueue(&Q); CreateStack(&S);Enqueue('a', &Q); Enqueue('b', &Q); Enqueue('c', &Q);Enqueue('d', &Q); Enqueue('e', &Q); Enqueue('f', &Q);for (int i=0; i<3; i++) { Dequeue(&x, &Q); Push(x, &S); }while (!StackEmpty(&S)) {

Pop(&x, &S); Enqueue(x, &Q); }

/* draw the content of Q and S at this point of time */return 0; }

Q1. Draw the queueand the stack.

docsity.com

  1. Queues

Circular Array

?

E

F

D

?

?

F E D??^?

rear = 5

entry

front = 3

G

entry

If we view the array as acircle, the cell after the5th cell is the 0th cell.

G

docsity.com

  1. Queues

Array implementation

#define MAXQUEUE 6typedef char QueueEntry;struct Queue {

int count;int front, rear;QueueEntry entry[MAXQUEUE]; };

F E D??^?

entry

D

E

F front = 3^ rear = 5 count = 3

Number of entriesin the Q

Change the definition ofQueueEntry to match yourapplication. E.g. if you needa queue of struct Student,writetypedef Student QueueEntry;

The capacity of thequeue. In arrayimplementation, wehave to fix the sizeof array in compiletime.

docsity.com

  1. Queues

Create Queue

void CreateQueue (Queue *Q) {

Q->count = 0;Q->front = 0;Q->rear = -1;

}

We can also use these values:front : 1

rear :

… 5 = MAXQUEUE - 1

We have to make sure that when we enqueue the firstentry, front and rear point to that entry.

docsity.com

  1. Queues

Enqueue

void Enqueue (QueueEntry x, Queue *Q) {

if (QueueFull(Q))

printf("Cannot … "); else {

Q->count ++;Q->rear = (Q->rear+1) % MAXQUEUE;Q->entry[Q->rear] = x; }

}

This formula is the same asif (Q->rear==MAXQUEUE-1)

Q->rear = 0; else

Q->rear ++;

Remember to update countwhenever the number ofentries in the Q changes!

docsity.com

  1. Queues

Traversing a Queue

void PrintQueue (Queue *Q) {

int c;int i = Q->front;int size=QueueSize(Q);printf("<< ");for (c=1; c<=size; c++) {

printf("%c ", Q->entry[i]);i = (i+1) % MAXQUEUE;

} printf("<<"); }

docsity.com

  1. Queues

Exercise

Q2. int QueueEmpty (Queue *Q);int QueueFull (Queue *Q);void ClearQueue (Queue *Q);int QueueSize (Queue *Q);void QueueFront (QueueEntry *x, Queue *Q);

Write the following functions on

array implementation of Queue.

docsity.com

  1. Queues

Code Listing, 2

#include <stdio.h>#include "queue.h"void CreateQueue (Queue *Q) {

Q->count = 0;Q->front = 0;Q->rear = -1; } void Enqueue (QueueEntry x, Queue *Q) {

if (QueueFull(Q))

printf("Cannot enqueue item into full queue."); else {

Q->count ++;Q->rear = (Q->rear+1) % MAXQUEUE;Q->entry[Q->rear] = x; } }

queue.cpp, p.

docsity.com

  1. Queues

Code Listing, 3

void Dequeue (QueueEntry *x, Queue *Q) {

if (QueueEmpty(Q))

printf("Cannot dequeue from empty queue."); else {

Q->count --;*x = Q->entry[Q->front];Q->front = (Q->front+1) % MAXQUEUE; } }; int QueueEmpty (Queue *Q) {

return Q->count <=0; } int QueueFull (Queue *Q) {

return Q->count >= MAXQUEUE; }

queue.cpp, p.

docsity.com