

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























docsity.com
docsity.com
docsity.com
docsity.com
#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
#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
?
?
?
rear = 5
entry
front = 3
entry
If we view the array as acircle, the cell after the5th cell is the 0th cell.
docsity.com
#define MAXQUEUE 6typedef char QueueEntry;struct Queue {
int count;int front, rear;QueueEntry entry[MAXQUEUE]; };
entry
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
void CreateQueue (Queue *Q) {
Q->count = 0;Q->front = 0;Q->rear = -1;
}
We can also use these values:front : 1
rear :
We have to make sure that when we enqueue the firstentry, front and rear point to that entry.
docsity.com
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
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
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
#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
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