![]() |
A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C. Implement Queue using Array in CTo implement Queue using Array in C, we can follow the below approach: Approach:
Representation of Queue in CThe queue will be represented as a structure of fixed size array which consists of two pointers front and rear. The fixed size array will store the elements of the queue and the front and rear pointers will help the users to manipulate the queue elements. struct Queue { The user can define the maximum size of the array as per their requirements and a utility function can be used to initialize the front and the rear pointers to -1. Basic Operations of QueueFollowing are the basic operations of the Queue data structure which are required to manipulate the elements present inside the Queue.
Now let’s see how we can implement the basic functions of queue in C: 1. Enqueue FunctionThe enqueue function will insert an element at the end of the queue using the rear pointer. Following is the algorithm for enqueue function: Algorithm for Enqueue Function
2. Dequeue FunctionThe dequeue function will delete an element from the front of the queue using the front pointer. Following is the algorithm for enqueue function: Algorithm for Dequeue Function
3. IsEmpty FunctionThe isEmpty function returns true if the queue is empty otherwise it returns false. Following is the algorithm for the isEmpty function: Algorithm for IsEmpty Function
4. IsFull FunctionThe isFull function returns true if the queue is full otherwise it returns false.The queue is full when the rear pointer points to the last index of the array. Following is the algorithm for the isFull function: Algorithm for IsFull Function
C Program to Implement Queue using ArrayThe following program illustrates how we can implement the queue data structure using arrays in C:
Output Enqueued 1 in queue Enqueued 2 in queue Enqueued 3 in queue Elements in the queue after enqueue operation: 1 2 3 Deleted element: 1 Elements in the queue after dequeue operation: 2 3 Applications of QueueThe queue data structure has various applications in different domains of computer science. Some of the applications are:
ConclusionIn the following article we have learned about the queue data structure and how we can implement it using arrays in C. We have learnt about the basic operations which are required in a queue data structure to manipulate the elements of the queue. We have also learned about the various applications of queue data structure in the computer science domain. Related ArticlesThese are some articles that you may want to read to improve your understanding about queue: |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |