DSA Assesment1
DSA Assesment1
PROGRAM CODE:
#include <iostream>
#define MAX_SIZE 5
class Stack {
private:
int* data;
int top;
public:
Stack() {
top = -1;
void display() {
if (isEmpty()) {
else {
}
cout << endl;
}
void push(int x) {
if (isFull()) {
cout << "Stack is full. Cannot push more elements." << endl;
else {
top++;
data[top] = x;
void pop() {
if (isEmpty()) {
cout << "Stack is empty. Cannot pop more elements." << endl;
else {
top--;
bool isEmpty() {
bool isFull() {
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.push(60);
s.pop();
s.pop();
s.display();
return 0;
OUTPUT:
ALGORITHM:
1.Declare a Queue class with a dynamic array arr, front_idx and rear_idx as
member variables.
2.Implement the Queue constructor that initializes front_idx and rear_idx to -1,
and dynamically allocates memory for arr.
3.Implement the enqueue function that inserts an element into the queue. If the
queue is empty, set both front_idx and rear_idx to 0. Otherwise, increment
rear_idx by 1. If rear_idx becomes equal to MAX_SIZE, display a message
indicating the queue is full, and decrement rear_idx to preserve the previous
state of the queue. Otherwise, insert the element at rear_idx.
4.Implement the dequeue function that removes an element from the front of
the queue. If the queue is empty, display a message indicating the queue is
empty. Otherwise, print the value of the dequeued element, increment front_idx
by 1 to remove it, and update the value of front_idx.
6.Implement the display function that displays all the elements in the queue.
PROGRAM CODE:
#include <iostream>
using namespace std;
# define MAX_SIZE 5
class Queue {
int* arr;
int front_idx;
int rear_idx;
public:
Queue() {
front_idx = -1;
rear_idx = -1;
void enqueue(int x) {
front_idx = 0;
rear_idx = 0;
} else {
rear_idx++;
if (rear_idx == MAX_SIZE) {
rear_idx--;
} else {
arr[rear_idx] = x;
void dequeue() {
} else {
void print_front_rear() {
void display() {
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.display();
q.dequeue();
q.print_front_rear();
q.display();
q.dequeue();
q.dequeue();
q.print_front_rear();
q.display();
q.dequeue();
q.dequeue();
q.print_front_rear();
q.display();
q.dequeue();
return 0;
OUTPUT: