Question 1
Question 1
Write a program to insert and delete items to and from a circular queue. Maximum size of array
is 8. Program must contain a switch function with 3-member function insert, delete and display.
#include<iostream>
class CircularQueue {
private:
int queue[8];
public:
CircularQueue() {
maxSize = 8;
bool isFull() {
bool isEmpty() {
void insert() {
if (isFull()) {
return;
int item;
front = rear = 0;
} else {
queue[rear] = item;
cout << "Element " << item << " inserted successfully.\n";
void deleteItem() {
if (isEmpty()) {
return;
if (front == rear) {
} else {
cout << "Element " << deletedItem << " deleted successfully.\n";
void display() {
if (isEmpty()) {
return;
int i = front;
i = (i + 1) % maxSize;
};
int main() {
CircularQueue cq;
int choice;
do {
switch (choice) {
case 1:
cq.insert();
break;
case 2:
cq.deleteItem();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
return 0;
Question 2:
Write a program which should implement a stack using a dynamic array, whose size will be
provided by user at program execution time. Elements of array used for stack are objects of
“student” class. “student” class contains attributes (privately defined) reg_no(int), st_name (string)
and cgpa (float). “student” class should also contain member functions (publicly defined);
constructor, input and output functions. User will push objects of class “student”, values of
attributes of objects will be provided by user. When an object will be popped from stack, value of
its attributes should be displayed on screen.
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
int reg_no;
string st_name;
float cgpa;
public:
// Constructor
Student() {
reg_no = 0;
cgpa = 0.0;
}
class DynamicArrayStack {
private:
int size;
int top;
Student* stackArray;
public:
// Constructor
DynamicArrayStack(int maxSize) {
size = maxSize;
top = -1;
stackArray = new Student[size];
}
// Function to pop a student object from the stack and display its attributes
void pop() {
if (isEmpty()) {
cout << "Stack Underflow. Cannot pop from an empty stack.\n";
return;
}
cout << "Popped Student Details:\n";
stackArray[top--].output();
}
};
int main() {
int stackSize;
cout << "Enter the size of the stack: ";
cin >> stackSize;
DynamicArrayStack stack(stackSize);
int choice;
do {
cout << "\nStack Operations\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Student newStudent;
newStudent.input();
stack.push(newStudent);
break;
}
case 2:
stack.pop();
break;
case 3:
cout << "Exiting the program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
}
while (choice != 3);
return 0;
}