STACK Lab Task 04
STACK Lab Task 04
Lab Task 01
Write a program containing two stacks of static arrays of size 5 each, elements of the array
should be of integer (int) type. The user will push values in the first stack, when a value is
popped from the first stack it should be pushed to the second stack. For each stack, you have to
check stack overflow and underflow conditions. User may remove an element from the second
stack as well.
CODE:
#include <iostream>
class Stack {
private:
int arr[5];
int top;
int size;
public:
bool isFull() {
bool isEmpty() {
if (isFull()) {
} else {
arr[++top] = value;
int pop() {
if (isEmpty()) {
return -1;
} else {
return arr[top--];
void display() {
if (isEmpty()) {
} else {
};
int main() {
do {
cout << "2. Pop from Stack 1 and Push to Stack 2\n";
switch (choice) {
case 1:
stack1.push(value);
break;
case 2:
value = stack1.pop();
if (value != -1) {
stack2.push(value);
break;
case 3:
stack2.pop();
break;
case 4:
stack1.display();
break;
case 5:
stack2.display();
break;
case 6:
break;
default:
Lab Task 02
Write a program which should implement a stack using a dynamic array, whose size will
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.
CODE:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int reg_no;
string st_name;
float cgpa;
public:
Student() : reg_no(0), st_name(""), cgpa(0.0) {}
void input() {
cout << "Enter Registration Number: ";
cin >> reg_no;
cout << "Enter Name: ";
cin.ignore();
getline(cin, st_name);
cout << "Enter CGPA: ";
cin >> cgpa;
}
void output() {
cout << "Registration Number: " << reg_no << "\n";
cout << "Name: " << st_name << "\n";
cout << "CGPA: " << cgpa << "\n";
}
};
class Stack {
private:
Student* arr;
int top;
int size;
public:
Stack(int s) : size(s), top(-1) {
arr = new Student[size];
}
~Stack() {
delete[] arr;
}
bool isFull() {
return top == size - 1;
}
bool isEmpty() {
return top == -1;
}
void push(Student s) {
if (isFull()) {
cout << "Stack Overflow\n";
} else {
arr[++top] = s;
}
}
Student pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return Student();
} else {
return arr[top--];
}
}
};
int main() {
int size, choice;
cout << "Enter the size of the stack: ";
cin >> size;
Stack stack(size);
do {
cout << "1. Push Student\n";
cout << "2. Pop and Display Student\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Student s;
s.input();
stack.push(s);
break;
}
case 2: {
Student s = stack.pop();
if (!stack.isEmpty()) {
s.output();
}
break;
}
case 3:
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 3);
return 0;
}