Implementing a Queue in C using Arrays: A Step by Step Guide (2024)

Implementing a Queue in C using Arrays: A Step by Step Guide (3)

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. Queues are commonly used in computer science for tasks such as scheduling and managing processes. In this blog post, we will learn how to implement a queue using an array in the C programming language.

Before we begin, it is important to understand the basic operations that can be performed on a queue. These include:

  • enqueue: adding an element to the rear of the queue
  • dequeue: removing an element from the front of the queue
  • peek: viewing the element at the front of the queue
  • isFull: check if the queue is full
  • isEmpty: check if the queue is empty

To implement a queue using an array, we first need to define the size of the array and initialize it. We will also need to keep track of the front and rear indices of the queue, as well as a variable to store the number of elements currently in the queue. Here is an example of how this can be done in C:

#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = 0;
int rear = -1;
int count = 0;

Next, we can implement the enqueue function. This function will add an element to the rear of the queue and update the rear index accordingly. It is important to check if the queue is full before adding an element to prevent overflow. Here is an example of how this can be done in C:

void enqueue(int element) {
if (isFull()) {
printf("Error: Queue is full\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = element;
count++;
}

The dequeue function will remove an element from the front of the queue and update the front index accordingly. It is important to check if the queue is empty before removing an element to prevent underflow. Here is an example of how this can be done in C:

int dequeue() {
if (isEmpty()) {
printf("Error: Queue is empty\n");
return -1;
}
int element = queue[front];
front = (front + 1) % MAX_SIZE;
count--;
return element;
}

The peek function simply returns the element at the front of the queue without removing it. Here is an example of how this can be done in C:

int peek() {
if (isEmpty()) {
printf("Error: Queue is empty\n");
return -1;
}
return queue[front];
}

The isFull and isEmpty functions check if the queue is full or empty respectively. Here is an example of how these can be implemented in C:

bool isFull() {
return count == MAX_SIZE;
}
bool isEmpty() {
return count == 0;
}

With these basic functions in place, we now have a working queue implemented using an array in the C programming language. This is just a basic example and can be further expanded upon to include additional functionality or error handling.

In conclusion, implementing a queue using an array in the C programming language is a relatively simple task. By understanding the basic operations of a queue and utilizing an array to store the elements, we can create a functional and efficient queue data structure. However, it’s important to keep in mind that this is just a basic example, and there are many more advanced implementations and optimization techniques that can be used to improve the performance of this data structure. Furthermore, it’s important to make sure that the program is well tested, and has proper error handling mechanism to avoid any unexpected crashes.

Full Code here

#include<stdio.h> 
#include<stdlib.h>
void insert();
void dequeue();
void display();
int front = -1, rear = -1 ,maxsize;
int queue[100];
int main ()
{
int choice;
printf("\n Enter the size of QUEUE : ");
scanf("%d",&maxsize);
printf("\n QUEUE OPERATIONS USING ARRAY");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");
while(choice != 4)
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
return 0;
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");

}
void dequeue()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;

}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}

}

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n Elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
}
}

Implementing a Queue in C using Arrays: A Step by Step Guide (2024)

FAQs

How do you implement an array to represent a queue as a list? ›

Create an array to store the elements of the queue. Create variables to keep track of the front and rear of the queue. Initialize the front variable to 0 and the rear variable to -1. To add an element to the queue, increment the rear variable and add the element to the position indicated by the rear variable.

What is the display function of queue? ›

The Display Queues function shows, in table format, the contents of the user, command or hold queues. Each displayed table entry contains a related TID, job name, user ID, current status, and related information such as files currently in use and command type.

What is a linear queue program in data structure using C? ›

A queue in C is basically a linear data structure to store and manipulate the data elements. It follows the order of First In First Out (FIFO). In queues, the first element entered into the array is the first element to be removed from the array. For example, let's consider the scenario of a bus-ticket booking stall.

How to implement array in C? ›

