0% found this document useful (0 votes)
18 views

DSA Assesment1

Uploaded by

gawono2945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

DSA Assesment1

Uploaded by

gawono2945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

HARDIK MANGESH CHHALLANI

REG NO. : 22BCE2454.

AIM: To implement stack utilizing c++ classes.


ALGORITHM:
1.Create a class "Stack".
2.Define two private variables: "data" (integer array) and "top"
(integer variable).
3.Define a constructor to initialize the "data" array with the
maximum size "MAX_SIZE" and "top" with -1 (to indicate an
empty stack).
4.Define a public member function "display" to print all the
elements in the stack. If the stack is empty, print an
appropriate message.
5.Define a public member function "push" to add a new
element to the top of the stack. Check if the stack is already
full. If so, print an appropriate message. Otherwise, increment
"top" and add the new element to the "data" array.
6.Define a public member function "pop" to remove the top
element of the stack. Check if the stack is already empty. If so,
print an appropriate message. Otherwise, print the popped
element, decrement "top", and remove the element from the
"data" array.
7.Define two public member functions "isEmpty" and "isFull"
to check if the stack is empty or full, respectively.
8.In the main function, create a new object of the "Stack" class.
9.Push some elements into the stack using the "push"
function.
10.Attempt to push more elements than the maximum size of
the stack to check for overflow.
11.Pop some elements from the stack using the "pop"
function.
12.Display the remaining elements of the stack using the
"display" function.
13.Return 0 to exit the program.

PROGRAM CODE:
#include <iostream>

using namespace std;

#define MAX_SIZE 5

class Stack {

private:

int* data;

int top;

public:

Stack() {

data = new int[MAX_SIZE];

top = -1;

void display() {

if (isEmpty()) {

cout << "Stack is empty." << endl;

else {

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

cout << data[i] << " ";

}
cout << endl;

}
void push(int x) {

if (isFull()) {

cout << "Stack is full. Cannot push more elements." << endl;

else {

top++;

data[top] = x;

cout << "Pushed element: " << x << endl;

void pop() {

if (isEmpty()) {

cout << "Stack is empty. Cannot pop more elements." << endl;

else {

cout << "Popped element: " << data[top] << endl;

top--;

bool isEmpty() {

return top == -1;

bool isFull() {

return top == MAX_SIZE - 1;

};

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:

RESULT: The implementation of stack has been successful.


AIM: To implement simple queue using c++ classes

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.

5.Implement the print_front_rear function that displays the current values of


front_idx and rear_idx.

6.Implement the display function that displays all the elements in the queue.

7.Test the Queue class by creating an instance of it and performing a sequence


of enqueue and dequeue operations, checking for queue underflow and
overflow, and verifying that the front and rear indexes are updated correctly.

PROGRAM CODE:
#include <iostream>
using namespace std;

# define MAX_SIZE 5
class Queue {

int* arr;

int front_idx;

int rear_idx;

public:

Queue() {

arr = new int[MAX_SIZE];

front_idx = -1;

rear_idx = -1;

void enqueue(int x) {

if (front_idx == -1 && rear_idx == -1) {

front_idx = 0;

rear_idx = 0;

} else {

rear_idx++;

if (rear_idx == MAX_SIZE) {

cout << "Queue Overflow" << endl;

rear_idx--;

} else {

arr[rear_idx] = x;

void dequeue() {

if (front_idx == -1 || front_idx > rear_idx) {

cout << "Queue Underflow" << endl;

} else {

cout << "Dequeued element: " << arr[front_idx] << endl;


front_idx++;

void print_front_rear() {

cout << "Front index: " << front_idx << endl;

cout << "Rear index: " << rear_idx << endl;

void display() {

cout << "Queue elements: ";

for(int i = front_idx; i <= rear_idx; i++) {

cout << arr[i] << " ";

cout << endl;

};

int main() {

Queue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

q.enqueue(40);

q.enqueue(50);

q.enqueue(60); // Queue overflow

q.print_front_rear(); // Front index: 0, Rear index: 4

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:

RESULT: Simple queue has been implemented successfully

You might also like