Tuesday, 6 March 2018

Array Queue using Arrays in C

#include<stdio.h>

int arr[10],front=-1,rear=-1,i;

void enqueue(int object){

 if(rear>=10)

  printf("\nNo enough space to enqueue");

 else if(front == -1 && rear == -1)

 {

 front=0;

 rear++;

  arr[rear]=object;

}

 else

 {
  arr[++rear]=object;
printf("\n %d just moved in to stack.",object);
 }

}

void dequeue(){

 if(front == -1 && rear==-1)

  printf("\nNo object to dequeue");

 else{

  printf("\n%d just moved out of queue",arr[front]);

  front++;

 }

}

void traverse(){

 printf("\n\nTraverse:");

    if(rear==-1)

     printf("\nNo Elements to display");

    else

     for(i=front;i<=rear;i++)

         printf("\n Queue[%d]=%d",i,arr[i]);

}

void main(){

 for(i=0;i<5;i++)

  enqueue(i);

 traverse();

 dequeue();

 dequeue();

 traverse();

 enqueue(6);

 traverse();

}

No comments:

Post a Comment