0% found this document useful (0 votes)
17 views5 pages

Yatin

ghygu

Uploaded by

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

Yatin

ghygu

Uploaded by

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

Experiment 1.

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.

2. Objective: To understand stacks.

3. Algorithm:
Initialization:

 Initialize an array a of size MAX to store stack elements.


 Initialize an integer top to -1 to indicate an empty stack.

Push Operation (push(int x)):

 Input: Integer x to be pushed onto the stack.


 Check if top is equal to MAX - 1:
o If true, print "Stack Overflow".
o If false:
 Increment top by 1.
 Store x at index top of array a.
 Print "x pushed into stack".

Pop Operation (pop()):

 Check if top is less than 0:


o If true, print "Stack Underflow" and return 0.
 Retrieve the element at index top of array a.
 Decrement top by 1.
 Return the retrieved element.
Peek Operation (peek()):

 Check if top is less than 0:


o If true, print "Stack is Empty" and return 0.
 Retrieve the element at index top of array a.
 Return the retrieved element.

isEmpty Operation (isEmpty()):

 Return true if top is less than 0.


 Return false otherwise.

4. Implementation/Code:

#include<iostream>

using namespace std;

#define MAX 3

class Stack {
int top;

public:
int a[MAX];

Stack() { top = -1; }

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";

//top element after popping


cout << "Top element is : " << s.peek() << endl;

//print all elements


cout <<"Elements present in stack : ";
while(!s.isEmpty())
{
// print top element
cout << s.peek() <<" ";
// remove top element
s.pop();

}
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.

You might also like