CONTENTS
S.NO DATE PAGE STAFF
TITLE OF THE PROGRAM NO SIGNATURE
LIST – ARRAY IMPLEMENTATION
1.
LIST – LINKED LIST IMPLEMENTATION
2. QUEUE ADT USING SINGLY LINKED
LIST:
3. INFIX TO POSTFIX CONVERSION
4. HEAP USING PRIORITY QUEUE
5. BINARY SEARCH TREE
6. AVL TREE
7. GRAPH TRAVERSAL
8. LINEAR & BINARY SEARCH
9. SORTING
Program:1
#include <iostream>
using namespace std;
class ArrayList {
private:
int* array;
int capacity;
int current;
public:
// Constructor to initialize the array list
ArrayList() {
array = new int[1];
capacity = 1;
current = 0;
// Function to add an element at the end
void add(int data) {
if (current == capacity) {
int* temp = new int[2 * capacity];
for (int i = 0; i < capacity; i++) {
temp[i] = array[i];
delete[] array;
capacity *= 2;
array = temp;
array[current] = data;
current++;
// Function to add an element at any index
void add(int data, int index) {
if (index == current) {
add(data);
} else {
array[index] = data;
// Function to get an element at any index
int get(int index) {
if (index < current) {
return array[index];
return -1; // Return -1 if index is out of bounds
// Function to remove the last element
void pop() {
current--;
// Function to get the size of the list
int size() {
return current;
// Function to get the capacity of the list
int getCapacity() {
return capacity;
// Function to print the array list
void print() {
for (int i = 0; i < current; i++) {
cout << array[i] << " ";
cout << endl;
}
// Destructor to clean up memory
~ArrayList() {
delete[] array;
};
int main() {
ArrayList list;
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
cout << "Elements of the list: ";
list.print();
cout << "Size of the list: " << list.size() << endl;
cout << "Capacity of the list: " << list.getCapacity() << endl;
list.pop();
cout << "Elements after popping: ";
list.print();
cout << "Size after popping: " << list.size() << endl;
cout << "Capacity after popping: " << list.getCapacity() << endl;
return 0;
}
Output:
Elements of the list: 1 2 3 4 5
Size of the list: 5
Capacity of the list: 8
Elements after popping: 1 2 3 4
Size after popping: 4
Capacity after popping: 8
Program:2
#include<iostream>
using namespace std;
class Node
public:
int data;
Node *next;
};
void enqueue (Node ** head, int data)
Node *new_node = new Node ();
// assign data value
new_node->data = data;
// change the next node of this new_node
// to current head of Linked List
new_node->next = *head;
//changing the new head to this newly entered node
*head = new_node;
void dequeue (Node ** head)
Node *temp = *head;
// if there are no nodes in Linked List can't delete
if (*head == NULL)
cout << ("Linked List Empty, nothing to delete");
return;
}
// move head to next node
*head = (*head)->next;
//cout<< ("Deleted: %d\n", temp->data);
delete (temp);
void display (Node * node)
//as linked list will end when Node is Null
while (node != NULL)
cout << node->data << " ";
node = node->next;
cout << endl;
int main ()
Node *head = NULL;
enqueue (&head, 10);
enqueue (&head, 11);
enqueue (&head, 12);
enqueue (&head, 13);
enqueue (&head, 14);
enqueue (&head, 15);
enqueue (&head, 16);
enqueue (&head, 17);
enqueue (&head, 18);
cout << "Queue before deletion: ";
display (head);
dequeue (&head);
cout << endl << "Queue after deletion: ";
display (head);
return 0;
}
Output:
Elements of the queue: 1 2 3 4 5
Front element of the queue: 1
Elements of the queue after dequeue: 2 3 4 5
Front element of the queue after dequeue: 2
Program:3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to return precedence of operators
int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
// Function to return associativity of operators
char associativity(char c) {
if (c == '^')
return 'R';
return 'L'; // Default to left-associative
}
// The main function to convert infix expression to postfix expression
void infixToPostfix(char s[]) {
char result[1000];
int resultIndex = 0;
int len = strlen(s);
char stack[1000];
int stackIndex = -1;
for (int i = 0; i < len; i++) {
char c = s[i];
// If the scanned character is an operand, add it to the output string.
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result[resultIndex++] = c;
}
// If the scanned character is an ‘(‘, push it to the stack.
else if (c == '(') {
stack[++stackIndex] = c;
}
// If the scanned character is an ‘)’, pop and add to the output string from the stack
// until an ‘(‘ is encountered.
else if (c == ')') {
while (stackIndex >= 0 && stack[stackIndex] != '(') {
result[resultIndex++] = stack[stackIndex--];
}
stackIndex--; // Pop '('
}
// If an operator is scanned
else {
while (stackIndex >= 0 && (prec(s[i]) < prec(stack[stackIndex]) ||
prec(s[i]) == prec(stack[stackIndex]) &&
associativity(s[i]) == 'L')) {
result[resultIndex++] = stack[stackIndex--];
}
stack[++stackIndex] = c;
}
}
// Pop all the remaining elements from the stack
while (stackIndex >= 0) {
result[resultIndex++] = stack[stackIndex--];
}
result[resultIndex] = '\0';
printf("%s\n", result);
}
// Driver code
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
// Function call
infixToPostfix(exp);
return 0;
}
Output:
abcd^e-fgh*+^*+i-
Program:4
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
void printPriorityQueue(priority_queue<int> pq) {
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
cout << endl;
int main() {
// Create a max-heap priority queue
priority_queue<int> maxHeap;
// Insert elements into the max-heap
maxHeap.push(10);
maxHeap.push(30);
maxHeap.push(20);
maxHeap.push(5);
maxHeap.push(1);
cout << "Elements in the max-heap (priority queue): ";
printPriorityQueue(maxHeap);
// Access the top element
cout << "Top element: " << maxHeap.top() << endl;
// Remove the top element
maxHeap.pop();
cout << "Elements in the max-heap after removing the top element: ";
printPriorityQueue(maxHeap);
return 0;}
output:
Elements in the max-heap (priority queue): 30 20 10 5 1
Top element: 30
Elements in the max-heap after removing the top element: 20 10 5 1
Program:5
#include <iostream>
using namespace std;
struct Node {
int key;
Node* left;
Node* right;
Node(int item) {
key = item;
left = right = NULL;
}
};
// function to search a key in a BST
Node* search(Node* root, int key) {
// Base Cases: root is null or key
// is present at root
if (root == NULL || root->key == key)
return root;
// Key is greater than root's key
if (root->key < key)
return search(root->right, key);
// Key is smaller than root's key
return search(root->left, key);
}
// Driver Code
int main() {
// Creating a hard coded tree for keeping
// the length of the code small. We need
// to make sure that BST properties are
// maintained if we try some other cases.
Node* root = new Node(50);
root->left = new Node(30);
root->right = new Node(70);
root->left->left = new Node(20);
root->left->right = new Node(40);
root->right->left = new Node(60);
root->right->right = new Node(80);
(search(root, 19) != NULL)? cout << "Found\n":
cout << "Not Found\n";
(search(root, 80) != NULL)? cout << "Found\n":
cout << "Not Found\n";
return 0;
}
Output:
Not Found
Found
Program:6
// C++ program to insert a node in AVL tree
#include<bits/stdc++.h>
using namespace std;
// An AVL tree node
class Node
public:
int key;
Node *left;
Node *right;
int height;
};
// A utility function to get the
// height of the tree
int height(Node *N)
if (N == NULL)
return 0;
return N->height;
// A utility function to get maximum
// of two integers
int max(int a, int b)
return (a > b)? a : b;
/* Helper function that allocates a
new node with the given key and
NULL left and right pointers. */
Node* newNode(int key)
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially
// added at leaf
return(node);
// A utility function to right
// rotate subtree rooted with y
// See the diagram given above.
Node *rightRotate(Node *y)
Node *x = y->left;
Node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
// Return new root
return x;
}
// A utility function to left
// rotate subtree rooted with x
// See the diagram given above.
Node *leftRotate(Node *x)
Node *y = x->right;
Node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
// Return new root
return y;
// Get Balance factor of node N
int getBalance(Node *N)
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
// Recursive function to insert a key
// in the subtree rooted with node and
// returns the new root of the subtree.
Node* insert(Node* node, int key)
/* 1. Perform the normal BST insertion */
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;
/* 2. Update height of this ancestor node */
node->height = 1 + max(height(node->left),
height(node->right));
/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
int balance = getBalance(node);
// If this node becomes unbalanced, then
// there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key)
node->left = leftRotate(node->left);
return rightRotate(node);
// Right Left Case
if (balance < -1 && key < node->right->key)
node->right = rightRotate(node->right);
return leftRotate(node);
/* return the (unchanged) node pointer */
return node;
// A utility function to print preorder
// traversal of the tree.
// The function also prints height
// of every node
void preOrder(Node *root)
if(root != NULL)
{
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
// Driver Code
int main()
Node *root = NULL;
/* Constructing tree given in
the above figure */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
cout << "Preorder traversal of the "
"constructed AVL tree is \n";
preOrder(root);
return 0;
}
Output:
Preorder traversal of the constructed AVL tree is
30 20 10 25 40 50
Program:7
#include<iostream>
#include <list>
using namespace std;
// This class represents an undirected graph using adjacency list representation
class Graph
int numberOfVertices; // No. of vertices
// Pointer to an array containing adjacency lists
list<int> *adjacencyList;
public:
Graph(int numberOfVertices); // Constructor
void addEdge(int from, int to);
void bfsTraversal(int startingVertex);
};
Graph::Graph(int numberOfVertices)
this->numberOfVertices = numberOfVertices;
adjacencyList = new list<int>[numberOfVertices];
void Graph::addEdge(int v, int w)
adjacencyList[v].push_back(w);
adjacencyList[w].push_back(v);
void Graph::bfsTraversal(int vertex)
// Mark all the vertices as not visited
bool *visited = new bool[numberOfVertices];
for(int i = 0; i < numberOfVertices; i++)
visited[i] = false;
// Create a queue for bfsTraversal
list<int> queue;
visited[vertex] = true;
queue.push_back(vertex);
list<int>::iterator i;
while(!queue.empty())
// Dequeue a vertex from queue and print it
vertex = queue.front();
cout << vertex << " ";
queue.pop_front();
// If a adjacent vertex has not been visited, then mark it visited and enqueue it
for (i = adjacencyList[vertex].begin(); i != adjacencyList[vertex].end(); ++i)
if (!visited[*i])
visited[*i] = true;
queue.push_back(*i);
int main()
Graph graph(7);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 1);
graph.addEdge(2, 5);
graph.addEdge(3, 6);
cout << "Breadth First Traversal from 1 is : \n";
graph.bfsTraversal(1);
return 0;
}
Output:
Breadth First Traversal from 1 is :
1034265
Program:8
1.Linear search
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index of the key
return -1; // Key not found
int main() {
int arr[] = {2, 4, 0, 1, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 1;
int result = linearSearch(arr, size, key);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found" << endl;
return 0;
2.Binary search
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if key is present at mid
if (arr[mid] == key) {
return mid;
// If key is greater, ignore left half
if (arr[mid] < key) {
left = mid + 1;
} else { // If key is smaller, ignore right half
right = mid - 1;
return -1; // Key not found
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 5;
int result = binarySearch(arr, 0, size - 1, key);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found" << endl;
return 0;
}
Output:1
Element found at index 3
Output:2
Element found at index 4
Program:9
1.Bubble sort
#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;
}
2.Selection sort:
#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;
}
3.Quick sort:
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
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[] = {10, 7, 8, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, size - 1);
cout << "Sorted array using Quick Sort: ";
printArray(arr, size);
return 0;
}
Output:1
Sorted array using Bubble Sort: 11 12 22 25 34 64 90
Output:2
Sorted array using Selection Sort: 11 12 22 25 64
Output:3
Sorted array using Quick Sort: 1 5 7 8 9 10