top of page

Queue Using Array

Writer's picture: Code CrazeCode Craze

#include <stdio.h>

#include<stdlib.h>

#include<math.h>


#define size 5

int queue[size];

int front = -1;

int rear = -1;


void enque(void);

void deque(void);

void peek(void);

void isEmpty(void);

void isFull(void);

void display(void);


void main() {

int c,e;

do{

switch(c)

{

case 1:

enque();

break;

case 2:

deque();

break;

case 3:

peek();

break;

case 4:

isEmpty();

break;

case 5:

isFull();

break;

case 6:

display();

break;

case 7:

printf("\n\n----Exit from program successfully----");

printf("\n--------------Thank you--------------");

exit(0);

}

printf("\n1:Enque\t 2:Deque\t 3:Peek\t 4:isEmpty\t 5:isFull\t 6:Display\t 7:Exit");

printf("\nEnter your choice : ");

scanf("%d",&c);

}while(c);

}


void enque()

{

int v;

printf("\nEnter value : ");

scanf("%d",&v);

if(rear==size-1)

{

printf("\nQueue is full!!");

}

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

{

front=rear=0;

queue[rear]=v;

}

else

{

rear++;

queue[rear]=v;

}

}


void display()

{

int i=0;

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

{

printf("\nQueue is empty");

}

else

{

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

{

printf("--%d",queue[i]);

}

}

}


void deque()

{

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

{

printf("\nQueue is empty!!");

}

else if(front==rear)

{

front=rear=-1;

}

else

{

int i;

front++;

}

}


void peek()

{

printf("\nTop : %d",rear);

printf("\nTop Value: %d",queue[rear]);

}


void isEmpty()

{

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

{

printf("\nQueue is empty!!");

}

else

{

printf("\nQueue is not empty!!");

}

}


void isFull()

{

if(rear==size-1)

{

printf("\nQueue is full!!");

}

else

{

printf("\nQueue is not full!!");

}

}

3 views0 comments

Recent Posts

See All

Stack Using Queue

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue...

Comments


bottom of page