dsa2
dsa2
dsa2
& TECHNOLOGY
PRACTICAL FILE
CODE:-
#include <iostream>
using namespace std;
void insertElement(int arr[], int& n, int element, int position) {
for (int i = n; i >= position; i--) {
arr[i] = arr[i - 1];
}
arr[position - 1] = element;
n++;
}
int main() {
int arr[100], n, element, position;
cout << "Enter the number of elements in the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the element to insert: ";
cin >> element;
cout << "Enter the position to insert the element: ";
cin >> position;
if (position < 1 || position > n + 1) {
cout << "Invalid position!" << endl;
return 1;
}
insertElement(arr, n, element, position);
cout << "Array after insertion: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
RESULT:-
PROGRAM-2
CODE:-
#include <iostream>
using namespace std;
int findElement(int arr[], int n, int key);
int deleteElement(int arr[], int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == -1) {
cout << "Element not found";
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
int main(){
int i;
int arr[] = { 10, 50, 30, 40, 20 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
cout << "Array before deletion\n";
CODE:-
#include <iostream>
using namespace std;
class Stack {
private:
int top;
int size;
int* stack;
public:
Stack(int s) {
size = s;
stack = new int[size];
top = -1;
}
~Stack() {
delete[] stack;
}
void push(int value) {
if (top == size - 1) {
cout << "Stack overflow." << endl;
return;
}
stack[++top] = value;
cout << "Pushed " << value << endl;
}
void pop() {
if (top == -1) {
cout << "Stack underflow." << endl;
return;
}
cout << "Popped " << stack[top--] << endl;
}
void display() {
if (top == -1) {
cout << "Stack is empty." << endl;
return;
}
int main() {
Stack s(5);
s.push(3);
s.push(6);
s.push(9);
s.push(12);
s.push(15);
s.push(18);
s.display();
s.pop();
s.pop();
s.display();
return 0;
}
RESULT:-
PROGRAM-4
CODE:-
#include <iostream>
using namespace std;
class Queue {
private:
int front, rear, size;
int* queue;
public:
Queue(int s) {
front = rear = -1;
size = s;
queue = new int[size];
}
~Queue() {
delete[] queue;
}
void enqueue(int value) {
if (rear == size - 1) {
cout << "Queue is full." << endl;
return;
}
if (front == -1) {
front = 0;
}
queue[++rear] = value;
cout << "Enqueued " << value << endl;
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue is empty." << endl;
return;
}
cout << "Dequeued " << queue[front++] << endl;
if (front > rear) {
front = rear = -1;
}
}
void display() {
if (front == -1) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";
for (int i = front; i <= rear; ++i) {
cout << queue[i] << " ";
}
cout << endl;
}
};
int main() {
Queue q(5);
q.enqueue(3);
q.enqueue(6);
q.enqueue(9);
q.enqueue(12);
q.enqueue(15);
q.enqueue(18);
q.display();
q.dequeue();
q.dequeue();
q.display();
return 0;
}
RESULT:-
PROGRAM-5
CODE:-
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int* leftArray = new int[n1];
int* rightArray = new int[n2];
for (int i = 0; i < n1; i++)
leftArray[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArray[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArray[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArray[j];
j++;
k++;
}
delete[] leftArray;
delete[] rightArray;
}
void mergeSort(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);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {13,6,22,14,9,17};
int arrSize = sizeof(arr) / sizeof(arr[0]);
cout << "Given array is: ";
printArray(arr, arrSize);
mergeSort(arr, 0, arrSize - 1);
cout << "Sorted array is: ";
printArray(arr, arrSize);
return 0;
}
RESULT:-
PROGRAM-6
CODE:-
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
RESULT:-
PROGRAM-7
CODE:-
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {22,16,24,19,13,7};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
insertionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
RESULT:-
PROGRAM-8
CODE:-
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {34,19,22,7,45,26};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
RESULT:-
PROGRAM-9
CODE:-
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {13,22,17,9,22,15};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
RESULT:-
PROGRAM-10
CODE:-
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
void insertAtBeginning(int data) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
}
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
LinkedList list;
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
list.display();
return 0;
}
RESULT:-