0% found this document useful (0 votes)
27 views8 pages

Nishant DAA 1.1

The document describes an experiment to implement a stack using templates in C++. It includes the objectives, input, procedure, sample code, observations and time complexity. The sample code shows how to create a stack class with push, pop, peek and other methods and test it by pushing and popping elements and checking if the stack is empty or full.

Uploaded by

Nishant Bhardwaj
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)
27 views8 pages

Nishant DAA 1.1

The document describes an experiment to implement a stack using templates in C++. It includes the objectives, input, procedure, sample code, observations and time complexity. The sample code shows how to create a stack class with push, pop, peek and other methods and test it by pushing and popping elements and checking if the stack is empty or full.

Uploaded by

Nishant Bhardwaj
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/ 8

PRACTICAL FILE

Student Name Nishant Bhardwaj


UID 21BCS7081
Section & 21BCS_IOT_633 A
Group
Department Computer Science & Engineering
Session July-Dec 2023
Course Name Design and Analysis of Algorithms with Lab
Course Code 21ITH-311/21CSH-311
Semester 5
TH

Department of Computer Science & Engineering


Chandigarh University, Mohali
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311
INDEX

S. No. Experiment Date Conduct Viva Worksheet Total Remarks


(12) (10) (8) (30)

1 Experiment 1.1: 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 Experiment 1.2: Develop a
program for implementation
of power function and
determine that complexity
should be O(log n) .
3 Experiment 1.3: Evaluate
the complexity of the
developed program to find
frequency of elements in a
given array.
4 Experiment 1.4:
i. Apply the concept of
Linked list and write
code to Insert and
Delete an element at
the beginning and
end of Singly
Linked List.
ii. Apply the concept of
Linked list and write
code to Insert and
Delete an element at
the beginning and at
end in Doubly and
Circular Linked List.
5 Experiment 2.1: Sort a
given set of elements using
the Quick sort method and
determine the time required
to sort the elements. Repeat
the experiment for different
values of n, the number of
elements in the list to be
sorted. The elements can be
read from a file or can be
generated using the random
number generator.

6 Experiment 2.2 Develop a


program and analyze
complexity to implement
subset-sum problem using
Dynamic Programming.
7 Experiment 2.3: Develop a
program and analyze
complexity to implement 0-1
Knapsack using Dynamic
Programming.
8 Experiment 3.1: Develop a
program and analyze
complexity to do a depth-
first search (DFS) on an
undirected graph.
Implementing an application
of DFS such as (i) to find the
topological sort of a directed
acyclic graph, OR (ii) to find
a path from source to goal in
a maze
9 Experiment 3.2: Develop a
program and analyze
complexity to find shortest
paths in a graph with positive
edge weights using Dijkstra’s
algorithm.
10 Experiment 3.3: Develop a
program and analyze
complexity to find all
occurrences of a pattern P in
a given string S.
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

Experiment 1.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.

Objectives:To understand Stack

Input/Apparatus Used:VS Code

Procedure/Algorithm:
Step1: Create stack.
Step2: Check underflow and overflow condition.
Step3: Increment top to store element in stack.
Step4: Decrement top after removing element form stack.
Step5: Check is stack empty or not.

Sample Code:
#include<iostream>
using namespace std;
class stack{
public:
int size;
int *arr;
int top;
stack(int size ){
this->size=size;
arr=new int[size];
top=-1;
}
void push(int element){
NishantBhardwaj UID: 21BCS7081
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311
if(top==size-1){
cout<<"Stack Overflow"<<endl;
}
top++;
arr[top]=element;
}
void pop(){
if(top==-1){
cout<<"Stack is empty"<<endl;
}
else if(top>=0){
top--;}
}
int peek() {
if(top>-1 && top<size){
return arr[top];
}
else{
cout<<"Stack is empty"<<endl;
return 0;}
}
bool isempty(){
if(top==-1){
return true;
}
else {
return false;
}
}
bool isfull(){
NishantBhardwaj UID: 21BCS7081
Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

if(top==size-1){
return true;
}
else{
return false;}
}
};
int main(){
stack st(5);
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout<<"Nishant Bhardwaj"<<endl;
cout<<"21BCS7081"<<endl;
cout<<"Top element of the stack is "<<st.peek()<<endl;
cout<<"Is stack is full ? " <<st.isfull()<<endl;
cout<<"Is stack is empty ? "<<st.isempty()<<endl;
st.pop();
cout<<"After Pop operation"<<endl;
cout<<"Top element of the stack is "<<st.peek()<<endl;}

NishantBhardwaj UID: 21BCS7081


Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311

Observations/Outcome :

Time Complexity:
Stack work on LIFO( Last in first out). The time complexity of all stack operations is constant
i.e. O(1).

NishantBhardwaj UID: 21BCS7081

You might also like