Lab04 2380125
Lab04 2380125
Lab04 2380125
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class LinkedListStack {
private:
Node* top;
public:
LinkedListStack() {
top = nullptr;
}
bool isEmpty() {
return top == nullptr;
}
};
int main() {
LinkedListStack stack;
stack.pushMultiple({10, 20, 30, 40});
cout << "Linked List Stack Output:" << endl;
while (!stack.isEmpty()) {
cout << stack.pop() << endl;
}
cout << "-------------------------" << endl;
return 0;
}
Output:
class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}
int pop() {
if (stack.empty()) {
cout << "Stack is empty!" << endl;
return -1;
}
int poppedValue = stack.back();
stack.pop_back();
return poppedValue;
}
bool isEmpty() {
return stack.empty();
}
};
int main() {
ArrayStack stack;
stack.pushMultiple({10, 20, 30, 40});
cout << "Array Stack Output:" << endl;
while (!stack.isEmpty()) {
cout << stack.pop() << endl;
}
cout << "-------------------------" << endl;
return 0;
}
Output:
TASK 02
Code:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}
void pop() {
if (!stack.empty()) {
stack.pop_back();
}
}
double calculateAverage() {
if (stack.empty()) return 0;
double sum = accumulate(stack.begin(), stack.end(), 0);
return sum / stack.size();
}
void printStack() {
for (int i = stack.size() - 1; i >= 0; --i) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
ArrayStack stack;
stack.pushMultiple({0, 1, 5, 2, 4, 7});
cout << "Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;
stack.pop();
stack.pop();
cout << "After removing two elements: Stack elements: ";
stack.printStack();
stack.pushMultiple({10, -1});
cout << "After adding two more elements: Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;
return 0;
Output:
TASK 03
Code:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}
void pop() {
if (!stack.empty()) {
stack.pop_back();
}
}
double calculateAverage() {
if (stack.empty()) return 0;
double sum = accumulate(stack.begin(), stack.end(), 0);
return sum / stack.size();
}
void printStack() {
for (int i = stack.size() - 1; i >= 0; --i) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
ArrayStack stack;
stack.pop();
stack.pop();
stack.pop();
cout << "After removing three elements: Stack elements: ";
stack.printStack();
stack.pushMultiple({5, 9});
cout << "After adding two more elements: Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;
return 0;
}
Output:
TASK 04
Code:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}
void pop() {
if (!stack.empty()) {
stack.pop_back();
}
}
double calculateAverage() {
if (stack.empty()) return 0;
double sum = accumulate(stack.begin(), stack.end(), 0);
return sum / stack.size();
}
void printStack() {
for (int i = stack.size() - 1; i >= 0; --i) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
ArrayStack stack;
stack.pop();
stack.pop();
cout << "After removing two elements: Stack elements: ";
stack.printStack();
stack.push(5);
cout << "After adding one more element: Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;
return 0;
}
Output:
TASK 05
Code:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}
void pop() {
if (!stack.empty()) {
stack.pop_back();
}
}
double calculateAverage() {
if (stack.empty()) return 0;
double sum = accumulate(stack.begin(), stack.end(), 0);
return sum / stack.size();
}
void printStack() {
for (int i = stack.size() - 1; i >= 0; --i) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
ArrayStack stack;
return 0;
}
Output: