Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue Implementation Using an Array #1143

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Queue/Queue with Array/Queue_implementation_using_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

// C program for array implementation of queue
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a queue
struct Queue {
int front, rear, size;
unsigned capacity;
int* array;
};

// function to create a queue
// of given capacity.
// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*)malloc(
sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;

// This is important, see the enqueue
queue->rear = capacity - 1;
queue->array = (int*)malloc(
queue->capacity * sizeof(int));
return queue;
}

// Queue is full when size becomes
// equal to the capacity
int isFull(struct Queue* queue)
{
return (queue->size == queue->capacity);
}

// Queue is empty when size is 0
int isEmpty(struct Queue* queue)
{
return (queue->size == 0);
}

// Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)
% queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}

// Function to remove an item from queue.
// It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)
% queue->capacity;
queue->size = queue->size - 1;
return item;
}

// Function to get front of queue
int front(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}

// Function to get rear of queue
int rear(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}

// Driver program to test above functions./
int main()
{
struct Queue* queue = createQueue(1000);

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);

printf("%d dequeued from queue\n\n",
dequeue(queue));

printf("Front item is %d\n", front(queue));
printf("Rear item is %d\n", rear(queue));

return 0;
}
28 changes: 28 additions & 0 deletions Queue/Queue with Array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Queue Implementation using Array
Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. The order is First In First Out (FIFO).

For implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the rear and dequeue an item from the front. If we simply increment front and rear indices, then there may be problems, the front may reach the end of the array. The solution to this problem is to increase front and rear in circular manner.

## Steps for enqueue:
- Check the queue is full or not
- If full, print overflow and exit
- If queue is not full, increment tail and add the element

## Steps for dequeue:
- Check queue is empty or not
- If empty, print underflow and exit
- If not empty, print element at the head and increment head

### Complexity Analysis:
Time Complexity
| Operations | Complexity |
|-------------|--------------|
| Enqueue(insertion) | O(1) |
| Deque(deletion) | O(1) |
| Front(Get front) | O(1) |
| Rear(Get Rear) | O(1) |
| IsFull(Check queue is full or not) | O(1) |
| IsEmpty(Check queue is empty or not) | O(1) |

### Auxiliary Space:
O(N) where N is the size of the array for storing elements.