Tuesday, July 21, 2015

Circular Queue Operation In Data Structure Using C Programming

Hello guys,
Here i am gonna show how to perform Circular Queue Operation in Data Structure using C programming.




#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 3
struct student {
    int roll[max];
    int rear;
    int front;
}s;
int isEmpty() {
    if(s.front == s.rear)
        return 1;
    else
        return 0;
}
int isFull() {
    if(s.front == (s.rear+1)%max)
        return 1;
    else
        return 0;
}
int display() {
    int i;
    if(isEmpty() == 1)
        printf("The Queue is Empty\n");
    else {
    for(i=s.front+1;i<=s.rear;i++)
        printf("%d\t",s.roll[i]);
    }
}
int enqueue(int num) {
        s.rear = (s.rear+1)%max;
        s.roll[s.rear] = num;
        return 1;
}
int dequeue() {
    s.front = (s.front+1)%max;
    return 1;
}
int loop() {
    int opt;
    printf("\nDo you want to continue to main menu?? 1.yes 2.no \t");
    scanf("%d",&opt);
    system("cls");
    return opt;
}
void main() {
    int opt,r;
    s.front = 0;
    s.rear = 0;
    do {
    printf("\t\t\t\t*****Queue*****\n");
    printf("1. Isempty \n2. Isfull\n3. Enqueue\n4. Dequeue\n5. Display\t");
    scanf("%d",&opt);
    switch(opt) {
        case 1 :
            if(isEmpty() == 1)
                printf("The Queue is empty");
            else
                printf("The is not Empty");
            break;
        case 2 :
            if(isFull() == 1)
                printf("The Queue is full");
            else
                printf("The is not full");
            break;
        case 3 :
            if(isFull() == 1)
                printf("Cannot add new item, queue is full!!");
            else {

                printf("\nEnter the number you want to add in queue \t");
                scanf("%d",&r);
                if(enqueue(r) == 1) {
                    printf("Succesfully number aded in queue !! \n");
                    }
            }
            break;
        case 4 :
            if(isEmpty() == 1)
                printf("Empty Queue");
            else {
                if(dequeue() == 1)
                    printf("Successfullly deleted item :)");
            }
            break;
        case 5 :
            display();
            break;
        default :
            printf("Invalid input");
            break;
    }
} while(loop() == 1);
}






Open C Compiler(any) and paste the above code, enjoy....

No comments:

Post a Comment