0% found this document useful (0 votes)
10 views9 pages

STACK Lab Task 04

The document contains two lab tasks involving stack implementations in C++. The first task requires creating two static integer stacks with overflow and underflow checks, allowing users to push and pop values between them. The second task involves a dynamic stack of 'student' objects, where users can input and display student data using a dynamically allocated array.

Uploaded by

ranahyderhyder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

STACK Lab Task 04

The document contains two lab tasks involving stack implementations in C++. The first task requires creating two static integer stacks with overflow and underflow checks, allowing users to push and pop values between them. The second task involves a dynamic stack of 'student' objects, where users can input and display student data using a dynamically allocated array.

Uploaded by

ranahyderhyder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab 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>

using namespace std;

class Stack {

private:

int arr[5];

int top;

int size;

public:

Stack() : top(-1), size(5) {}

bool isFull() {

return top == size - 1;

bool isEmpty() {

return top == -1;


}

void push(int value) {

if (isFull()) {

cout << "Stack Overflow\n";

} else {

arr[++top] = value;

int pop() {

if (isEmpty()) {

cout << "Stack Underflow\n";

return -1;

} else {

return arr[top--];

void display() {

if (isEmpty()) {

cout << "Stack is Empty\n";

} else {

for (int i = 0; i <= top; i++) {

cout << arr[i] << " ";


}

cout << "\n";

};

int main() {

Stack stack1, stack2;

int choice, value;

do {

cout << "1. Push to Stack 1\n";

cout << "2. Pop from Stack 1 and Push to Stack 2\n";

cout << "3. Pop from Stack 2\n";

cout << "4. Display Stack 1\n";

cout << "5. Display Stack 2\n";

cout << "6. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value to push into Stack 1: ";

cin >> value;

stack1.push(value);
break;

case 2:

value = stack1.pop();

if (value != -1) {

stack2.push(value);

break;

case 3:

stack2.pop();

break;

case 4:

cout << "Stack 1: ";

stack1.display();

break;

case 5:

cout << "Stack 2: ";

stack2.display();

break;

case 6:

break;

default:

cout << "Invalid choice\n";

} while (choice != 6);


return 0;

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

You might also like