Our Android App Also Got an Upgrade. Download now!

Queue Implementation Using Array C Program

queue_array.txt

File size: 1.17 KB

File content type: text/plain

Category: Practical Files

Subject: Data Structure

#include<stdio.h>
#define MAXSIZE 5

void enqueue();
int dequeue();
void peek();
int isEmpty();
int isFull();

int queue[MAXSIZE];
int rear = -1, front = -1;

void main() {
	
	int value, choice;
	
	while(1) {
		
		printf("Enter choice.\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
		scanf("%d",&choice);
		
		switch(choice) {
			case 1: if(isFull()) {
						printf("Cannot push. Overflow.\n");
						break;
					}
					printf("Enter number\n");
					scanf("%d",&value);
					enqueue(value);
					break;
					
			case 2: if(isEmpty()) {
						printf("Cannot pop. Underflow.\n");
						break;
					}
					value = dequeue();
					printf("Value popped = %d\n",value);
					break;
					
			case 3: peek();
					break;
			
			case 4: return;
						
		}
	}
}

int isFull() {
	if(rear == MAXSIZE-1)
		return 1;
	return 0;
}

int isEmpty() {
	if(front == -1 || front > rear)
		return 1;
	return 0;
}

void enqueue(int value) {
	queue[++rear] = value;
	if(front == -1)
		front++;
}

int dequeue() {
	int value = queue[front++];
	return value;
}

void peek() {
	int i;
	for(i=front;i<=rear;i++)
		printf("%d ",queue[i]);
	printf("\n");
}

Queue using array in C

Last Updated: July 16, 2022