DATA STRUCTURE LAB
DATA STRUCTURE LAB
(b)Queue ADT
(b)Selection sort
#include <iostream>
class ArrayList {
private:
int arr[MAX_SIZE];
int size;
public:
ArrayList() : size(0) {}
arr[size++] = value;
} else {
if (arr[i] == value) {
size--;
return;
return;
};
int main() {
ArrayList arrayList;
arrayList.insert(10);
arrayList.insert(20);
arrayList.insert(30);
arrayList.display();
arrayList.remove(20);
arrayList.display();
return 0;
}
OUTPUT:
#include <iostream>
class Node {
public:
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList() : head(nullptr) {}
if (!head) {
head = newNode;
} else {
while (temp->next) {
temp = temp->next;
temp->next = newNode;
if (!head) return;
if (head->data == value) {
delete temp;
return;
current = current->next;
if (current->next) {
current->next = current->next->next;
delete temp;
} else {
if (!temp) {
return;
while (temp) {
temp = temp->next;
~LinkedList() {
while (head) {
delete temp;
};
int main() {
LinkedList linkedList;
linkedList.insert(10);
linkedList.insert(20);
linkedList.insert(30);
linkedList.display();
linkedList.remove(20);
linkedList.display();
return 0;
}
OUTPUT:
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() {
top = nullptr;
// Push operation
newNode->data = value;
newNode->next = top;
top = newNode;
// Pop operation
void pop() {
if (top == nullptr) {
return;
}
Node* temp = top;
top = top->next;
delete temp;
// Peek operation
int peek() {
if (top != nullptr) {
return top->data;
} else {
return -1;
bool isEmpty() {
void display() {
temp = temp->next;
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.display();
stack.pop();
stack.display();
return 0;
}
OUTPUT:
10 pushed to stack
20 pushed to stack
30 pushed to stack
Top element: 30
Popped element: 30
#include <iostream>
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = nullptr;
rear = nullptr;
// Enqueue operation
newNode->data = value;
newNode->next = nullptr;
if (rear == nullptr) {
} else {
rear->next = newNode;
rear = newNode;
// Dequeue operation
void dequeue() {
if (front == nullptr) {
return;
front = front->next;
if (front == nullptr) {
rear = nullptr;
delete temp;
// Peek operation
int peek() {
if (front != nullptr) {
return front->data;
} else {
return -1;
bool isEmpty() {
void display() {
temp = temp->next;
};
int main() {
Queue queue;
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
queue.dequeue();
queue.display();
return 0;
}
OUTPUT:
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
Front element: 10
Dequeued element: 10
#include <iostream>
#include <stack>
#include <string>
return 1;
return 2;
return 0;
switch (op) {
return 0;
stack<char> s;
string postfix;
postfix += ch;
s.push(ch);
postfix += s.top();
s.pop();
// If an operator is encountered
else {
postfix += s.top();
s.pop();
s.push(ch);
while (!s.empty()) {
postfix += s.top();
s.pop();
return postfix;
}
// Function to evaluate postfix expression
stack<int> s;
if (isdigit(ch)) {
// If the character is an operator, pop two elements and apply the operation
else {
s.push(result);
return s.top(); // The result will be the last element in the stack
int main() {
string infixExp;
return 0;
}
OUTPUT:
Evaluation result: 14
PROGRAM:4
#include <iostream>
#include <vector>
class PriorityQueue {
private:
vector<int> heap;
swap(heap[index], heap[parentIndex]);
largest = leftChild;
largest = rightChild;
if (largest != index) {
swap(heap[index], heap[largest]);
public:
cout << "Inserted " << value << " into the priority queue.\n";
int dequeue() {
if (heap.empty()) {
return -1;
heap.pop_back();
cout << "Removed " << topElement << " from the priority queue.\n";
return topElement;
int peek() {
if (heap.empty()) {
return -1;
return heap[0];
return heap.empty();
void display() {
if (heap.empty()) {
return;
};
int main() {
PriorityQueue pq;
pq.enqueue(10);
pq.enqueue(20);
pq.enqueue(5);
pq.enqueue(30);
pq.display();
cout << "Highest priority element (peek): " << pq.peek() << endl;
pq.dequeue();
pq.display();
return 0;
}
OUTPUT:
Priority Queue: 30 20 5 10
Priority Queue: 20 10 5
PROGRAM:5
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
newNode->data = value;
return newNode;
if (root == nullptr) {
return createNode(value);
} else {
return root;
return false;
if (root->data == key) {
return true;
} else {
root = root->left;
return root;
if (root == nullptr) {
return root;
} else {
if (root->left == nullptr) {
return temp;
delete root;
return temp;
root->data = temp->data;
return root;
if (root == nullptr) {
return;
inorder(root->left);
inorder(root->right);
int main() {
inorder(root);
if (search(root, key)) {
} else {
inorder(root);
return 0;
}
OUTPUT:
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
int height;
};
int height(Node* N) {
if (N == nullptr)
return 0;
return N->height;
node->key = key;
node->left = nullptr;
node->right = nullptr;
return node;
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
return x;
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
return y;
int getBalance(Node* N) {
if (N == nullptr)
return 0;
// Insertion function
if (node == nullptr)
return newNode(key);
if (key < node->key)
return node;
return rightRotate(node);
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
}
// Function to find the node with minimum value
current = current->left;
return current;
if (root == nullptr)
return root;
else {
// No child case
if (temp == nullptr) {
temp = root;
root = nullptr;
delete temp;
} else {
root->key = temp->key;
if (root == nullptr)
return root;
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
if (root != nullptr) {
inorder(root->left);
inorder(root->right);
int main() {
inorder(root);
inorder(root);
return 0;
}
OUTPUT:
#include <iostream>
#include <list>
#include <queue>
#include <stack>
class Graph {
public:
// Constructor
Graph(int V);
// BFS traversal
// DFS traversal
};
// Constructor
Graph::Graph(int V) {
this->V = V;
// BFS implementation
visited[i] = false;
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
int v = q.front();
q.pop();
if (!visited[*i]) {
visited[*i] = true;
q.push(*i);
delete[] visited;
visited[v] = true;
if (!visited[*i]) {
DFSUtil(*i, visited);
// DFS implementation
visited[i] = false;
DFSUtil(start, visited);
delete[] visited;
// Main function
int main() {
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.addEdge(3, 4);
g.addEdge(4, 5);
g.DFS(0);
return 0;
}
OUTPUT:
if (arr[i] == key) {
int main() {
int n, key;
cin >> n;
int arr[n];
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
}
OUTPUT:
#include <iostream>
if (arr[mid] == key)
return mid;
right = mid - 1;
else
left = mid + 1;
int main() {
int n, key;
cin >> n;
int arr[n];
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
}
OUTPUT:
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
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 size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
cout << "Sorted array using Bubble Sort: ";
printArray(arr, size);
return 0;
}
OUTPUT:
#include <iostream>
using namespace std;
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
swap(arr[minIdx], arr[i]);
}
}
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 size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
cout << "Sorted array using Selection Sort: ";
printArray(arr, size);
return 0;
}
OUTPUT:
#include <iostream>
int j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
arr[j + 1] = arr[j];
j = j - 1;
int main() {
int n;
cin >> n;
int arr[n];
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT:
Sorted array: 11 12 22 25 64
PROGRAM:9(d)
#include <iostream>
max = arr[i];
return max;
int count[10] = {0}; // Count array to store the count of each digit (0-9)
// Change count[i] so that it contains the actual position of this digit in output[]
arr[i] = output[i];
// Perform counting sort for every digit (1s, 10s, 100s, etc.)
countingSort(arr, n, exp);
int main() {
int n;
cin >> n;
int arr[n];
radixSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT: