Stack 0:
#include <iostream>
class Stack {
public:
int *array;
int top;
int capacity;
public:
Stack(int capacity) {
this->capacity = capacity;
array = new int[capacity];
top = -1;
}
void push(int element) {
if (top == capacity - 1) {
std::cout << "Stack is full. Cannot push element.\n";
} else {
top++;
array[top] = element;
std::cout << "Pushed element: " << element << std::endl;
}
}
int pop() {
if (top == -1) {
std::cout << "Stack is empty. Cannot pop element.\n";
return -1;
} else {
int poppedElement = array[top];
top--;
std::cout << "Popped element: " << poppedElement << std::endl;
return poppedElement;
}
}
int peek() {
if (top == -1) {
std::cout << "Stack is empty. No element to peek.\n";
return -1;
} else {
std::cout << "Peeked element: " << array[top] << std::endl;
return array[top];
}
}
bool isEmpty() {
return (top == -1);
}
~Stack() {
delete[] array;
}
};
int main() {
Stack stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
stack.push(40);
stack.push(50);
stack.pop();
stack.pop();
stack.pop();
stack.pop();
return 0;
}
Stack 1:
#include <iostream>
class StackAL
{
public:
static const int MAX_SIZE = 1000;
int list[MAX_SIZE];
int top;
public:
StackAL() {
top = -1;
}
void push(int data) {
if (top >= MAX_SIZE - 1) {
std::cout << "Stack Overflow\n";
return;
}
list[++top] = data;
}
bool isEmpty() {
return top == -1;
}
int pop() {
if (isEmpty()) {
std::cout << "Stack Underflow\n";
return -1;
}
return list[top--];
}
int peek() {
if (isEmpty()) {
std::cout << "Stack is empty\n";
return -1;
}
return list[top];
}
};
int main() {
StackAL stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while (!stack.isEmpty()) {
std::cout << stack.peek() << std::endl;
std::cout << stack.pop() << std::endl;
}
return 0;
}
Stack 2:
#include <iostream>
#include <stack>
int main()
{
std::stack<int> stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while (!stack.empty())
{
std::cout << stack.top() << std::endl;
stack.pop();
}
return 0;
}
Stack 3:
#include <iostream>
class StackDS {
private:
struct Node {
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};
public:
class Stack {
private:
Node* head;
public:
Stack() : head(nullptr) {}
void push(int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
return;
}
newNode->next = head;
head = newNode;
}
bool isEmpty() {
return head == nullptr;
}
int pop() {
if (isEmpty()) {
return -1;
}
Node* top = head;
int data = top->data;
head = head->next;
delete top;
return data;
}
int peek() {
if (isEmpty()) {
return -1;
}
return head->data;
}
~Stack() {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
};
static void main() {
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while (!stack.isEmpty()) {
std::cout << stack.peek() << std::endl;
stack.pop();
}
}
};
int main() {
StackDS::main();
return 0;
}
Stack 4:
#include <iostream>
#include <stack>
void pushAtBottom(std::stack<int>& s, int data) {
if (s.empty()) {
s.push(data);
return;
}
int temp = s.top();
s.pop();
pushAtBottom(s, data);
s.push(temp);
}
int main() {
std::stack<int> stack;
stack.push(1);
stack.push(2);
stack.push(3);
pushAtBottom(stack, 4);
while (!stack.empty()) {
std::cout << stack.top() << std::endl;
stack.pop();
}
return 0;
}
Stack 5:
#include <iostream>
#include <stack>
// Function to push an element at the bottom of a stack
void pushAtBottom(std::stack<int>& s, int data) {
if (s.empty()) {
s.push(data);
return;
}
int temp = s.top();
s.pop();
pushAtBottom(s, data);
s.push(temp);
}
// Function to reverse a stack
void reverse(std::stack<int>& s) {
if (s.empty()) {
return;
}
int top = s.top();
s.pop();
reverse(s);
pushAtBottom(s, top);
}
int main() {
std::stack<int> stack;
stack.push(1);
stack.push(2);
stack.push(3);
// Reverse the stack
reverse(stack);
// Print the reversed stack
while (!stack.empty()) {
std::cout << stack.top() << std::endl;
stack.pop();
}
return 0;
}
No comments:
Post a Comment
Fell free to write your query in comment. Your Comments will be fully encouraged.