0% found this document useful (0 votes)
7 views

Lab Assignment-5 DSA

The document presents a lab assignment focused on implementing a queue using an array in C programming. It includes the code for creating a queue, checking if it's full or empty, and functions for enqueueing and dequeueing items. The main function demonstrates the usage of these queue operations with sample outputs.

Uploaded by

yadav1258ji
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)
7 views

Lab Assignment-5 DSA

The document presents a lab assignment focused on implementing a queue using an array in C programming. It includes the code for creating a queue, checking if it's full or empty, and functions for enqueueing and dequeueing items. The main function demonstrates the usage of these queue operations with sample outputs.

Uploaded by

yadav1258ji
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/ 7

Lab Assignment -5(Data structure)

Name – Aryan yadav

Enrol. No. – 0901CS231025

Q. Implementation of Queue using Array

Code –

// 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;

Output –

You might also like