0% found this document useful (0 votes)
19 views18 pages

Geetadsa13 15

The document presents a series of C programs that implement various types of queues, including a standard queue, a reversed queue, and a double-ended queue (Deque). Each program includes functions for creating, inserting, deleting, checking size, and displaying elements of the queue. The document also includes sample outputs demonstrating the functionality of each queue implementation.

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)
19 views18 pages

Geetadsa13 15

The document presents a series of C programs that implement various types of queues, including a standard queue, a reversed queue, and a double-ended queue (Deque). Each program includes functions for creating, inserting, deleting, checking size, and displaying elements of the queue. The document also includes sample outputs demonstrating the functionality of each queue implementation.

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

/* 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 : Geeta Negi

Section : G2

Roll no : 08

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 \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 \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 \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:

geeta@tuf:-/Desktop/code/dsa-lab $ gcc ques13.c

geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out

Output :

Queue created

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: 66

Element 66 inserted

Queue elements: 66

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: 40

Element 40 inserted successfully!

Queue elements: 66 40
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: 80

Element 6 inserted successfully!

Queue elements: 66 40 80

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 : Geeta Negi

Section : G2

Roll no : 08

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:
geeta@tuf:-/Desktop/code/dsa-lab $ gcc 14.c

geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out

OUTPUT:

Press:

1 to insert

2 to exit

Enter data: 10

Press:

1 to insert

2 to exit

Enter data: 20

Press:

1 to insert

2 to exit

Enter data: 30

Press:

1 to insert

1 to insert

2 to exit

Initial Queue : 10 20 30

Reverse Queue : 30 20 10

/*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 : Geeta Negi

Section : G2

Roll no : 08

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 :

geeta@tuf:-/Desktop/code/dsa-lab $ gcc ques13.c

geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out
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: 10

Contents of queue from front: 10

Contents of queue from end: 10

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: 20

Contents of queue from front: 20 10

Contents of queue from end: 10 20

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: 30

Contents of queue from front: 20 10 30

Contents of queue from end: 30 10 20

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: 20

Contents of queue from front: 10 30

Contents of queue from end: 30 10

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy