1.array implementation of a stack:
Aim:
Write a program of array implementation of stack in c++
Algorithm:
Step 1: Initialize the stack with MAX_SIZE = 5, top = -1.
Step 2: Push elements into the stack: [1, 2, 3].
Step 3: Pop an element: returns 3, stack becomes [1, 2].
Step 4: Peek top element: returns 2.
Step 5: Check if stack is empty: returns False. 6. Check if stack is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Stack {
private:
int stack[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
void push(int item) {
if (top == MAX_SIZE - 1) {
cout << "Stack Overflow!" << endl;
return;
}
, stack[++top] = item;
cout << "Pushed item: " << item << endl;
}
int pop() {
if (top == -1) {
cout << "Stack Underflow!" << endl;
exit(1);
}
int item = stack[top--];
cout << "Popped item: " << item << endl;
return item;
}
void display() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
Stack stack;
stack.push(10);
, stack.push(20);
stack.push(30);
stack.display();
stack.pop();
stack.display();
return 0;
}
Output:
Pushed item: 10
Pushed item: 20
Pushed item: 30
Stack elements: 10 20 30
Popped item: 30
Stack elements: 10 20
, 2.array implementation of Queue
Aim:
Write a program of array implementation of queue in c++
Algorithm:
Step 1: Initialize the queue with MAX_SIZE = 5, front = -1, rear = -1.
Step 2: Enqueue elements into the queue: [1, 2, 3].
Step 3: Dequeue an element: returns 1, queue becomes [2, 3].
Step 4: Peek front element: returns 2.
Step 5: Check if queue is empty: returns False. 6. Check if queue is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int item) {
if (rear == MAX_SIZE - 1) {
Aim:
Write a program of array implementation of stack in c++
Algorithm:
Step 1: Initialize the stack with MAX_SIZE = 5, top = -1.
Step 2: Push elements into the stack: [1, 2, 3].
Step 3: Pop an element: returns 3, stack becomes [1, 2].
Step 4: Peek top element: returns 2.
Step 5: Check if stack is empty: returns False. 6. Check if stack is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Stack {
private:
int stack[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
void push(int item) {
if (top == MAX_SIZE - 1) {
cout << "Stack Overflow!" << endl;
return;
}
, stack[++top] = item;
cout << "Pushed item: " << item << endl;
}
int pop() {
if (top == -1) {
cout << "Stack Underflow!" << endl;
exit(1);
}
int item = stack[top--];
cout << "Popped item: " << item << endl;
return item;
}
void display() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
Stack stack;
stack.push(10);
, stack.push(20);
stack.push(30);
stack.display();
stack.pop();
stack.display();
return 0;
}
Output:
Pushed item: 10
Pushed item: 20
Pushed item: 30
Stack elements: 10 20 30
Popped item: 30
Stack elements: 10 20
, 2.array implementation of Queue
Aim:
Write a program of array implementation of queue in c++
Algorithm:
Step 1: Initialize the queue with MAX_SIZE = 5, front = -1, rear = -1.
Step 2: Enqueue elements into the queue: [1, 2, 3].
Step 3: Dequeue an element: returns 1, queue becomes [2, 3].
Step 4: Peek front element: returns 2.
Step 5: Check if queue is empty: returns False. 6. Check if queue is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int item) {
if (rear == MAX_SIZE - 1) {