0% found this document useful (0 votes)
13 views4 pages

Stack Operations in C++ Templates

The document outlines an experiment for a Computer Science and Engineering course focused on implementing stack operations using C++ templates. It includes objectives such as understanding stack operations, handling underflow and overflow conditions, and applying object-oriented design principles. The experiment also provides a code implementation, expected outputs, and complexity analysis for the stack operations.

Uploaded by

Krishna Sharma
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)
13 views4 pages

Stack Operations in C++ Templates

The document outlines an experiment for a Computer Science and Engineering course focused on implementing stack operations using C++ templates. It includes objectives such as understanding stack operations, handling underflow and overflow conditions, and applying object-oriented design principles. The experiment also provides a code implementation, expected outputs, and complexity analysis for the stack operations.

Uploaded by

Krishna Sharma
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

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING
Experiment 1.1
Student Name: Daksh Chandel UID: 23BCS11828
Branch: BE-CSE Section/Group:23BCS_FS-605 (A)
Semester: 5th Date of Performance:31/07/2025
Subject Name: DAA
Subject Code:23CSH-301

1. Aim: -
Analyze if the stack is empty or full, and if elements are present, return the top element in the stack
using templates. Also, perform push and pop operations on the stack.

2. Objective:-
Understand Stack Operations:- Implement push, pop, isEmpty, isFull, and peek (top)
functions for a stack.

Use of Templates:- Use C++ templates to make the stack generic (i.e., it can handle any data
type like int, float, char, string, etc.

Condition Handling:- Handle underflow (when popping from an empty stack) and overflow
(when pushing into a full stack) conditions.

Object-Oriented Design:- Apply object-oriented programming (OOP) principles such as


encapsulation and class-based design

Practical Understanding of Stack Applications:- Strengthen your ability to use stacks in real-
world programming scenarios like function calls, expression parsing, backtracking, etc.

3. Software Used:- Visual Studio, JetBrains CLion

4. Procedure/Algorithm:-

a. Step1: Create stack.


b. Step2: Check underflow and overflow condition.
c. Step3: Increment top to store element in stack.
d. Step4: Decrement top after removing element form stack.
e. Step5: Check is stack empty or not.

23BCS11828 Daksh Chandel


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
5. CODE:-

#include <iostream> using


namespace std;

template <class T>


class Stack {
private:
int top; int
size;
T* arr;

public:
Stack(int s)
{ size = s;
arr = new T[size]; top
= -1;
}

~Stack()
{ delete[]
arr;
}

void push(T value) {


if (isFull()) {
cout << "Stack is full. Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << "Pushed: " << value << endl;
}
}

void pop() {
if (isEmpty()) {
cout << "Stack is empty. Cannot pop." << endl;
} else {
cout << "Popped: " << arr[top--] << endl;
}
}

23BCS11828 Daksh Chandel


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
bool isEmpty()
{ return top == -1;
}

bool isFull() {
return top == size - 1;
}

T peek() {
if (!isEmpty())
{ return
arr[top];
} else {
throw runtime_error("Stack is empty. No top element.");
}
}
};

int main() {
Stack<int> s(5); // Stack of integers with size 5

[Link](10);
[Link](20);
[Link](30);

if (![Link]()) {
cout << "Top element: " << [Link]() << endl;
}

[Link]();
[Link]();
[Link]();
[Link](); // Try popping from empty stack

if ([Link]()) {
cout << "Stack is now empty." << endl;
}

[Link](100);
cout << "Top element after pushing 100: " << [Link]() << endl;

return 0;
}

23BCS11828 Daksh Chandel


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
6. Output:-

Fig:-VS Code Output Console

7. Complexity Analysis:-

 Time Complexity [All Operations(Push,Pop,Peek,IsEmpty,IsFull)]:- O(1)

 Space Complexity:- O(n) (where n is the size of the stack)

23BCS11828 Daksh Chandel

You might also like