You are on page 1of 2

Experiment Title: 4.

Student Name: Ayush Tiwari UID: 20BCS7611


Branch: BE-CSE Section/Group: 20BCS_WM_905-B
Semester: 5th Date of Performance:
Subject Name: DAA Lab Subject Code: 20CSP-312

1. Aim/Overview of the practical: Code to push & pop and check Isempty, Isfull and Return
top element in stacks using templates.

2. Task to be done/ Which logistics used: The task is to implement stack data structure using
templates and execute functions like push(), pop(), isEmpty(), isFull() and top().

3. Algorithm/Flowchart (For programming based labs):


• Create a stack using template Stack<int>.
• Insert elements into stack using push() operation.
• Create a display function to display all elements in stack.
• Remove element from stack using pop() operation.
4. Steps for experiment/practical/Code:
#include<bits/stdc++.h>
using namespace std;
void display(stack<int> s){
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
int main(){
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
cout<<"Original Stack: "<<endl;
display(s);
cout<<"Top element in stack: "<<s.top()<<endl;
s.pop();
cout<<"Stack after poping element: "<<endl;
display(s);
}
5. Observations/Discussions/ Complexity Analysis:

• Time Complexity: O(1)


• Space Complexity: O(1)

6. Result/Output/Writing Summary:

Output

Learning outcomes (What I have learnt):

1. Learnt about stack and its implementation using templates.

2. Learnt how to insert element into stack using push operation.

3. Learnt how to remove element from stack using pop operation.

4. Learnt how to analyze time and space complexity.

You might also like