Program to implement Simple Queue using Array in C | Wave the world

Program to implement Simple Queue using Array in C

/*
         Program to implement a Simple Queue using Array.
         Created By: Pirate
*/

#include<stdio.h>
int queue[200];
int front = -1;
int rear = -1;
int size = 0;
void enqueue();
void dequeue();
void traverse();
void main(){
    int ch = 1;
    printf("*** Single Queue Using Array ***");
    do{
        printf("\nEnter the size of Queue : ");
        scanf("%d", &size);
        if(size < 1){
            printf("\nInvalid Queue size. \n Continue Y : 1/N : 0 - ");
            scanf("%d",&ch);
            if(ch == 0)
                exit(0);
        }
    }while(size < 1 && ch == 1);
    do{
        printf("\n1 : Insert\n2 : Delete\n3 : Traverse\n4 : Exit\n");
        scanf("%d",&ch);
        switch(ch){
            case 1:
                enqueue();
                break;
            case 2:
                dequeue();
                break;
            case 3:
                traverse();
                break;
            case 4:
                exit(0);
                break;
            default:
                printf("\n Invalid input. \n");

        }
    }while(1);
}
void enqueue(){
    if(rear == size - 1){
        printf("\nQueue is full");
        return;
    }
    if( rear == -1){
        rear = 0;
        front = 0;
    }
    else
        rear = rear + 1;
    printf("\nEnter the value to input : ");
    scanf("%d", &queue[rear]);
    printf("\n Success.");
}
void dequeue(){
    if(front == -1){
        printf("\nQueue is Empty.");
        return;
    }
    if(front == size - 1){
        front = -1;
        rear = -1;
    }
    else if(front == rear){
        front = -1;
        rear = -1;
    }
    else
        front = front + 1;
    printf("\n Success.");
    return;
}
void traverse(){
    int i;
    if(front == -1){
        printf("\nQueue is Empty.");
        return;
    }
    printf("\nQueue : \n");
    for(i=rear;i>=front;i--){
            printf("%d\t",queue[i]);
    }
    return;

}


Output :



3 comments:

  1. Thank you so much solved my problem!!!

    ReplyDelete
  2. Casinos in San Diego - MapyRO
    Casinos near Harrah's Casino 충청남도 출장안마 · San Diego's Hard Rock Hotel and Casino · The Palazzo · San Diego Sun · The 순천 출장마사지 Mirage 통영 출장샵 Hotel 안성 출장마사지 · The Mirage · Las 군포 출장마사지 Vegas

    ReplyDelete

 

Pro

About