0% found this document useful (0 votes)
11 views8 pages

Practical 8 DS

Uploaded by

kirtanpanchalkpk
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)
11 views8 pages

Practical 8 DS

Uploaded by

kirtanpanchalkpk
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/ 8

23SE02IT017 SECE2221

1. Write a program to perform the following operations in linear queue – Addition, Deletion
and Traversing.

Input:

#include <stdio.h>

#define MAX 5

int queue[MAX];

int front = -1, rear = -1;

int isFull() {

return (front == (rear + 1) % MAX);

int isEmpty() {

return (front == -1);

void enqueue(int element) {

if (isFull()) {

printf("Queue is full\n");

return;

}
23SE02IT017 SECE2221

if (isEmpty()) {

front = 0;

rear = (rear + 1) % MAX;

queue[rear] = element;

printf("%d enqueued to queue\n", element);

int dequeue() {

if (isEmpty()) {

printf("Queue is empty\n");

return -1;

int element = queue[front];

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % MAX;

return element;

void traverse() {
23SE02IT017 SECE2221

if (isEmpty()) {

printf("Queue is empty\n");

return;

printf("Queue elements: ");

int i = front;

while (i != rear) {

printf("%d ", queue[i]);

i = (i + 1) % MAX;

printf("%d\n", queue[rear]);

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

enqueue(40);

enqueue(50);

traverse();

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


23SE02IT017 SECE2221

traverse();

enqueue(60);

traverse();

return 0;

Output:

2. Write a program to perform the following operations in linear queue – Addition, Deletion
and Traversing using Switch case.
23SE02IT017 SECE2221

Input:

#include <stdio.h>

#include <stdlib.h>

#define MAX 3

typedef struct {

int items[MAX];

int front, rear;

} CircularQueue;

void initQueue(CircularQueue *q) {

q->front = -1;

q->rear = -1;

int isFull(CircularQueue *q) {

return (q->front == (q->rear + 1) % MAX);

int isEmpty(CircularQueue *q) {

return (q->front == -1);

void enqueue(CircularQueue *q, int value) {

if (isFull(q)) {

printf("Queue is full!\n");

return;

if (isEmpty(q)) {
23SE02IT017 SECE2221

q->front = 0;

q->rear = (q->rear + 1) % MAX;

q->items[q->rear] = value;

printf("Enqueued %d\n", value);

int dequeue(CircularQueue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return -1;

int value = q->items[q->front];

if (q->front == q->rear) {

q->front = q->rear = -1;

} else {

q->front = (q->front + 1) % MAX;

printf("Dequeued %d\n", value);

return value;

void traverseQueue(CircularQueue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");
23SE02IT017 SECE2221

return;

printf("Queue: ");

int i = q->front;

do {

printf("%d ", q->items[i]);

i = (i + 1) % MAX;

} while (i != (q->rear + 1) % MAX);

printf("\n");

int main() {

CircularQueue q;

initQueue(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

traverseQueue(&q);

dequeue(&q);

traverseQueue(&q);

enqueue(&q, 40);

traverseQueue(&q);

return 0;

}
23SE02IT017 SECE2221

Output:

You might also like