Queue implementation Using Array

 #include<conio.h>
#include<stdlib.h>
#include<stdio.h>
void enqueue(int a[], int *f, int *r, int *size)
{
    int data,choice=1;
    while(choice==1){
    if(*f==-1 && *r==-1)
    {
        *f=0;*r=0;
        printf("enter the value to be inserted: ");
        scanf("%d",&data);
        a[*r]=data;
    }
    else if(*r==*size-1)
    {
        printf("queue is full\n");
        break;
    }
    else
    {
        *r+=1;
        printf("enter the value to be inserted: ");
        scanf("%d",&data);
        a[*r]=data;
    }
    printf("want another element to be inserted(0,1): ");
    scanf("%d",&choice);
    }
}

void dequeue(int a[], int *f, int *r)
{
int choice=1;
while(choice==1)
  {
    if(*f==-1 && *r==-1)
    {
        printf("Queue is empty");
        break;
    }
    else if(*f==*r)
    {
        printf("Deleted element is: %d\n\nAnd Queue is now EMPTY\n",a[*f]);
        *r=-1;*f=-1;
        break;
    }
    else
    {
        printf("deleted element if %d\n",a[*f]);
        *f+=1;
    }
    printf("Do you want another element to be deleted(0,1): ");
    scanf("%d",&choice);
  }
}

void display(int a[], int f, int r)
{
    int i;
    if(f==-1 &&  r==-1)
    printf("Queue is empty");
    else
    {
     printf("queue is:\n");
     for(i=f;i<=r;i++)
     printf("%d\n",a[i]);
    }
}

void peek(int a[], int f, int r)
{
if(f==-1 && r==-1)
    printf("Queue is empty");
else
    printf("The element at front end is: %d\n",a[f]);
}

int main()
{
    int frnt=-1, rear=-1,n,ch,choice=1;
    printf("enter the size of queue: ");
    scanf("%d",&n);
    int queue[n];
    while(choice==1){
    printf("\nEnter 1. to enqueue(insert in queue) \n");
    printf("Enter 2. to dequeue (to delete from queue)\n");
    printf("Enter 3. to display \n");
    printf("Enter 4. to peek (to show the element at front) \n choice: ");
    scanf("%d",&ch);
    if(ch==1)
        enqueue(queue, &frnt, &rear, &n);
    else if(ch==2)
        dequeue(queue, &frnt, &rear);
    else if(ch==3)
        display(queue, frnt, rear);
    else if(ch==4)
        peek(queue, frnt, rear);
    else
        printf("invalid choice:");
    printf("\nDo you want to continue(0,1): ");
    scanf("%d",&choice);
    }
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree