KEMBAR78
Simple Queue Using Array | PDF | Queue (Abstract Data Type) | Computer Programming
0% found this document useful (0 votes)
3 views5 pages

Simple Queue Using Array

The document provides a C implementation of a simple queue using an array, detailing functions for enqueueing, dequeueing, and displaying the queue. It highlights significant drawbacks of a linear queue, including memory wastage, limited size, and inefficiency in reusing space. The document suggests using a circular queue as a solution to these limitations, allowing for efficient space utilization and maintaining O(1) time complexity for operations.

Uploaded by

questandlearn001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Simple Queue Using Array

The document provides a C implementation of a simple queue using an array, detailing functions for enqueueing, dequeueing, and displaying the queue. It highlights significant drawbacks of a linear queue, including memory wastage, limited size, and inefficiency in reusing space. The document suggests using a circular queue as a solution to these limitations, allowing for efficient space utilization and maintaining O(1) time complexity for operations.

Uploaded by

questandlearn001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Simple Queue using Array

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#define SIZE 100 // maximum array size

/* Global variables */

int queue[SIZE];

int front = -1, rear = -1;

int ele, i, n; // n = user-defined queue size

/* Function Declarations */

void enqueue();

void dequeue();

void display();

void main() {

int choice;

clrscr();

printf("Enter size of the queue (max %d): ", SIZE);

scanf("%d", &n);

while (1) {

clrscr();

printf("\n===== SIMPLE QUEUE USING ARRAY =====\n");


printf("1. Enqueue (Insert)\n");

printf("2. Dequeue (Delete)\n");

printf("3. Display Queue\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: enqueue(); break;

case 2: dequeue(); break;

case 3: display(); break;

case 4: printf("\nExiting...\n"); getch(); exit(0);

default: printf("\nInvalid choice! Try again.\n");

printf("\n\nPress any key to continue...");

getch();

/* ---- ENQUEUE (Insert element at REAR) ---- */

void enqueue() {

if (rear == n - 1) {

printf("\nOverflow! Queue is full.");

} else {

printf("\nEnter element to enqueue: ");

scanf("%d", &ele);
if (front == -1) {

front = 0; // first element

rear++;

queue[rear] = ele;

printf("\nEnqueued %d successfully!", ele);

/* ---- DEQUEUE (Delete element from FRONT) ---- */

void dequeue() {

if (rear == -1 || front > rear) {

printf("\nUnderflow! Queue is empty.");

} else {

ele = queue[front];

printf("\nDequeued %d from queue.", ele);

front++;

/* ---- DISPLAY QUEUE ---- */

void display() {

if (rear == -1 || front > rear) {

printf("\nQueue is empty!");

} else {
printf("\nQueue: FRONT -> ");

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

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

printf("<- REAR");

Yes 👍 there are a couple of important drawbacks of a simple (linear) queue implementation
using arrays. Let me explain clearly:

🔴 Drawbacks of Simple Linear Queue


1. Wastage of Memory (Space Inefficiency)

 In a simple queue, insertion happens at the rear, and deletion happens at the front.
 After a few dequeue operations, the front keeps moving forward.
 Even though there may be empty slots at the beginning of the array, they cannot be
reused because insertion always happens only at the end (rear).

Example:
Suppose queue size = 5.

Enqueue: 10, 20, 30, 40, 50


Queue = [10, 20, 30, 40, 50] front=0, rear=4

Now perform 2 dequeues:

Dequeue → removes 10, then 20


Queue = [ , , 30, 40, 50] front=2, rear=4

 Even though two slots at the beginning are free, we cannot insert new elements
unless rear < size - 1.
 So when rear reaches the end, the queue is considered full even if free spaces exist at
the front.

This is the biggest drawback of a linear queue.

2. Limited Size (Fixed Capacity)


 Since it is implemented using a fixed-size array, once the array is full, no more
elements can be added.
 Increasing capacity requires creating a new array and copying elements, which is
inefficient.

3. Shifting Problem (If Tried to Reuse Space)

 Some implementations try to shift all elements to the beginning when front moves
forward.
 Example: after every dequeue, move all elements one step left.
 But this shifting makes each dequeue O(n) (slow for large queues).

✅ Solution to These Drawbacks


The Circular Queue was designed to overcome these limitations:

 In a circular queue, when rear reaches the end of the array, it wraps around to the
beginning (if free slots exist).
 This ensures efficient reuse of empty spaces and avoids unnecessary shifting.
 Time complexity stays O(1) for enqueue and dequeue.

You might also like