0% found this document useful (0 votes)
11 views

DSA Day 2

Uploaded by

engg.mohsin1
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)
11 views

DSA Day 2

Uploaded by

engg.mohsin1
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/ 14

UET LAHORE

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

First In First Out

Last In First Out

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;

void push(int val) {


if (is_full()) {
cout << "stack is full \n";
return;
}
top++;
arr[top] = val;
}
int pop_returnval() {
if (is_Empty()) {
cout << "stack is empty\n";
return -1111;
}
int to_return = arr[top];
arr[top] = 0;
top--;
return to_return;
}
void pop() {
if (is_Empty()) {
cout << "stack is empty\n";
return;
}
arr[top] = 0;
top--;
}
void print() {
if (top == -1) {
cout << "NOTING IN STACK!\n";
return;
}
for (int i = top; i >= 0; i--)
{
cout << " | " << arr[i] << " | " << endl;
}
cout << endl;
}
int get_val_from_top() {
if (top == -1) {
cout << "stack is empty\n";
return -11111;
}
return arr[top];
}
int Stack_size() {
return top + 1;
}
bool is_full() {
return top + 1 == n;
}
bool is_Empty() {
return top == -1;
}

};

Using linked list


class node {
public:
int data;
node* next;
node(int v) {
data = v;
next = nullptr;
}
};
class Stack {
public:
node* top = nullptr;
void push(int v) {
if (top == nullptr) {
top = new node(v);
}
else {
node* temp = new node(v);
temp->next = top;
top = temp;
}
}
void pop() {
if (is_Empty()) {
cout << "stack is empty\n";
}
else {
node* todelete = top;
top = top->next;
delete todelete;
}
}
int top_val() {
if (top == nullptr) {
return -111;
}
else {
return top->data;
}
}
bool is_Empty() {
return top == nullptr;
}
int Stack_size() {
if (top == nullptr) {
return 0;
}
else {
node* temp = top;
int counter = 0;
while (temp != nullptr) {
counter++;
temp = temp->next;
}
return counter;
}
}
void print() {
if (top == nullptr) {
cout << "stack is empty\n";
}
else {

node* temp = top;


while (temp != nullptr) {
cout << " | " << temp->data << " | " << endl;
temp = temp->next;
}
cout << endl;
}
}};
Just use BUILD IN Stack from library <stack>

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

o Processing Function Calls

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

using namespace std;

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;
}

void enQueue(int element) {


if (isFull()) {
cout << "Queue is full";
}
else {
if (front == -1) front = 0;
rear++;
items[rear] = element;
cout << endl
<< "Inserted " << element << endl;
}
}

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;
}
}
};

Linked list implementation of queues.


Priority Queues:
A priority queue is a special type of queue in which each element is associated with a priority
and is served according to its priority. If elements with the same priority occur, they are served
according to their order in the queue.
Banking System using a priority queue.
#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;

class node {
public:
string name;
string purpose;
int CNIC;
int priority;
node* next;

node(string n, char cs, string p, int nic) {


name = n;
purpose = p;
CNIC = nic;
switch (cs)
{
case 'D':
priority = 3; break;
case 'G':
priority = 2; break;
case 'S':
priority = 1; break;

}
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);

cout << "\tEnter your purpose ::";


getline(cin, purpose);
cout << "\tEnter your cnic ::";
cin >> cnic;
cout << "\tPlease enter the corresponding\n";
cout << "\tD:: Diamond card\n";
cout << "\tG:: Gold card\n";
cout << "\tS:: Silver card\n";
cin >> cs;
obj.Enqueue(name, cs, purpose, cnic);
cout << " SECESSFULLY ENTERED\n"; break;
case 2:
obj.dequeue();
cout << "SECESSFULLY REMOVED\n"; break;
case 3:
obj.print();
Sleep(1000);
break;
case 4:
exit = false; break;
}
Sleep(2000);
system("cls");
}

} Just use BUILD IN Stack from library <queue>


Circular Queues.
Application
• Queues are widely used as waiting lists for a single shared resource like a printer, disk, or CPU.
• Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.
• Queues are used to maintain the playlist in media players to add and remove the songs from the playlist.
• Queues are used in operating systems for handling interrupts
• Call Center phone systems use Queues to hold people calling them in order.

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>

using namespace std;


int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{
mid = (beg + end)/2;
/* if the item to be searched is present at middle */
if(a[mid] == val)
{
return mid+1;
}
/* if the item to be searched is smaller than middle, then it can only be in left subarray */
else if(a[mid] < val)
{
return binarySearch(a, mid+1, end, val);
}
/* if the item to be searched is greater than middle, then it can only be in right subarray */
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}

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;

void selectionsort(int arr[], int n)


{
int i, j, small;

for (i = 0; i < n-1; i++) // One by one move boundary of unsorted subarray
{ small = i; //minimum element in unsorted array

for (j = i+1; j < n; j++)


if (arr[j] < arr[small])
small = j;
// Swap the minimum element with the first element
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}}
Insertion sort
void insert(int a[], int n) /* function to sort an array with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j]) {


a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
Time complexity

Algorithms Best Case Worst Case


Bubble sort O(n) O(n2)
Selection sort O(n2) O(n2)
Insertion sort O(n) O(n2)
Binary Search O(1) O(logn)

References
https://www.programiz.com/dsa/queue
https://www.javatpoint.com/data-structure-queue

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy