0% found this document useful (0 votes)
10 views5 pages

Handout 3.3 - Queues

Uploaded by

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

Handout 3.3 - Queues

Uploaded by

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

QUEUES

In C, a queue is a data structure that follows the FIFO (First In, First Out) principle, meaning
that elements are added to the rear (tail) of the queue and removed from the front (head). You can
implement a queue in C using arrays or linked lists.
Here's a simple queue implementation using both approaches:

1. Queue Using Arrays


This method uses a fixed-size array to implement a queue.
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

typedef struct Queue{


int items[MAX];
int front;
int rear;
}

// Function to create an empty queue


Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
}

// Check if the queue is full


int isFull(Queue* q) {
if (q->rear == MAX - 1) return 1;
return 0;
}

// Check if the queue is empty


int isEmpty(Queue* q) {
if (q->front == -1) return 1;
return 0;
}

// Add elements to the queue


void enqueue(Queue* q, int value) {
if (isFull(q)) {
printf("Queue is full\n");
} else {
if (q->front == -1) q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Inserted %d\n", value);
}
}

1
// Remove elements from the queue
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
return item;
}
}

// Display elements of the queue


void display(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
} else {
printf("Queue contains: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
}

int main() {
Queue* q = createQueue();

enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
enqueue(q, 40);
enqueue(q, 50);

display(q);

dequeue(q);
dequeue(q);

display(q);

return 0;
}
Output:
Inserted 10
Inserted 20
Inserted 30
Inserted 40
Inserted 50

2
Queue contains: 10 20 30 40 50
Queue contains: 30 40 50

2. Queue Using Linked List


This method allows dynamic memory allocation and avoids the fixed-size limitation of an array.
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

typedef struct {
Node* front;
Node* rear;
} Queue;

// Function to create an empty queue


Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = q->rear = NULL;
return q;
}

// Add elements to the queue


void enqueue(Queue* q, int value) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
temp->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
printf("Inserted %d\n", value);
}

// Remove elements from the queue


int dequeue(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
return -1;
}
Node* temp = q->front;
int item = temp->data;
q->front = q->front->next;

if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return item;
}

3
// Display elements of the queue
void display(Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
return;
}
Node* temp = q->front;
printf("Queue contains: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
Queue* q = createQueue();

enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);

display(q);

dequeue(q);
dequeue(q);

display(q);

return 0;
}
Output:
Inserted 10
Inserted 20
Inserted 30
Queue contains: 10 20 30
Queue contains: 30

Key Operations:
 Enqueue: Adds an element to the end of the queue.
 Dequeue: Removes an element from the front of the queue.
 IsFull (for array implementation): Checks if the queue is full.
 IsEmpty: Checks if the queue is empty.
Differences Between Array and Linked List Implementation:
 Array: Fixed size, but access is faster (constant time).
 Linked List: Dynamic size, but requires additional memory for pointers.

4
Both implementations have their own advantages based on the application’s requirements (e.g.,
memory constraints, dynamic sizing).

You might also like