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

Worksheet 1.1

The document outlines a practical exercise for a student named Arihant Jain, focusing on stack operations using templates in C++. It includes code for implementing stack functionalities such as checking if the stack is empty or full, pushing and popping elements, and retrieving the top element, along with their time complexities. The learning outcomes emphasize understanding stack underflows, overflows, and basic operations.

Uploaded by

ayush20104
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)
5 views4 pages

Worksheet 1.1

The document outlines a practical exercise for a student named Arihant Jain, focusing on stack operations using templates in C++. It includes code for implementing stack functionalities such as checking if the stack is empty or full, pushing and popping elements, and retrieving the top element, along with their time complexities. The learning outcomes emphasize understanding stack underflows, overflows, and basic operations.

Uploaded by

ayush20104
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/ 4

WORKSHEET 1.

Student Name: Arihant Jain UID: 21BCS11123


Branch: CSE Section/Group: 601-B
Semester: Fifth Date of Performance: 04/08/2023
Subject Name: DAA Subject Code: 21CSH-311

1. Aim/Overview of the practical


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. Observations/Discussions/ Complexity Analysis:

Code:
#include <iostream>

template <typename T>


class Stack {
private:
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};

Node* top;

public:
Stack() : top(nullptr) {}
bool isEmpty() const {
return top == nullptr;
}

bool isFull() const {


return false;
}

void push(const T& value) {


Node* newNode = new Node(value);
newNode->next = top;
top = newNode;
}

void pop() {
if (isEmpty()) {
std::cout << "Stack is empty. Cannot pop.\n";
return;
}

Node* temp = top;


top = top->next;
delete temp;
}

T topElement() const {
if (isEmpty()) {
std::cout << "Stack is empty. No top element.\n";
return T();
}

return top->data;
}

~Stack() {
while (!isEmpty()) {
pop();
}
}
};

int main() {
Stack<int> stack;

stack.push(10);
stack.push(20);
stack.push(30);

std::cout << "Name: Arihant Jain\nUID 21BCS11123 \nSection 601-B


\n\n\n\nTop element: " << stack.topElement() << '\n';

stack.pop();

std::cout << "Top element after pop: " << stack.topElement() << '\n';

return 0;
}
3. Result/Output
The time complexity of this code is:
• For push Operation: O(1)
• For pop Operation: O(1)

Learning Outcomes:
• Learnt to analyse of the stack is empty and underflows.
• Learnt to check if the stack if full and overflows.
• Learnt to perform push and pop operation.
• Learnt how to return the top element.

You might also like