0% found this document useful (0 votes)
7 views8 pages

Question 1

Uploaded by

Samra Nawabi
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)
7 views8 pages

Question 1

Uploaded by

Samra Nawabi
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/ 8

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>

using namespace std;

class CircularQueue {

private:

int front, rear, maxSize;

int queue[8];

public:

CircularQueue() {

front = rear = -1;

maxSize = 8;

bool isFull() {

return (front == 0 && rear == maxSize - 1) || (rear == (front - 1 + maxSize) % maxSize);

bool isEmpty() {

return front == -1;

void insert() {

if (isFull()) {

cout << "Queue is full. Cannot insert more elements.\n";

return;

int item;

cout << "Enter the element to insert: ";

cin >> item;


if (front == -1) {

front = rear = 0;

} else {

rear = (rear + 1) % maxSize;

queue[rear] = item;

cout << "Element " << item << " inserted successfully.\n";

void deleteItem() {

if (isEmpty()) {

cout << "Queue is empty. Cannot delete elements.\n";

return;

int deletedItem = queue[front];

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % maxSize;

cout << "Element " << deletedItem << " deleted successfully.\n";

void display() {

if (isEmpty()) {

cout << "Queue is empty.\n";

return;

int i = front;

cout << "Queue elements: ";


do {

cout << queue[i] << " ";

i = (i + 1) % maxSize;

} while (i != (rear + 1) % maxSize);

cout << endl;

};

int main() {

CircularQueue cq;

int choice;

do {

cout << "\nCircular Queue Menu\n";

cout << "1. Insert\n";

cout << "2. Delete\n";

cout << "3. Display\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cq.insert();

break;

case 2:

cq.deleteItem();

break;

case 3:

cq.display();

break;
case 4:

cout << "Exiting the program.!\n";

break;

default:

cout << "Invalid choice. Please enter a valid option.\n";

} while (choice != 4);

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;
}

// Input function to get values from the user


void input() {
cout << "Enter Registration Number: ";
cin >> reg_no;
cin.ignore(); // Clear the input buffer
cout << "Enter Student Name: ";
getline(cin, st_name);
cout << "Enter CGPA: ";
cin >> cgpa;
}

// Output function to display the values of the student


void output() {
cout << "Registration Number: " << reg_no << endl;
cout << "Student Name: " << st_name << endl;
cout << "CGPA: " << cgpa << endl;
}
};

class DynamicArrayStack {
private:
int size;
int top;
Student* stackArray;

public:
// Constructor
DynamicArrayStack(int maxSize) {
size = maxSize;
top = -1;
stackArray = new Student[size];
}

// Destructor to free the dynamically allocated memory


~DynamicArrayStack() {
delete[] stackArray;
}

// Function to check if the stack is empty


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

// Function to check if the stack is full


bool isFull() {
return top == size - 1;
}

// Function to push a student object onto the stack


void push(Student student) {
if (isFull()) {
cout << "Stack Overflow. Cannot push more elements.\n";
return;
}
stackArray[++top] = student;
cout << "Student pushed onto the stack.\n";
}

// 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;
}

You might also like