affiliate marketing

Monday, 12 December 2011

ARRAY IMPLEMENTATION OF CIRCULAR QUEUE


ARRAY IMPLEMENTATION OF CIRCULAR QUEUE
Aim
                        To write a c program using arrays for implementing circular queue data structure.

Algorithm

                        Step 1: [Include All Header Files Required]
                        Step 2: [Define the array size as 5 and declare front and rear pointers]
                        Step 3: Declare the functions isEmpty() , isFull(), enqueue(), size(),dequeue(), peek() and
 view()]
                        Step 4: [Call the functions]
                                    Choice :1  CALL enqueue()
                                    Choice :2  CALL deenqueue()
Choice :3  CALL peek()
Choice :4  CALL size()
Choice :5  CALL view()

Algorithm for isEmpty( )

                        Step 1: [Check for underflow]
                                    If ( front         -1 and rear          -1 )
                                    RETURN -1
                        Step 2: Else RETURN 0
                                    [Finish the process]


Algorithm for isFull( )

Step 1: [Check for overflow]
                                    If (= (rear+1)% qsize         front )
                                    RETURN -1
                        Step 2: Else RETURN 0
                                    [Finish the process]

Algorithm for Enqueue( )

STEP 1: If[front= =rear]
                        Initialize front=rear=0
STEP 2: else rear=(rear+1)% qsize
                         Set queue[rear] =value
                         [return]

Algorithm for Dequeue( )

STEP 1: If[front = =rear]
                   1.1: temp=queue[front]
                  1.2: Initialize front=rear=-1
STEP 2:else
                 2.1: front=(front+1)% qsize
                        [return]




Algorithm for Peek( )

STEP 1:If [front= =rear]
STEP 1.1: temp=queue[front]
                            [return]
Algorithm for Size( )

STEP 1:If [front= =rear]
                     1.1: Set f=front
                     1.2: Set count=1
STEP 2: If [front!=rear]
                      2.1: front=(front+1)%qsize
                      2.2: set count=count+1
                             [return]

Algorithm for View( )

STEP 1: If [front = =rear]
                         Write (“Queue is empty”)
STEP 2: else
                         [display elements]        

Coding:

#include<stdio.h>
#include<conio.h>
#define qsize 5
int queue[qsize],front=-1,rear=-1;
void enqueue(int value);
void dequeue();
void view();
void main()
{
            int c,data,item;
            clrscr();
            printf("\n1.ENQUEUE\n2.DEQUEUE\n3.VIEW");
            while(1)
            {
                        printf("\n\nEnter your Choice :: ");
                        scanf("%d",&c);
                        switch(c)
                        {
                                    case 1:
                                    printf("\nEnter the element::");
                                    scanf("%d",&data);
                                    enqueue(data);
                                    break;
                                    case 2:
                                    dequeue();
                                    break;
                                    case 3:
                                    printf("\n\nContents of circular queue is \t");
                                    view();
                                    break;
                                    default:
                                    printf("\nInvalid Choice......");
                                    exit(0);
                        }
            }
}
int isfull()
{
            extern int queue[],front,rear;
            if(front==(rear+1)%qsize)
                        return(1);
            else
                        return(0);
}
int isempty()
{
            extern int queue[],front,rear;
            if((front==-1)&&(rear==-1))
                        return(1);
            else
                        return(0);
}
void enqueue(int value)
{
            extern int queue[],front,rear;
            if(isfull())
            {
                        printf("\nOverflow");
                        return;
            }
            else
            {
                        if(isempty())
                                    front=rear=0;
                        else
                                    rear=(rear+1)%qsize;
                        queue[rear]=value;
            }
}
void dequeue()
{
            int value;
            extern int queue[],front,rear;
            if(isempty())
                        printf("\n\nQueue is Empty");
            else
            {
                        value=queue[front];
                        printf("\nDequeue value is %d",value);
            }
            if(front==rear)
            {
                        front=-1;
                        rear=-1;
            }
            else
                        front=(front+1)%qsize;
}
void view()
{
            extern int queue[],front,rear;
            int f;
            if(isempty())
                        printf("\nUnderflow");
            else
            {
                        printf("\nFront-->");
                        for(f=front;f!=rear;f=(f+1)%qsize)
                                    printf("%d ---> ",queue[f]);
                        printf("%d <--Rear",queue[f]);
            }
            if(isfull())
                        printf("\nQueue is full");
}


Output

1.ENQUEUE
2.DEQUEUE
3.VIEW

Enter your Choice :: 1

Enter the element::2

Enter your Choice :: 3

Contents of circular queue is
Front-->2 <--Rear

Enter your Choice :: 1

Enter the element::3

Enter your Choice :: 1

Enter the element::5

Enter your Choice :: 3

Contents of circular queue is
Front-->2 ---> 3 ---> 5 <--Rear

Enter your Choice :: 2

Dequeue value is 2

Enter your Choice :: 3

Contents of circular queue is
Front-->3 ---> 5 <--Rear

Enter your Choice :: 4

Invalid Choice......

No comments:

Post a Comment