2081 3

CSC211 ยท 2081 ยท 10 marks

Define circular queue. How queue differ from stack. Write a program to implement linear queue.

Define circular queue. How queue differ from stack. Write a program to implement linear queue.[10]

Data Structures and AlgorithmsChapter 3: QueueLinear Queue, Circular Queue, Priority Queue, Queue Applicat

Quick answer

A circular queue is a linear data structure that arranges data elements in a circular pattern, where the last element is connected back to the first element. It overcomes the major drawback of a linear queue (wastage of memory space).

Circular Queue, Queue vs Stack, and Linear Queue Implementation


1. Definition of Circular Queue (2 marks)

In a circular queue:

  • Insertion and deletion follow the FIFO (First-In-First-Out) principle.
  • When the rear pointer reaches the end of the array, it wraps around to the beginning (index 0), reusing the freed spaces.
  • This eliminates the problem where storage space at the beginning of the array is discarded and never used again (as happens in a linear queue).

Condition:

  • Queue is empty when front == -1
  • Queue is full when (rear + 1) % SIZE == front

2. Differences Between Queue and Stack (3 marks)

FeatureQueueStack
PrincipleFollows FIFO (First-In-First-Out)Follows LIFO (Last-In-First-Out)
Insertion EndInsertion is done at the rear endInsertion (PUSH) is done at the top
Deletion EndDeletion is done at the front endDeletion (POP) is done at the top
Pointers UsedUses two pointers: front and rearUses one pointer: top
OperationsCalled Enqueue (insert) and Dequeue (delete)Called PUSH (insert) and POP (delete)
AccessOnly the front element is accessible for removalOnly the top element is accessible
Example UseCPU scheduling, printer spoolingFunction call management, expression evaluation

As stated in the notes: "The stack works as LIFO (last-in-first-out) technique but the queue works as FIFO technique (first-in-first-out) i.e., the first element inserted into the queue is the first element to be removed."


3. Program to Implement Linear Queue (5 marks)

A linear queue is an ordered collection where:

  • Elements are inserted at the rear end.
  • Elements are deleted from the front end.
  • It follows the FIFO principle.

Note: The main problem with linear queue is that both rear and front indices are increased but never decreased, leading to wastage of storage space.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 5   /* Maximum size of the queue */

int queue[SIZE];
int front = -1;
int rear  = -1;

/* ---- Function to check if queue is empty ---- */
int isEmpty()
{
    if (front == -1 || front > rear)
        return 1;
    return 0;
}

/* ---- Function to check if queue is full ---- */
int isFull()
{
    if (rear == SIZE - 1)
        return 1;
    return 0;
}

/* ---- Enqueue: Insert element at rear ---- */
void enqueue(int value)
{
    if (isFull())
    {
        printf("Queue Overflow! Cannot insert %d\n", value);
        return;
    }
    if (front == -1)
        front = 0;   /* First element being inserted */

    rear = rear + 1;
    queue[rear] = value;
    printf("Inserted: %d\n", value);
}

/* ---- Dequeue: Delete element from front ---- */
void dequeue()
{
    if (isEmpty())
    {
        printf("Queue Underflow! Queue is empty.\n");
        return;
    }
    printf("Deleted: %d\n", queue[front]);
    front = front + 1;

    /* Reset queue when all elements are removed */
    if (front > rear)
    {
        front = -1;
        rear  = -1;
    }
}

/* ---- Display all elements in the queue ---- */
void display()
{
    int i;
    if (isEmpty())
    {
        printf("Queue is empty.\n");
        return;
    }
    printf("Queue elements (front to rear): ");
    for (i = front; i <= rear; i++)
    {
        printf("%d  ", queue[i]);
    }
    printf("\n");
}

/* ---- Main Function ---- */
int main()
{
    int choice, value;

    printf("=== Linear Queue Implementation ===\n");

    do {
        printf("\n1. Enqueue (Insert)");
        printf("\n2. Dequeue (Delete)");
        printf("\n3. Display");
        printf("\n4. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                enqueue(value);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                display();
                break;
            case 4:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice!\n");
        }
    } while (choice != 4);

    return 0;
}

Sample Output

=== Linear Queue Implementation ===

1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Enter value to insert: 10
Inserted: 10

Enter your choice: 1
Enter value to insert: 20
Inserted: 20

Enter your choice: 1
Enter value to insert: 30
Inserted: 30

Enter your choice: 3
Queue elements (front to rear): 10  20  30

Enter your choice: 2
Deleted: 10

Enter your choice: 3
Queue elements (front to rear): 20  30

Enter your choice: 4
Exiting...

4. Limitation of This Linear Queue

Once rear reaches SIZE - 1, isFull() reports the queue as full even if elements have already been dequeued from the front and slots 0 to front-1 are sitting empty, because rear and front only ever move forward and are never wrapped back to the start of the array. This wasted space is exactly the drawback that the circular queue (Section 1) fixes, by wrapping rear and front around to index 0 with the modulo operation once the array end is reached, so the same allocated storage can be reused indefinitely instead of being abandoned after one pass through the array.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.