0% found this document useful (0 votes)
6 views28 pages

Dsa File

DSA practical file

Uploaded by

Gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views28 pages

Dsa File

DSA practical file

Uploaded by

Gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

GEETA UNIVERSITY

PANIPAT

BCA 3rd Semester


School of Computer Application
(2024-2025)

Data Structure & Algorithm

(CA3343)

Lab Practical file

Submitted To: - Ravi kumar


Submitted By:- Gaurav
GEETA UNIVERSITY, PANIPAT
LAB PRACTICAL FILE
LABORATORY:
DEPARTMENT OF
Data Structure & Algorithm SEMESTER: 3rd
School CSE

DATE PAGE
S.No. LIST OF EXPERIMENT SIGN.
NO.
Sorting Algorithms-Non-Recursive
1.

Sorting Algorithms-Recursive
2.

Searching Algorithm
3.

Implementation of Stack using Array


4.

Implementation of Queue using Array


5.

Implementation of Circular Queue using Array


6.

Implementation of Stack using Linked List


7.

Implementation of Queue using Linked List


8.

Implementation of Circular Queue using Linked List


9.

Implementation of Tree Structures, Binary Tree, Tree


10. Traversal, Binary Search Tree, Insertion and Deletion in
BST
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-1
Objective: Sorting Algorithms-Non-Recursive

#include <iostream>

#include <vector>

using namespace std;

