SAVITIBAI PHULE UNIVERSITY OF PUNE
S. Y. B.Sc. (Computer Science) Semester III
Practical Examination
SUBJECT: CS-233 Practical course based on CS231
/* Q. 2 Implement a queue library (dyqueue.h) of integers using a dynamic (linked list) implementation of the queue and implement init, enqueue, dequeue, isempty, peek operations.*/
/// File name : "dyqueue.c"
#include"dyqueue.h"
int init()
{
front = rear = NULL;
return 0;
}
int isEmpty()
{
if((front == NULL && rear == NULL) || front == rear)
return 1;
else
return 0;
}
void enqueue(int x)
{
struct Queue *NewNode = NULL;
NewNode = (struct Queue*) malloc(sizeof(struct Queue));
if (NewNode != NULL)
{
NewNode->data = x;
NewNode->Next = NULL;
if (front == NULL)
rear = front = NewNode;
else{
rear->Next = NewNode;
rear = NewNode;
}
}
}
int dequeue()
{
int x;
struct Queue *temp = front;
if(isEmpty() == 1)
{
printf("Queue is empty...");
return -1;
}
else
{
x = front->data;
if(front == rear)
front = rear = NULL;
else
front = front->Next;
free(temp);
return (x);
}
}
int peek()
{
int x;
struct Queue *temp = front;
x = front->data;
if(front == rear)
front = rear = NULL;
else
front = front->Next;
return (x);
}
void Display()
{
struct Queue *temp = front;
printf("\n The Queue elements are : \t");
while(temp)
{
printf("%d\t", temp->data);
temp = temp->Next;
}
}
No comments:
Post a Comment