| Article Index |
|---|
| CIRCULAR QUEUE IMPLEMENTATION USING ADT |
| SOURCE CODE |
| DOCUMENTATION |
| TEST CASES AND COMPATIBILITY |
| All Pages |
IMPLEMENTATION OF A CIRCULAR QUEUE USING ADT
The aim of this article is to implement a circular queue using abstract data type.
INTRODUCTION:
We know that a linear queue is a “first in first out “ data structure,i.e., insertion can be made only at the end and deletion can be made only at the front. In a linear queue, the traversal through the queue is possible only once,i.e.,once an element is deleted,we cannot insert another element in its position. This disadvantage of a linear queue is overcome by a circular queue, thus saving memory.
Linear queue:

No more elements can be inserted in a linear queue now.
Circular queue:

In a circular queue, after rear reaches the end of the queue, it can be reset to zero. This helps in refilling the empty spaces in between.
This article contains the code for implementing a circular queue using ADT. Here, we demonstrate all the functions that can be performed in a circular queue.
CODE:
GOAL OF THE CODE:
To implement a circular queue using ADT.
VARIABLES USED:
1.a-pointer variable which stores the contents of the queue.
2.rear-pointer variable which points to the location where insertion is done.
3.front-pointer variable which points to the location where deletion is done.
4.cap-variable which indicates the maximum no.of elements that the queue can hold.
5.size-variable which indicates the no.of elements present in the queue at a particular instant.
6.queue-pointer to the structure qrec.
7.max-variable which indicates the maximun no.of elements (to be entered by the user).
8.x-local variable which stores the element to be inserted(in enqueue()).
9.p- local variable which contains the front most element(returned by frontanddelete()).
10.i-counter variable used to display the contents of the queue(in display()).
IN MAIN() FUNCTION:
11.m-variable which indicates the maximun no.of elements (to be entered by the user).
12.b-variable which stores the choice(to be entered by the user).
13.c-variable used in almost all the case statements to store the respective values.
FUNCTIONS USED:
|
FUNCTIONS |
PURPOSE |
|
createqueue() |
Dynamically allocates memory for the queue. |
|
makempty() |
Initializes rear and front to -1. |
|
isfull() |
Returns 1 if the queue is full else returns 0. |
|
isempty() |
Returns 1 if the queue is empty else returns 0. |
|
enqueue() |
Inserts elements into the queue. |
|
dequeue() |
Deletes elements from the queue. |
|
front() |
Returns the front most element of the queue. |
|
frontanddelete() |
Deletes the front most element and then returns the value pointed out by the front pointer. |
|
display() |
Displays the contents of the queue. |
IMPLEMENTATION:
1.Initially,the maximum no.of elements to be stored in the queue is obtained from the user.
2.A circular queue is created by dynamically allocating memory using createqueue() function.
3.Then, choices are provided to the user from which one can choose the operation to be performed.
4.A switch case is made use of in this code. Depending upon the choice entered by the user,the corresponding operations are performed.
5.After each operation,the display() function is called so that the user can better understand the implementation.
6.If the user enters a number that has not been specified in the cases, the default case is executed and the program terminates.
Initialization Conditions:
1.front and rear are initialised to -1.
2.size is initialised to 0.
3.capacity is assigned with the max. no.of elements.
TEST CASES:
Enter the max size 5
MENU
1.Enqueue
2.Dequeue
3.Front
4.Front and delete
5.Exit
ENTER UR CHOICE 1
Enter a no 3
CONTENTS 3
ENTER UR CHOICE 1
Enter a no 4
CONTENTS 3 4
ENTER UR CHOICE 1
Enter a no 5
CONTENTS 3 4 5
ENTER UR CHOICE 1
Enter a no 6
CONTENTS 3 4 5 6
ENTER UR CHOICE 2
Deq
CONTENTS 4 5 6
ENTER UR CHOICE 1
Enter a no 7
CONTENTS 4 5 6 7
ENTER UR CHOICE 3
Front value is 4
CONTENTS 4 5 6 7
ENTER UR CHOICE 2
Deq
CONTENTS 5 6 7
ENTER UR CHOICE 3
Front value is 5
CONTENTS 5 6 7
ENTER UR CHOICE 4
CONTENTS 6 7
ENTER UR CHOICE 5
COMPATIBILITY:
Compatible with all computers which are capable of running gcc-4.3.3-5. The program has been tested with the above version of gcc as of june 12.
| < Prev | Next > |
|---|




