SAVITIBAI PHULE UNIVERSITY OF PUNE
S. Y. B.Sc. (Computer Science) Semester III
Practical Examination
SUBJECT: CS-233 Practical course based on CS231
/* Write a program to reverse the elements of a queue using queue library. Implement basic queue operations init, enqueue, dequeue. */
// File name : "Queue.c"
#include"Queue.h"
int init(struct Queue *q)
{
q->front = q->rear = -1;
}
int isEmpty(struct Queue *q)
{
if(q->front == q->rear)
{
return 1;
}
else{
return 0;
}
}
int isFull(struct Queue *q)
{
if(q->rear == MAX-1)
{
return 1;
}
else{
return 0;
}
}
int enqueue(struct Queue *q, int x)
{
if(isFull(q) == 1)
{
printf("The queue is overflow...");
return 1;
}
else{
q->data[++(q->rear)] = x;
}
return 0;
}
int dequeue(struct Queue *q)
{
if(isEmpty(q) == 1)
{
printf("The stack is underflow...");
return 1;
}
else{
return q->data[++(q->front)];
}
}
void Display(struct Queue *q)
{
int i = 0;
printf("\n The Queue elements are : \t");
for(i = q->front + 1; i <= q->rear; i++)
printf("%d\t", q->data[i]);
}
void Reverse(struct Queue *q)
{
int x;
if (isEmpty(q) == 1)
return;
x = dequeue(q);
Reverse(q);
enqueue(q, x);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "Queue.h"
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
struct Queue
{
int data[MAX];
int front, rear;
};
int init(struct Queue *q);
int isEmpty(struct Queue *q);
int isFull(struct Queue *q);
int enqueue(struct Queue *q, int x);
int dequeue(struct Queue *q);
void Display(struct Queue *q);
void Reverse(struct Queue *q);
No comments:
Post a Comment