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

Programs MODULE 3

The document contains multiple C programs that simulate different types of queues: a simple queue, a circular queue, a double-ended queue using a circular queue, and a double-ended queue using a linear queue. Each program provides functionalities for inserting, deleting, and displaying elements in the queue. The code is structured with functions for each operation, and includes user interaction for selecting operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programs MODULE 3

The document contains multiple C programs that simulate different types of queues: a simple queue, a circular queue, a double-ended queue using a circular queue, and a double-ended queue using a linear queue. Each program provides functionalities for inserting, deleting, and displaying elements in the queue. The code is structured with functions for each operation, and includes user interaction for selecting operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MODULE 3

Program 1
Design, develop and execute a program in C to simulate the working of a queue
of integers using an array. Provide the following operations.
a. Insert
b. Delete
c. Display

#include<stdio.h>
#define maxsize 3
int q[maxsize], front=0,rear=-1;
void insert()
{
int n;
if(rear==maxsize-1)
printf("\nQueue full\n");
else
{
printf("\nEnter the data to be added\n");
scanf("%d", &n);
q[++rear]=n;
}
}
void delete()
{
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\n%d is deleted\n",q[front++]);
if(front>rear && rear==maxsize-1)
{
printf("\nReinit\n");
front=0; rear=-1;
}
}
}
void display()
{
int i;
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\nQueue status is\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
}

int main()
MODULE 3
{
int ch;
while(1)
{
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
puts("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2:delete(); break;
case 3:display(); break;
case 4: return 0;
default :printf("\nInvalid choice\n");
}
}
}
MODULE 3
MODULE 3
MODULE 3
Program 2
Simulate the working of Messaging system in which a message is placed in a
circular Queue by a message sender, a message is removed from the circular
queue by a message Receiver, which can also display the contents of the queue.
#include <stdio.h>
#define sz 5
int count =0;
int f=0,r=-1;
int q[sz];
void push(){
int item;
if(count == sz){
printf("Queue overflow");
return;
}
printf("Enter the element ");
scanf("%d",&item);
r = (r+1)%sz;
q[r]=item;
count++;
}
void pop(){
if(count == 0){
printf("queue underflow");
return;
}
printf("Item deleted is %d \n",q[f]);
f = (f+1)%sz;
count--;
}
void display(){
if(count == 0){
printf("Queue is empty \n");
return;
}
int j = f;
for(int i=0;i<count;i++){
printf("%d \t",q[j]);
j = (j+1)%sz;
}
printf("\n");
}
int main()
{
int option;
while(1){
printf("1.Push 2.Pop 3.Display 4. Exit \n");

printf("Enter the option ");


scanf("%d",&option);
MODULE 3
switch(option){
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return 0;
}
}
return 0;
}
MODULE 3
MODULE 3
Program 3
Implement a Double-Ended Queue (DEQue) Using a Circular Queue#include
<stdio.h>
#define Size 5

int deque_arr[Size];
int front = -1;
int rear = -1;

/* Insert at rear */
void insert_rear() {
int added_item;
if ((front == 0 && rear == Size - 1) || (front == rear + 1)) {
printf("Queue Overflow\n");
return;
}
if (front == -1) { /* if queue is initially empty */
front = 0;
rear = 0;
} else if (rear == Size - 1) { /* rear is at last position of queue */
rear = 0;
} else {
rear = rear + 1;
}
printf("Input the element for adding in queue: ");
scanf("%d", &added_item);
deque_arr[rear] = added_item;
}

/* Insert at front */
void insert_front() {
int added_item;
if ((front == 0 && rear == Size - 1) || (front == rear + 1)) {
printf("Queue Overflow \n");
return;
}
if (front == -1) { /* if queue is initially empty */
front = 0;
rear = 0;
} else if (front == 0) {
front = Size - 1;
} else {
front = front - 1;
}
printf("Input the element for adding in queue: ");
scanf("%d", &added_item);
MODULE 3
deque_arr[front] = added_item;
}

/* Delete from front */


void delete_front() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is %d\n", deque_arr[front]);
if (front == rear) { /* Queue has only one element */
front = -1;
rear = -1;
} else if (front == Size - 1) {
front = 0;
} else {
front = front + 1;
}
}

