0% found this document useful (0 votes)
18 views19 pages

Garv Dsa

The document contains C programs that implement various queue operations, including a standard queue, a reversed queue, and a double-ended queue (deque). Each program provides functions for creating, inserting, deleting, checking size, and displaying elements of the queue. The programs are designed to demonstrate the functionality of queues and deques with user interaction for input and output.

Uploaded by

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

Garv Dsa

The document contains C programs that implement various queue operations, including a standard queue, a reversed queue, and a double-ended queue (deque). Each program provides functions for creating, inserting, deleting, checking size, and displaying elements of the queue. The programs are designed to demonstrate the functionality of queues and deques with user interaction for input and output.

Uploaded by

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

/* 13. Design an algorithm and a program to implement queue using array.

The program should

implement following queue operations:

1. a) Create() - create a queue

2. b) EnQueue(k) - insert an element k into the queue

3. c) DeQueue() - delete an element from the queue

4. d) IsEmpty() - check if queue is empty or not

5. e) Size() - finds the size of the queue

Name : Garv Singhal

Section : G2

Roll no : 04

Course : Btech cse

*/

#include <stdio.h>

#define MAX_SIZE 10

int queue[MAX_SIZE];

int front = 0;

int rear = 0;

int size = 0;

void createQueue() {

printf("Queue created successfully!\n");

void enQueue(int k) {

if (size == MAX_SIZE) {

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

} else {

queue[rear] = k;
rear = (rear + 1) % MAX_SIZE;

size++;

printf("Element %d inserted successfully!\n", k);

int deQueue() {

if (size == 0) {

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

return -1;

} else {

int temp = queue[front];

queue[front] = 0;

front = (front + 1) % MAX_SIZE;

size--;

printf("Element %d deleted successfully!\n", temp);

return temp;

int isEmpty() {

return size == 0;

int getSize() {

return size;

void display() {
printf("Queue elements: ");

for (int i = 0; i < size; i++) {

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

printf("\n");

int main() {

createQueue();

int choice,k;

do{

printf("\n1. To enqueue\n");

printf("2. To dequeue\n");

printf("3. Check if queue is empty\n");

printf("4. Get size of the queue\n");

printf("5. Display queue elements\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the element to insert: ");

scanf("%d", &k);

enQueue(k);

display();

break;
case 2:

deQueue();

display();

break;

case 3:

if (isEmpty()) {

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

} else {

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

break;

case 4:

printf("Size of the queue: %d\n", getSize());

break;

case 5:

display();

break;

case 6:

return 0;

default:

printf("Invalid choice!\n");

}while(choice!=6);

return 0;

path:
PS C:\Users\dell\code in vs\.vscode\DSA with c\QUEUE>

Output :

Queue created successfully!

1. To enqueue

2. To dequeue

3. Check if queue is empty

4. Get size of the queue

5. Display queue elements

6. Exit

Enter your choice: 1

Enter the element to insert: 34

Element 34 inserted successfully!

Queue elements: 34

1. To enqueue

2. To dequeue

3. Check if queue is empty

4. Get size of the queue

5. Display queue elements

6. Exit

Enter your choice: 1

Enter the element to insert: 42

Element 42 inserted successfully!

Queue elements: 34 42

1. To enqueue

2. To dequeue

3. Check if queue is empty


4. Get size of the queue

5. Display queue elements

6. Exit

Enter your choice: 1

Enter the element to insert: 6

Element 6 inserted successfully!

Queue elements: 34 42 6

1. To enqueue

2. To dequeue

3. Check if queue is empty

4. Get size of the queue

5. Display queue elements

6. Exit

Enter your choice: 4

Size of the queue: 3

1. To enqueue

2. To dequeue

3. Check if queue is empty

4. Get size of the queue

5. Display queue elements

6. Exit

Enter your choice: 6

/* 14. Design an algorithm and write a program to reverse a queue.


Name : Garv Singhal

Section : G2

Roll no : 04

Course : Btech cse

*/

#include <stdio.h>

#define MAX_SIZE 10

int queue[MAX_SIZE];

int front = 0;

int rear = 0;

int size = 0;

void enqueue(int data) {

if (size == MAX_SIZE) {

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

} else {

queue[rear] = data;

rear = (rear + 1) % MAX_SIZE;

size++;

int dequeue() {

if (size == 0) {

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

return -1;
} else {

int data = queue[front];

queue[front] = 0;

front = (front + 1) % MAX_SIZE;

size--;

return data;

void reverseQueue() {

int stack[MAX_SIZE];

int top = -1;

while (size > 0) {

stack[++top] = dequeue();

while (top >= 0) {

enqueue(stack[top--]);

void printQueue() {

int tempFront = front;

int tempSize = size;

printf("Initial Queue : ");

while (tempSize > 0) {

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

tempFront = (tempFront + 1) % MAX_SIZE;

tempSize--;

}
printf("\n");

reverseQueue();

tempFront = front;

tempSize = size;

printf("Reverse Queue : ");

while (tempSize > 0) {

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

tempFront = (tempFront + 1) % MAX_SIZE;

tempSize--;

printf("\n");

int main() {

int choice, data;

do {

printf("Press:\n1 to insert\n2 to exit\n");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter data: ");

scanf("%d", &data);

enqueue(data);

} else if (choice != 2) {

printf("Invalid choice!\n");

} while (choice != 2);

printQueue();

return 0;

path:
PS C:\Users\dell\code in vs\.vscode\DSA with c\QUEUE>

OUTPUT:

Press:

1 to insert

2 to exit

Enter data: 924

Press:

1 to insert

2 to exit

Enter data: 380

Press:

1 to insert

2 to exit

Enter data: 380

Press:

1 to insert

2 to exit

Press:

1 to insert

2 to exit

1 to insert

2 to exit
1

2 to exit

Enter data: 206

Press:

1 to insert

Press:

1 to insert

1 to insert

2 to exit

Initial Queue : 924 380 206

Reverse Queue : 206 380 924

/*15 Design an algorithm and write a program to implement Deque i.e. double ended queue. Double
ended queue is a queue in which insertion and deletion can be done from both ends of the queue.

The program should implement following operations:

a) insertFront() - insert an element at the front of Deque

b) insertEnd() - insert an element at the end of Deque

c) deleteFront() - delete an element from the front of Deque

d) deleteEnd() - delete an element from the end of Deque

e) isEmpty() - checks whether Deque is empty or not

f) isFull() - checks whether Deque is full or not

g) printFront() - print Deque from front

h) printEnd() - print Deque from end

Name : Garv Singhal

Section : G2

Roll no : 04

Course : Btech cse

*/

#include<stdio.h>

void insertFront(int data) {

if (isFull()) {

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

} else {

if (isEmpty()) {

front = rear = 0;

} else if (front == 0) {

front = MAX_SIZE - 1;

} else {

front--;

}
deque[front] = data;

size++;

void insertEnd(int data) {

if (isFull()) {

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

} else {

if (isEmpty()) {

front = rear = 0;

} else if (rear == MAX_SIZE - 1) {

rear = 0;

} else {

rear++;

deque[rear] = data;

size++;

void deleteFront() {

if (isEmpty()) {

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

} else {

printf("Deleted element from front: %d\n", deque[front]);

if (front == rear) {

front = rear = -1;

} else if (front == MAX_SIZE - 1) {


front = 0;

} else {

front++;

size--;

void deleteEnd() {

if (isEmpty()) {

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

} else {

printf("Deleted element from end: %d\n", deque[rear]);

if (front == rear) {

front = rear = -1;

} else if (rear == 0) {

rear = MAX_SIZE - 1;

} else {

rear--;

size--;

int isEmpty() {

return size == 0;

int isFull() {
return size == MAX_SIZE;

void printFront() {

if (isEmpty()) {

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

} else {

printf("Contents of queue from front: ");

int tempFront = front;

int tempSize = size;

while (tempSize > 0) {

printf("%d ", deque[tempFront]);

tempFront = (tempFront + 1) % MAX_SIZE;

tempSize--;

printf("\n");

void printEnd() {

if (isEmpty()) {

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

} else {

printf("Contents of queue from end: ");

int tempRear = rear;

int tempSize = size;

while (tempSize > 0) {

printf("%d ", deque[tempRear]);

if (tempRear == 0) {
tempRear = MAX_SIZE - 1;

} else {

tempRear--;

tempSize--;

printf("\n");

int main() {

int choice, data;

do {

printf("Press:\n1 to insert at front\n2 to insert at end\n3 to delete from front\n4 to delete from
end\n5 to exit\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data: ");

scanf("%d", &data);

insertFront(data);

printFront();

printEnd();

break;

case 2:

printf("Enter data: ");

scanf("%d", &data);

insertEnd(data);

printFront();
printEnd();

break;

case 3:

deleteFront();

printFront();

printEnd();

break;

case 4:

deleteEnd();

printFront();

printEnd();

break;

case 5:

break;

default:

printf("Invalid choice!\n");

} while (choice != 5);

return 0;

path:

PS C:\Users\dell\code in vs\.vscode\DSA with c\QUEUE>

OUTPUT:

Press:

1 to insert at front

2 to insert at end

3 to delete from front

4 to delete from end


5 to exit

Enter data: 382

Contents of queue from front: 382

Contents of queue from end: 382

Press:

1 to insert at front

2 to insert at end

3 to delete from front

4 to delete from end

5 to exit

Enter data: 299

Contents of queue from front: 299 382

Contents of queue from end: 382 299

Press:

1 to insert at front

2 to insert at end

3 to delete from front

4 to delete from end

5 to exit

Enter data: 543

Contents of queue from front: 299 382 543

Contents of queue from end: 543 382 299

Press:

1 to insert at front

2 to insert at end

3 to delete from front


4 to delete from end

5 to exit

Deleted element from front: 299

Contents of queue from front: 382 543

Contents of queue from end: 543 382

Press:

1 to insert at front

2 to insert at end

3 to delete from front

4 to delete from end

5 to exit

You might also like