Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab04 2380125

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

TASK 01

IMPLEMENTATION USING LINKED LIST


Code:
#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

class LinkedListStack {
private:
Node* top;

public:
LinkedListStack() {
top = nullptr;
}

void push(int value) {


Node* newNode = new Node(value);
newNode->next = top;
top = newNode;
}

void pushMultiple(const vector<int>& elements) {


for (int value : elements) {
push(value);
}
}
int pop() {
if (top == nullptr) {
cout << "Stack is empty!" << endl;
return -1;
}
Node* temp = top;
top = top->next;
int poppedValue = temp->data;
delete temp;
return poppedValue;
}

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:

IMPLEMENTATION USING ARRAYS


Code:
#include <iostream>
#include <vector>
using namespace std;

class ArrayStack {
private:
vector<int> stack;
public:
void push(int value) {
stack.push_back(value);
}

void pushMultiple(const vector<int>& elements) {


stack.insert(stack.end(), elements.begin(), elements.end());
}

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 pushMultiple(const vector<int>& elements) {


stack.insert(stack.end(), elements.begin(), elements.end());
}

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 pushMultiple(const vector<int>& elements) {


stack.insert(stack.end(), elements.begin(), elements.end());
}

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({4, 12, 3, 7, 11});


cout << "Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;

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 pushMultiple(const vector<int>& elements) {


stack.insert(stack.end(), elements.begin(), elements.end());
}

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({8, 14, 2, 6, 10});


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.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 pushMultiple(const vector<int>& elements) {


stack.insert(stack.end(), elements.begin(), elements.end());
}

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({7, 3, 11, 6, 2, 4});


cout << "Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;

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


stack.pop();
}
cout << "After removing four elements: Stack elements: ";
stack.printStack();

stack.pushMultiple({8, 12, 5});


cout << "After adding four more elements: Stack elements: ";
stack.printStack();
cout << "Average of the given stack values: " << stack.calculateAverage() << endl;

return 0;
}
Output:

You might also like