/* Delete from rear */


void delete_rear() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is: %d\n", deque_arr[rear]);
if (front == rear) { /* Queue has only one element */
front = -1;
rear = -1;
} else if (rear == 0) {
rear = Size - 1;
} else {
rear = rear - 1;
}
}

/* Display the queue */


void display_queue() {
int front_pos = front, rear_pos = rear;
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue elements:\n");
if (front_pos <= rear_pos) {
MODULE 3
while (front_pos <= rear_pos) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
} else {
while (front_pos <= Size - 1) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos) {
printf("%d ", deque_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}

/* Main function */
int main() {
int choice;
while (1) {
printf("\nEnter the choice:\n1. Insert at Rear\n2. Insert at Front\n3. Delete at Front\n4.
Delete at Rear\n5. Display Dequeue\n6. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_rear();
break;
case 2:
insert_front();
break;
case 3:
delete_front();
break;
case 4:
delete_rear();
break;
case 5:
display_queue();
break;
case 6:
return 0;
default:
printf("Wrong choice\n");
}
MODULE 3
}
}
MODULE 3

Program 4
Implement a Double-Ended Queue (DEQue) Using a Linear Queue

#include <stdio.h>
#include <conio.h> // Required for getch()

#define SIZE 100 // Define the size of the queue

int queue[SIZE]; // Array to hold queue elements


int front = -1; // Front pointer
int rear = -1; // Rear pointer

/* Function to insert an element at the front end */


MODULE 3
void i_front(int element)
{
if (front == -1) { /* Adding element in an empty queue */
front = rear = front + 1;
queue[front] = element;
return;
}
if (front == 0) { /* Checking whether the queue is full at the front end */
printf("Queue is Full.\n");
getch();
return;
}
front = front - 1; /* Decrementing front pointer */
queue[front] = element; /* Inserting the new element */
}

/* Function to delete an element from the front end */


int d_front()
{
int i;
if (front == -1 && rear == -1) { /* Checking whether the queue is empty */
printf("\n\tQueue is Empty.\n");
getch();
return -9999;
}
if (front == rear) { /* Checking whether the queue has only one element left */
i = queue[front];
front = -1;
rear = -1;
return i;
}
return queue[front++]; /* Returning the front-most element and incrementing the front pointer
*/
}

/* Function to insert an element at the rear end */


void i_rear(int element)
{
if (rear == -1) { /* Adding element in an empty queue */
front = rear = rear + 1;
queue[rear] = element;
return;
}
if (rear == SIZE - 1) { /* Checking whether the queue is full at the rear end */
printf("Queue is Full.\n");
getch();
MODULE 3
return;
}
rear = rear + 1; /* Incrementing rear pointer */
queue[rear] = element; /* Inserting the new element */
}

/* Function to delete an element from the rear end */


int d_rear()
{
int i;
if (front == -1 && rear == -1) { /* Checking whether the queue is empty */
printf("\n\tQueue is Empty.\n");
getch();
return -9999;
}
if (front == rear) { /* Checking whether the queue has only one element left */
i = queue[rear];
front = -1;
rear = -1;
return i;
}
return queue[rear--]; /* Returning the rear-most element and decrementing the rear pointer */
}

/* Function to display the current state of the queue */


void display_queue()
{
if (front == -1) {
printf("\nQueue is Empty\n");
return;
}

printf("\nQueue Elements:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}

/* Main function to test the linear DEQueue */


int main()
{
int choice, element;

while (1) {
printf("\nEnter your choice:\n");
MODULE 3
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display Queue\n");
printf("6. Exit\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert at front: ");
scanf("%d", &element);
i_front(element);
break;
case 2:
printf("Enter the element to insert at rear: ");
scanf("%d", &element);
i_rear(element);
break;
case 3:
element = d_front();
if (element != -9999)
printf("Deleted element from front: %d\n", element);
break;
case 4:
element = d_rear();
if (element != -9999)
printf("Deleted element from rear: %d\n", element);
break;
case 5:
display_queue();
break;
case 6:
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
MODULE 3
MODULE 3

You might also like