DSA Lab 5
DSA Lab 5
Tools:
Eclipse IDE
JDK
Theory:
Stack: It is an ordered collection of data elements into which new elements may be inserted and from
which elements may be deleted at one end called the “TOP” of stack.
A stack is a last-in-first-out (LIFO) structure.
Insertion operation is referred as “PUSH” and deletion operation is referred as “POP”.
The most accessible element in the stack is the element at the position “TOP”.
Stack must be created as empty.
Whenever an element is pushed into stack, it must be checked whether the stack
is full or not.
Whenever an element is popped form stack, it must be checked whether the
stack is empty or not.
We can implement the stack ADT either with array or linked list.
Procedure:
1. Algorithm Push:
Step 1: if top> =max-1 then
Step 2: Display the stack
overflows
Step 3: else then
LAB MANUAL Data Structures and Algorithms
Step 4: top ++
Step 5: assign stack[top]=x
Step 6: Display element is inserted
2. Algorithm Pop:
Step 1: if top = =-1 then
Step 2: Display the stack is
underflows
Step 3: else
Step 4: assign
x=stack[top]
Step 5: top- -
Step 6: return x
3. Algorithm Peek:
4. Algorithm IsFull:
begin procedure isfull
end procedure
Tasks:
Make functions implement below stack operations
Task-1: Implementation for Push Code:
#include <iostream>
#define MAX 100
using namespace std; //Huzaifa Ali 59384
class Stack {
private:
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void push(int value) {
if (top >= MAX - 1) {
LAB MANUAL Data Structures and Algorithms
cout << "Stack overflow! Cannot push " << value << endl;
} else {
top++;
arr[top] = value;
cout << value << " pushed onto the stack." << endl;
}
}
void display() {
if (top == -1) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stack;
int choice, value;
cout << "Enter the first element to push into the stack: ";
cin >> value;
stack.push(value);
while (true) {
cout << "\nChoose an option:\n";
cout << "1. Display stack\n";
cout << "2. Push another element\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
stack.display();
break;
case 2:
LAB MANUAL Data Structures and Algorithms
Observations:
In this lab, we have explored the concept of stacks and various stack-related functions. Throughout our work, we
successfully executed both the push and pop operations on the stack, in addition to verifying the stack's empty
status.
LAB MANUAL Data Structures and Algorithms
Rubrics:
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented a
provided laboratory to construct functional/ working
instructions instructions the working model/ logic/
properly. and familiar fundamental schematic/ circuit/ block
The student with the lab schematic/ model/ block diagram/ code
can name the environment block diagram/ and
Demonstration hardware or (Trainer/ diagram/ code, and successfully
simulation software/ code/ model have executed the
platform, but IDE), but on the successfully lab objective in
unable to cannot protoboard/ executed the Realtime or in
implement implement on trainer/ program/ run a simulation
anything the platform simulation circuit on environment
practically or practically or software. software and produced
on the software on the platform the desired
software results
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature
Tools:
VS Code
Dev C++
Compare minimum with the third element. Again, if the third element is smaller, then assign
minimum to the third element otherwise do nothing. The process goes on until the last
element.
3. After each iteration, minimum is placed in the front of the unsorted list.
4. For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated
until all the elements are placed at their correct positions.
Insertion Sort:
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in
your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted
part are picked and placed at the correct position in the sorted part.
Characteristics of Insertion Sort:
This algorithm is one of the simplest algorithms with simple implementation
Basically, Insertion sort is efficient for small data values
Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already
partially sorted.
Compare key with the first element. If the first element is greater than key, then key is placed in front
of the first element.
2. Now, the first two elements are sorted.
Take the third element and compare it with the elements on the left of it. Placed it just behind
the element smaller than it. If there is no element smaller than it, then place it at the
beginning of the array.
3. Similarly, place every unsorted element at its correct position.
Output
Observations:
During this lab, we explored three sorting techniques, bubble sort, insertion sort and selection sort
delving into their implementations and distinguishing characteristics. These method include Selection
Sort.
LAB MANUAL Data Structures and Algorithms
Rubrics:
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented a
provided laboratory to construct functional/ working
instructions instructions the working model/ logic/
properly. and familiar fundamental schematic/ circuit/ block
The student with the lab schematic/ model/ block diagram/ code
can name the environment block diagram/ and
Demonstration hardware or (Trainer/ diagram/ code, and successfully
simulation software/ code/ model have executed the
platform, but IDE), but on the successfully lab objective in
unable to cannot protoboard/ executed the Realtime or in
implement implement on trainer/ program/ run a simulation
anything the platform simulation circuit on environment
practically or practically or software. software and produced
on the software on the platform the desired
software results
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature