DSA Nish

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

DATA STRUCTURE(CE0417)

Practical-1
AIM.1: WRITE A PROGRAM TO IMPLEMENT SELECTION SORT:
PROGRAM:
#include <iostream>
using namespace std;
void selection(int arr[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++)
{
small = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}
void printArr(int a[], int n) /* function to print the array */
{
int i;
for (i = 0; i < n; i++)
cout<< a[i] <<" ";
}
int main()
{
int a[] = { 80, 10, 29, 11, 8, 30, 15 };
int n = sizeof(a) / sizeof(a[0]);
cout<< "Before sorting array elements are - "<<endl;

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

printArr(a, n);
selection(a, n);
cout<< "\nAfter sorting array elements are - "<<endl;
printArr(a, n);
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-2
AIM.1(b): IMPLEMENT BUBBLE SORT AND INSERTION SORT ?
PROGRAM:
#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]) {
swap(arr[j], arr[j+1]);
}
}
}
}
void insertionSort(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
(j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original Array: ";
printArray(arr, n);
bubbleSort(arr, n);
cout << "Array after Bubble Sort: ";
printArray(arr, n);
int originalArr[] = {64, 34, 25, 12, 22, 11, 90};
for (int i = 0; i < n; i++) {
arr[i] = originalArr[i];
}
insertionSort(arr, n);
cout << "Array after Insertion Sort: ";
printArray(arr, n)
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-3
AIM.1(c): IMPLEMENT QUICK SORT AND MERGE SORT
PROGRAM:
#include<iostream>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;

for (int j = low; j <= high - 1; 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 pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
void merge(int arr[], int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

L[i] = arr[left + i];


for (int j = 0; j < n2; j++)
R[j] = arr[middle + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int middle = left + (right - left) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

merge(arr, left, middle, right);


}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original Array: ";
printArray(arr,n);
quickSort(arr, 0, n - 1);
cout << "Array after Quick Sort: ";
printArray(arr, n);
int originalArr[] = {64, 34, 25, 12, 22, 11, 90};
for (int i = 0; i < n; i++) {
arr[i] = originalArr[i];
}
mergeSort(arr, 0, n - 1);
cout << "Array after Merge Sort: ";
printArray(arr, n);
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-4
AIM.2 IMPLEMENT LINEAR SEARCH AND BINARY SERACH:
PROGRAM:
#include <iostream>
#include <algorithm>
using namespace std;
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

}
cout << endl;
}
int main() {
int arr[] = {11, 12, 22, 25, 34, 64, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original Sorted Array: ";
printArray(arr, n);
int key;
cout << "Enter the key for Linear Search: ";
cin >> key;
int linearResult = linearSearch(arr, n, key);
if (linearResult != -1) {
cout << "Linear Search: Key found at index " << linearResult << endl;
} else {
cout << "Linear Search: Key not found" << endl;
}
sort(arr, arr + n);
cout << "Array after sorting: ";
printArray(arr, n);
cout << "Enter the key for Binary Search: ";
cin >> key;
int binaryResult = binarySearch(arr, 0, n - 1, key);
if (binaryResult != -1) {
cout << "Binary Search: Key found at index " << binaryResult << endl;
} else {
cout << "Binary Search: Key not found" << endl;
}
return 0;
}

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-5
AIM : Write a program to implement following STACK operations.
PUSH (2) POP (3) PEEP (4) CHANGE (5) DISPLAY:
PROGRAM:
#include <iostream>
using namespace std;
#define MAX_SIZE 10
class Stack {
private:
int arr[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX_SIZE - 1;
}
void push(int value) {
if (isFull()) {
cout << "Error: Stack overflow\n";
} else {
arr[++top] = value;
cout << "Pushed " << value << " onto the stack\n";
}
}
void pop() {

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

if (isEmpty()) {
cout << "Error: Stack underflow\n";
} else {
cout << "Popped " << arr[top--] << " from the stack\n";
}
}
void peep() {
if (isEmpty()) {
cout << "Stack is empty\n";
} else {
cout << "Top element of the stack: " << arr[top] << endl;
}
}
void change(int value) {
if (isEmpty()) {
cout << "Error: Stack is empty, cannot change\n";
} else {
arr[top] = value;
cout << "Changed top element to " << value << endl;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty\n";
} else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

}
}
};
int main() {
Stack stack;
stack.push(2);
stack.push(4);
stack.push(6);
stack.display();
stack.pop();
stack.display();
stack.peep();
stack.change(8);
stack.display();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-6
AIM: Write a program to implement following QUEUE operations.
INSERT (2) DELETE (3) DISPLAY:
PROGRAM:
#include <iostream>
using namespace std;
#define MAX_SIZE 10
class Queue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
Queue() {
front = rear = -1;
}
bool isEmpty() {
return front == -1;
}
bool isFull() {
return (rear + 1) % MAX_SIZE == front;
}
void insert(int value) {
if (isFull()) {
cout << "Error: Queue overflow\n";
} else {
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

arr[rear] = value;
cout << "Inserted " << value << " into the queue\n";
}
}
void remove() {
if (isEmpty()) {
cout << "Error: Queue underflow\n";
} else {
cout << "Deleted " << arr[front] << " from the queue\n";
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty\n";
} else {
cout << "Queue elements: ";
int i = front;
do {
cout << arr[i] << " ";
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
cout << endl;
}
}
};

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

int main() {
Queue queue;
queue.insert(2);
queue.insert(4);
queue.insert(6);
queue.display();
queue.remove();
queue.display();
queue.insert(8);
queue.display();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-7
AIM: Write a program to implement following CQUEUE operations.
INSERT (2) DELETE (3) DISPLAY:
PROGRAM:
#include <iostream>
using namespace std;
#define MAX_SIZE 5
class CircularQueue {
private:
int arr[MAX_SIZE];
int front, rear;
public:
CircularQueue() {
front = rear = -1;
}
bool isEmpty() {
return front == -1;
}
bool isFull() {
return (rear + 1) % MAX_SIZE == front;
}
void insert(int value) {
if (isFull()) {
cout << "Error: Circular Queue overflow\n";
} else {
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

arr[rear] = value;
cout << "Inserted " << value << " into the circular queue\n";
}
}
void remove() {
if (isEmpty()) {
cout << "Error: Circular Queue underflow\n";
} else {
cout << "Deleted " << arr[front] << " from the circular queue\n";
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}
}
void display() {
if (isEmpty()) {
cout << "Circular Queue is empty\n";
} else {
cout << "Circular Queue elements: ";
int i = front;
do {
cout << arr[i] << " ";
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
cout << endl;
}
}
};

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

int main() {
CircularQueue cqueue;
cqueue.insert(2);
cqueue.insert(4);
cqueue.insert(6);
cqueue.display();
cqueue.remove();
cqueue.display();
cqueue.insert(8);
cqueue.display();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-8
AIM: WRITE A PROGRAM TO IMPLEMENT SINGLE LINKED LIST:
PROGRAM:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
void display() {
Node* current = head;

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

while (current != nullptr) {


cout << current->data << " ";
current = current->next;
}
cout << endl;
}
~LinkedList() {
Node* current = head;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
}
};
int main() {
LinkedList myList;
myList.insert(2);
myList.insert(4);
myList.insert(6);
cout << "Linked List: ";
myList.display();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-9
AIM: Write a program to implement following operations of the doubly linked list
PROGRAM:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node* prev;
Node(int value) : data(value), next(nullptr), prev(nullptr) {}
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

void displayForward() {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
void displayBackward() {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
while (current != nullptr) {
cout << current->data << " ";
current = current->prev;
}
cout << endl;
}
~DoublyLinkedList() {
Node* current = head;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
}
};
int main() {

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

DoublyLinkedList myList;
myList.insert(2);
myList.insert(4);
myList.insert(6);
cout << "Doubly Linked List (Forward): ";
myList.displayForward();
cout << "Doubly Linked List (Backward): ";
myList.displayBackward();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

Practical-10
AIM: Write a program to implement stack using linked list
PROGRAM:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class Stack {
private:
Node* top;
public:
Stack() : top(nullptr) {}
bool isEmpty() {
return top == nullptr;
}
void push(int value) {
Node* newNode = new Node(value);
newNode->next = top;
top = newNode;
cout << "Pushed " << value << " onto the stack\n";
}
void pop() {
if (isEmpty()) {
cout << "Error: Stack underflow\n";
} else {
Node* temp = top;

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

top = top->next;
cout << "Popped " << temp->data << " from the stack\n";
delete temp;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty\n";
} else {
cout << "Stack elements: ";
Node* current = top;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
}
~Stack() {
Node* current = top;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
}
};
int main() {
Stack myStack;

NISH MEHTA IU2041050070


DATA STRUCTURE(CE0417)

myStack.push(2);
myStack.push(4);
myStack.push(6);
myStack.display();
myStack.pop();
myStack.display();
myStack.push(8);
myStack.display();
return 0;
}
OUTPUT:

NISH MEHTA IU2041050070

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