Develop a ‘C’ program to create and implement a QUEUE using arrays.

 

DATA STRUCTURE & ALGORITHM USING C – LAB (2018407)

UNIT – 05

Develop a ‘C’ program to create and implement a QUEUE using arrays.

Program:

#include<stdio.h>

#define n 5
int main()
{
    int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
    printf("Queue using Array");
    printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
    while(ch)
    {
        printf("\nEnter the Choice:");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            if(rear==x)
                printf("\n Queue is Full");
            else
            {
                printf("\n Enter no %d:",j++);
                scanf("%d",&queue[rear++]);
            }
            break;
        case 2:
            if(front==rear)
            {
                printf("\n Queue is empty");
            }
            else
            {
                printf("\n Deleted Element is %d",queue[front++]);
                x++;
            }
            break;
        case 3:
            printf("\nQueue Elements are:\n ");
            if(front==rear)
                printf("\n Queue is Empty");
            else
            {
                for(i=front; i<rear; i++)
                {
                    printf("%d",queue[i]);
                    printf("\n");
                }
                break;
            case 4:
                exit(0);
            default:
                printf("Wrong Choice: please see the options");
            }
        }
    }
    return 0;
}
 
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
 
Enter the Choice:1               
Enter no 1:10
 
 
Enter the Choice:1               
Enter no 2:54
 
Enter the Choice:1               
Enter no 3:98
 
Enter the Choice:1               
Enter no 4:234
 
Enter the Choice:3               
Queue Elements are:
10
54
98
234
 
Enter the Choice:2               
 Deleted Element is 10
 
Enter the Choice:3               
Queue Elements are:
54
98
234
 
Enter the Choice:4
 
Result: Thus, the program executed successfully. 

 

Post a Comment

0 Comments