DSA Day 2
DSA Day 2
NEW CAMPUS
Programming
Session
Phase 1
DSA
Guidelines for Effective Learning
1. Carefully read the provided documents and references to build a strong foundation on the topic.
2. Engage actively with the content, clarify any doubts, and summarize key points in your own words.
3. Problems are given in group, you can collaborate with group members to solve the provided problems,
discussing and debating different approaches.
4. Regularly update your solutions on GitHub, ensuring clear organization and proper documentation.
5. If you encounter any problems, you can freely ask in the Competitive Programming group
Stack
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. Insertions and deletions of
an element can occur only at one end, i.e., at the top of the Stack. It is used in all those applications in which
data must be stored and retrieved in the last.
Implementation
Using Array
#include<iostream>
using namespace std;
#define n 20
class Stack {
public:
int arr[n];
int top = -1;
};
Application
o Evaluation of Arithmetic Expressions
Prefix, Postfix, Infix expression
o Backtracking
Backtracking is another application of Stack. It is a recursive algorithm that is used for solving
the optimization problem.
o Delimiter Checking
o Converting Decimal to Binary
Queue
A queue can be defined as an ordered list which enables insert operations to be performed at one end called
REAR and delete operations to be performed at another end called FRONT. Queue is referred to as the First In
First Out list.
Implementation
Using Array
#include <iostream>
#define SIZE 10
class Queue {
private:
int items[SIZE], front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
bool isEmpty() {
if (front == -1)
return true;
else
return false;
}
int deQueue() {
int element;
if (isEmpty()) {
cout << "Queue is empty" << endl;
return (-1);
}
else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after deleting it. */
else {
front++;
}
cout << "Deleted -> " << element << endl;
return (element); }}
void display() {
if (isEmpty()) {
cout << "Empty Queue" << endl;
}
else {
cout << endl
<< "Front index-> " << front;
cout << endl
<< "Items -> ";
for (int i = front; i <= rear; i++)
cout << items[i] << " ";
cout << endl
<< "Rear index-> " << rear << endl;
}
}
};
class node {
public:
string name;
string purpose;
int CNIC;
int priority;
node* next;
}
next = nullptr;
}
};
class P_Queue {
public:
node* front = nullptr;
void Enqueue(string n, char cs, string p, int nic) {
node* entered = new node(n, cs, p, nic);
if (front == nullptr) {
front = entered;
}
else {
node* prev = nullptr;
node* travel = front;
while (travel != nullptr) {
if (travel->priority < entered->priority) {
break;
}
prev = travel;
travel = travel->next;
}
if (prev == nullptr) {
entered->next = front;
front = entered;
return;
}
entered->next = prev->next;
prev->next = entered;
}
}
void dequeue() {
if (front == nullptr) {
cout << "Queue is empty\n";
}
else {
node* todelete = front;
front = front->next;
delete todelete;
}
}
void print() {
if (front == nullptr) {
cout << "Queue is empty\n";
}
else {
int count = 1;
node* travel = front;
while (travel != nullptr) {
cout <<
"\t====================================================================\n";
cout << "\t--------------COSTUMER " << count << "----------------------
-------\n";
cout << "\t NAME ::" << travel->name << endl;
cout << "\t PURPOSE ::" << travel->purpose << endl;
cout << "\t CNIC ::" << travel->CNIC << endl;
travel = travel->next;
count++;
}
}
}
};
int main() {
string name;
string purpose;
int cnic;
bool exit = true;
P_Queue obj;
int choice = 0;
while (exit) {
system("color 0b");
cout << R"(
=====================================================================
____ _ _ _____ _
| _ \ | | (_) / ____| | |
| |_) | __ _ _ __ | | ___ _ __ __ _ | (___ _ _ ___| |_ ___ _ __ ___
| _ < / _` | '_ \| |/ / | '_ \ / _` | \___ \| | | / __| __/ _ \ '_ ` _ \
| |_) | (_| | | | | <| | | | | (_| | ____) | |_| \__ \ || __/ | | | | |
|____/ \__,_|_| |_|_|\_\_|_| |_|\__, | |_____/ \__, |___/\__\___|_| |_| |_|
__/ | __/ |
|___/ |___/
=======================================================================
)";
cout << "\t=================================================================\n";
cout << "\t------------------MENU------------------------------------------\n";
cout << "\t=================================================================\n";
cout << "\t1.ENTER A USER ...........\n";
cout << "\t2.REMOVE A USET.............\n";
cout << "\t3.DISPLAY...............\n";
cout << "\t4.EXIT.....................\n";
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
char cs;
cout << "\tEnter your name ::";
getline(cin, name);
Algorithms
STABILITY
A sorting algorithm is said to be stable if two objects with equal keys appear in the same order
in sorted output as they appear in the input array to be sorted. Some sorting algorithms are
stable by nature like Insertion sort, Bubble Sort, etc. Some sorting algorithms are not, like
selection sort etc.
EFFICIENCY
Algorithm efficiency refers to how well an algorithm performs in terms of time and space
requirements. It measures the amount of resources an algorithm consumes to solve a problem.
Binary Search
#include <iostream>
Bubble sort
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 1; i < n ; i++)
{
for(j = 0; j < n - i; j++)
{
if(a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
Selection sort
#include <iostream>
using namespace std;
for (i = 0; i < n-1; i++) // One by one move boundary of unsorted subarray
{ small = i; //minimum element in unsorted array
References
https://www.programiz.com/dsa/queue
https://www.javatpoint.com/data-structure-queue