There are various ways to do this:
  1. Initialize at the time of declaration using “ {} ”. int a[5] = {1, 2, 3, 4, 5};
  2. Initialize an array without specifying its size at declaration time. int a[] = {1, 2, 3, 4, 5}; ...
  3. Initialization by using the index of an element.
May 27, 2024

How to implement queue using array and linked list? ›

Implementing Queue using Linked list
  1. Initialize two pointers, FRONT and REAR, which represent the head and the tail of the queue, respectively. ...
  2. Assign the value to be enqueued to the data field of NEW_NODE(say DATA).
  3. Set the next pointer of NEW_NODE to NULL.
  4. Check if the queue is empty, indicated by FRONT being NULL.
Nov 21, 2023

How a queue is represented through array? ›

You can represent queues in the form of an array using pointers: front and rear. For example, the array shown in the diagram below consists of 11 characters having S at the front and N at the rear node. The value of the rear pointer becomes 11, whereas the front pointer remains the same.

Which array methods can be used to implement a queue? ›

A basic implementation of the queue data structure will be done with the methods:
  • enqueue() — Adds an element to the queue.
  • dequeue() — Removes and returns the first item entered in the queue.
  • isEmpty() — Returns true or false based on if the queue is empty or not.
  • front() — Returns the front element of the queue.
Mar 2, 2021

How do you implement a priority queue using arrays? ›

Implementation of Priority Queue using Array
  1. We will define a structure with two members - Value and Priority.
  2. Declare an array of structure having a fixed size maxSize.
  3. Define a variable size which will determine the size of the priority queue at any instance. Initially, size=-1 indicates that the queue is empty.
Mar 27, 2024

What is the queue structure in C? ›

In C, the queue can be represented as the structure that contains one array of fixed size, index pointer to the front, and index pointer to the end. The front pointer initial value will be -1 and the back pointer initial value will be 0.

What is queue with example? ›

It is also known as “first-come first-served.” The simplest example of a queue is the typical line that we all participate in from time to time. We wait in a line for a movie, we wait in the check-out line at a grocery store, and we wait in the cafeteria line (so that we can pop the tray stack).

How to implement queue data structure? ›

Queues can be implemented by using arrays or linked lists. Queues can be used to implement job scheduling for an office printer, order processing for e-tickets, or to create algorithms for breadth-first search in graphs.

How to make a queue using array in C? ›

  1. Ask the user for the operation like insert, delete, display and exit.
  2. According to the option entered, access its respective function using switch statement. ...
  3. In the function insert(), firstly check if the queue is full. ...
  4. In the function delete(), firstly check if the queue is empty.

What is application of queue in data structure C? ›

What are some real-world applications of a queue data structure? Real-world uses of queue data structures include call centers, printer management, traffic systems, task scheduling, and breadth-first search algorithms, enabling organized data handling and resource allocation.

How many types of queue are there in C? ›

There are four types of queues in a data structure: linear queue, circular queue, priority queue, and de-queue. Linear Queue inserts from one end while deletes from the other. In a circular queue, all nodes are circular. It is identical to a linear queue, except the last member is connected to the first.

How to implement a stack in C using an array? ›

Implementing a Stack using Array:
  1. Start by defining the maximum size of the stack.
  2. Declare an array of the desired data type, and initialize the top variable to -1.
  3. Implement the push function, which adds an item to the stack. ...
  4. Implement the pop function, which removes and returns the top item from the stack.

How do you iterate through an array in C? ›

To iterate through an array of the structures in C, we can simply use a loop (for loop or a while loop) and access each struct in the array using the array indexing, and then we can access every member using the member name.

How to implement priority queue with array? ›

Array-based implementation of a priority queue:
  1. Create an array to store the elements of the priority queue.
  2. To insert an element into the priority queue, add the element to the end of the array.
  3. To remove the highest priority element (in a max heap) or the lowest priority element (in a min heap),
Mar 2, 2023

Can we implement stack and queue using array? ›

Array is a linear data structure in which any specified index can be accessed. It can be used to implement stack and queue. Stack is LIFO principle-based, i.e., last in, first out.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5285

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.