void bubbleSort(vector<int>& arr) {

int n = arr.size();

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

swap(arr[j], arr[j + 1]);

int main() {

vector<int> arr = {64, 34, 25, 12, 22, 11, 90};

bubbleSort(arr);

for (int i = 0; i < arr.size(); i++) {

cout << arr[i] << " ";

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-2
Objective: Sorting Algorithm-Recursive
#include <iostream>
#include <vector>
using namespace std;

void merge(vector<int>& arr, int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int i = 0; i < n2; i++) R[i] = arr[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(vector<int>& arr, int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
mergeSort(arr, 0, arr.size() - 1);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
return 0;
}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-3
Objective: Searching Algorithm
#include <iostream>
#include <vector>
using namespace std;

int binarySearch(const vector<int>& arr, int l, int r, int x) {


while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}

int main() {
vector<int> arr = {2, 3, 4, 10, 40};
int x = 10;
int result = binarySearch(arr, 0, arr.size() - 1, x);
if (result != -1) cout << "Element found at index " << result;
else cout << "Element not found";
return 0;
}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-4
Objective: Implementation of Stack using Array

#include <iostream>

using namespace std;

class Stack {

private:

int arr[1000];

int top;

public:

Stack() { top = -1; }

bool push(int x) {

if (top >= 999) {

cout << "Stack Overflow";

return false;

arr[++top] = x;

return true;

int pop() {

if (top < 0) {

cout << "Stack Underflow";

return 0;

}
return arr[top--];

};

int main() {

Stack s;

s.push(10);

s.push(20);

s.push(30);

cout << s.pop() << " Popped from stack\n";

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-5
Objective: Implementation of Queue using Array

#include <iostream>

using namespace std;

class Queue {

private:

int arr[1000];

int front, rear;

public:

Queue() { front = rear = -1; }

bool enqueue(int x) {

if (rear >= 999) {

cout << "Queue Overflow";

return false;

arr[++rear] = x;

if (front == -1) front = 0;

return true;

int dequeue() {

if (front < 0 || front > rear) {

cout << "Queue Underflow";

return 0;
}

return arr[front++];

};

int main() {

Queue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

cout << q.dequeue() << " Dequeued from queue\n";

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-6
Objective: Implementation of Circular Queue using Array

#include <iostream>

using namespace std;

class CircularQueue {

private:

int arr[5];

int front, rear;

int size;

public:

CircularQueue() {

front = rear = -1;

size = 5;

bool enqueue(int x) {

if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) {

cout << "Queue Overflow";

return false;

else if (front == -1) {

front = rear = 0;

else if (rear == size - 1 && front != 0) {


rear = 0;

else {

rear++;

arr[rear] = x;

return true;

int dequeue() {

if (front == -1) {

cout << "Queue Underflow";

return 0;

int data = arr[front];

if (front == rear) {

front = -1;

rear = -1;

else if (front == size - 1) {

front = 0;

else {

front++;

return data;

};

int main() {
CircularQueue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

cout << q.dequeue() << " Dequeued from circular queue\n";

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-7
Objective: Implementation of Stack using Linked List

#include <iostream>

using namespace std;

class StackNode {

public:

int data;

StackNode* next;

StackNode(int d) {

data = d;

next = nullptr;

};

class Stack {

private:

StackNode* top;

public:

Stack() { top = nullptr; }

void push(int data) {

StackNode* newNode = new StackNode(data);

newNode->next = top;

top = newNode;

cout << data << " pushed to stack\n";

}
int pop() {

if (top == nullptr) {

cout << "Stack Underflow\n";

return 0;

StackNode* temp = top;

top = top->next;

int popped = temp->data;

delete temp;

return popped;

};

int main() {

Stack s;

s.push(10);

s.push(20);

s.push(30);

cout << s.pop() << " Popped from stack\n";

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-8
Objective: Implementation of Queue using Linked List

#include <iostream>

using namespace std;

class QueueNode {

public:

int data;

QueueNode* next;

QueueNode(int d) {

data = d;

next = nullptr;

};

class Queue {

private:

QueueNode *front, *rear;

public:

Queue() {

front = rear = nullptr;

void enqueue(int data) {

QueueNode* newNode = new QueueNode(data);


if (rear == nullptr) {

front = rear = newNode;

return;

rear->next = newNode;

rear = newNode;

int dequeue() {

if (front == nullptr) {

cout << "Queue Underflow\n";

return 0;

QueueNode* temp = front;

front = front->next;

if (front == nullptr) {

rear = nullptr;

int data = temp->data;

delete temp;

return data;

};

int main() {

Queue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

cout << q.dequeue() << " Dequeued from queue\n";


return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-9
Objective: Implementation of Circular Queue using Linked List

#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node(int d) {

data = d;

next = nullptr;

};

class CircularQueue {

private:

Node *front, *rear;

public:

CircularQueue() {

front = rear = nullptr;

void enqueue(int data) {


Node* newNode = new Node(data);

if (front == nullptr) {

front = rear = newNode;

rear->next = front;

} else {

rear->next = newNode;

rear = newNode;

rear->next = front;

int dequeue() {

if (front == nullptr) {

cout << "Queue is empty" << endl;

return -1;

int value;

if (front == rear) {

value = front->data;

delete front;

front = rear = nullptr;

} else {

Node* temp = front;

value = temp->data;

front = front->next;

rear->next = front;

delete temp;

return value;
}

void display() {

if (front == nullptr) {

cout << "Queue is empty" << endl;

return;

Node* temp = front;

cout << "Queue elements: ";

do {

cout << temp->data << " ";

temp = temp->next;

} while (temp != front);

cout << endl;

};

int main() {

CircularQueue q;

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

q.display();

cout << q.dequeue() << " dequeued from queue" << endl;

q.display();

return 0;

}
GEETA UNIVERSITY, PANIPAT
Experiment Tittle: EXPER. No.

PAGE No.

DEPARTMENT OF --- LABORATORY: Subject Name (Code) SEMESTER: 3rd

Experiment No-10
Objective: Implementation of Tree Structures, Binary Tree, Tree Traversal, Binary Search Tree,
Insertion and Deletion in BST
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* left;

Node* right;

Node(int d) {

data = d;

left = right = nullptr;

};

class BST {

public:

Node* root;

BST() { root = nullptr; }

Node* insert(Node* node, int data) {

if (node == nullptr) {

return new Node(data);


}

if (data < node->data) {

node->left = insert(node->left, data);

} else if (data > node->data) {

node->right = insert(node->right, data);

return node;

Node* minValueNode(Node* node) {

Node* current = node;

while (current && current->left != nullptr)

current = current->left;

return current;

Node* deleteNode(Node* root, int data) {

if (root == nullptr) return root;

if (data < root->data)

root->left = deleteNode(root->left, data);

else if (data > root->data)

root->right = deleteNode(root->right, data);

else {

if (root->left == nullptr) {

Node* temp = root->right;

delete root;

return temp;

} else if (root->right == nullptr) {

Node* temp = root->left;


delete root;

return temp;

Node* temp = minValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

void inorder(Node* root) {

if (root != nullptr) {

inorder(root->left);

cout << root->data << " ";

inorder(root->right);

void preorder(Node* root) {

if (root != nullptr) {

cout << root->data << " ";

preorder(root->left);

preorder(root->right);

void postorder(Node* root) {

if (root != nullptr) {

postorder(root->left);
postorder(root->right);

cout << root->data << " ";

};

int main() {

BST tree;

tree.root = tree.insert(tree.root, 50);

tree.insert(tree.root, 30);

tree.insert(tree.root, 20);

tree.insert(tree.root, 40);

tree.insert(tree.root, 70);

tree.insert(tree.root, 60);

tree.insert(tree.root, 80);

cout << "Inorder traversal: ";

tree.inorder(tree.root);

cout << endl;

cout << "Preorder traversal: ";

tree.preorder(tree.root);

cout << endl;

cout << "Postorder traversal: ";

tree.postorder(tree.root);

cout << endl;

cout << "Delete 20\n";


tree.deleteNode(tree.root, 20);

cout << "Inorder traversal: ";

tree.inorder(tree.root);

cout << endl;

return 0;

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