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

TurboC Programs Input Output

Uploaded by

tarun9340solanki
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)
3 views

TurboC Programs Input Output

Uploaded by

tarun9340solanki
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/ 6

Turbo C++ Programs - Input and Output

Question 6: Queue using Array

Input Code:
#include <iostream>
#define SIZE 100
using namespace std;

class Queue {
int arr[SIZE];
int front, rear;

public:
Queue() {
front = rear = -1;
}

void enqueue(int value) {


if (rear == SIZE - 1)
cout << "Queue Overflow\n";
else {
if (front == -1) front = 0;
arr[++rear] = value;
cout << "Inserted " << value << endl;
}
}

void dequeue() {
if (front == -1 || front > rear)
cout << "Queue Underflow\n";
else
cout << "Deleted: " << arr[front++] << endl;
}

void display() {
if (front == -1 || front > rear)
cout << "Queue is Empty\n";
else {
cout << "Queue: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
}
};

int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
Turbo C++ Programs - Input and Output

q.enqueue(30);
q.display();
q.dequeue();
q.display();
return 0;
}

Expected Output:
Inserted 10
Inserted 20
Inserted 30
Queue: 10 20 30
Deleted: 10
Queue: 20 30

Question 7: Circular Queue using Array

Input Code:
#include <iostream>
#define SIZE 5
using namespace std;

class CircularQueue {
int arr[SIZE];
int front, rear;

public:
CircularQueue() {
front = rear = -1;
}

void enqueue(int value) {


if ((rear + 1) % SIZE == front)
cout << "Queue is Full\n";
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
arr[rear] = value;
cout << "Inserted: " << value << endl;
}
}

void dequeue() {
if (front == -1)
cout << "Queue is Empty\n";
else {
cout << "Deleted: " << arr[front] << endl;
if (front == rear)
Turbo C++ Programs - Input and Output

front = rear = -1;


else
front = (front + 1) % SIZE;
}
}

void display() {
if (front == -1)
cout << "Queue is Empty\n";
else {
cout << "Queue: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % SIZE;
}
cout << endl;
}
}
};

int main() {
CircularQueue cq;
cq.enqueue(1);
cq.enqueue(2);
cq.enqueue(3);
cq.enqueue(4);
cq.dequeue();
cq.enqueue(5);
cq.display();
return 0;
}

Expected Output:
Inserted: 1
Inserted: 2
Inserted: 3
Inserted: 4
Deleted: 1
Inserted: 5
Queue: 2 3 4 5

Question 8: Dequeue using Array

Input Code:
#include <iostream>
#define SIZE 10
Turbo C++ Programs - Input and Output

using namespace std;

class Deque {
int arr[SIZE];
int front, rear;

public:
Deque() {
front = -1;
rear = 0;
}

bool isFull() {
return ((front == 0 && rear == SIZE - 1) || (front == rear + 1));
}

bool isEmpty() {
return (front == -1);
}

void insertFront(int key) {


if (isFull()) {
cout << "Overflow\n";
return;
}
if (front == -1) {
front = rear = 0;
} else if (front == 0) {
front = SIZE - 1;
} else {
front--;
}
arr[front] = key;
}

void insertRear(int key) {


if (isFull()) {
cout << " Overflow\n";
return;
}
if (front == -1) {
front = rear = 0;
} else if (rear == SIZE - 1) {
rear = 0;
} else {
rear++;
}
arr[rear] = key;
}
Turbo C++ Programs - Input and Output

void deleteFront() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return;
}
cout << "Deleted from front: " << arr[front] << endl;
if (front == rear) {
front = rear = -1;
} else if (front == SIZE - 1)
front = 0;
else
front++;
}

void deleteRear() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return;
}
cout << "Deleted from rear: " << arr[rear] << endl;
if (front == rear) {
front = rear = -1;
} else if (rear == 0)
rear = SIZE - 1;
else
rear--;
}

void display() {
if (isEmpty()) {
cout << "Queue is Empty\n";
return;
}
cout << "Deque: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % SIZE;
}
cout << endl;
}
};

int main() {
Deque dq;
dq.insertRear(10);
dq.insertRear(20);
dq.insertFront(5);
dq.display();
Turbo C++ Programs - Input and Output

dq.deleteRear();
dq.deleteFront();
dq.display();
return 0;
}

Expected Output:
Deque: 5 10 20
Deleted from rear: 20
Deleted from front: 5
Deque: 10

You might also like