Yatin
Yatin
1
Student Name: Yatin Rana UID:22BCS10204
Branch: BE-CSE Section/Group:701 A
Semester: 5 Date of Performance:20/7/24
Subject Name:DAA Subject Code:22CSH311
1. Aim: Analyze if stack Isempty, Isfull and if elements are present then return top
element in stacks using templates and also perform push and pop
operation in stack.
3. Algorithm:
Initialization:
4. Implementation/Code:
#include<iostream>
#define MAX 3
class Stack {
int top;
public:
int a[MAX];
bool push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow\n";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int pop()
{
if (top < 0) {
cout << "\nStack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool isEmpty()
{
return (top < 0);
}
};
int main()
{
class Stack s;
s.push(63);
s.push(400);
s.push(900);
s.push(350);
cout << s.pop() << " Popped from stack\n";
}
s.pop();
return 0;
}
5. Output:
6. Time Complexity :
All operations (push, pop, peek, isEmpty) in this stack implementation have a time complexity of
O(1) because because estack work on LIFO principle which have constant time complexity.
7. Learning Outcomes:
I. How to create a Stack.
II. Operations on stack.
III. Time Complexity.