0% found this document useful (0 votes)
26 views64 pages

Original With Detail Out Put

The document contains various C++ programs demonstrating different data structures and algorithms, including sequential search, binary search, Fibonacci search, stack operations, queue implementations, priority queues, linked lists, and binary tree traversals. Each program includes a main function that tests the respective data structure or algorithm, displaying results such as found elements or the contents of the data structure. The document serves as a comprehensive guide for understanding and implementing these fundamental programming concepts.

Uploaded by

goooglebaba420
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)
26 views64 pages

Original With Detail Out Put

The document contains various C++ programs demonstrating different data structures and algorithms, including sequential search, binary search, Fibonacci search, stack operations, queue implementations, priority queues, linked lists, and binary tree traversals. Each program includes a main function that tests the respective data structure or algorithm, displaying results such as found elements or the contents of the data structure. The document serves as a comprehensive guide for understanding and implementing these fundamental programming concepts.

Uploaded by

goooglebaba420
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/ 64

AIM:

PROGRAM:
#include <iostream>

using namespace std;

int sequentialSearch(int arr[], int size, int key) {

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

if (arr[i] == key)

return i;

return -1;

int main() {

int arr[] = {2, 4, 6, 8, 10};

int key = 6;

int size = sizeof(arr)/sizeof(arr[0]);

int result = sequentialSearch(arr, size, key);

if (result != -1)

cout << "Element found at index " << result << endl;

else
cout << "Element not found" << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

int binarySearch(int arr[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == key)

return mid;

if (arr[mid] < key)

left = mid + 1;

else

right = mid - 1;

return -1;

int main() {

int arr[] = {2, 3, 4, 10, 40};

int key = 10;

int size = sizeof(arr)/sizeof(arr[0]);


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:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <algorithm>

using namespace std;

int fibonacciSearch(int arr[], int size, int key) {

int fib2 = 0;

int fib1 = 1;

int fib = fib2 + fib1;

while (fib < size) {

fib2 = fib1;

fib1 = fib;

fib = fib2 + fib1;

int offset = -1;

while (fib > 1) {

int i = min(offset + fib2, size - 1);

if (arr[i] < key) {


fib = fib1;

fib1 = fib2;

fib2 = fib - fib1;

offset = i;

} else if (arr[i] > key) {

fib = fib2;

fib1 = fib1 - fib2;

fib2 = fib - fib1;

} else {

return i;

if (fib1 && arr[offset + 1] == key)

return offset + 1;

return -1;

int main() {

int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100};

int key = 85;

int size = sizeof(arr) / sizeof(arr[0]);

int result = fibonacciSearch(arr, size, key);

if (result != -1)

cout << "Element found at index " << result << endl;

else

cout << "Element not found" << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <stack>

#include <string>

using namespace std;

int evaluate(string expression) {

stack<int> values; // stack to store integers

stack<char> ops; // stack to store operators

for (int i = 0; i < expression.length(); i++) {

if (isdigit(expression[i])) {

values.push(expression[i] - '0');

} else if (expression[i] == '+' || expression[i] == '-') {

while (!ops.empty()) {

int val2 = values.top();

values.pop();

int val1 = values.top();

values.pop();

char op = ops.top();

ops.pop();

if (op == '+')
values.push(val1 + val2);

else if (op == '-')

values.push(val1 - val2);

ops.push(expression[i]);

while (!ops.empty()) {

int val2 = values.top();

values.pop();

int val1 = values.top();

values.pop();

char op = ops.top();

ops.pop();

if (op == '+')

values.push(val1 + val2);

else if (op == '-')

values.push(val1 - val2);

return values.top();

int main() {

string expression = "2+3-1";

cout << "Result: " << evaluate(expression) << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <stack>

using namespace std;

int main() {

stack<int> s;

s.push(10);

s.push(20);

s.push(30);

cout << "Top element: " << s.top() << endl;

s.pop();

cout << "Top element after pop: " << s.top() << endl;

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#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[s];

void enqueue(int val) {

if ((rear + 1) % size == front) {

cout << "Queue is full\n";

} else {

rear = (rear + 1) % size;

queue[rear] = val;

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

}
void dequeue() {

if (front == -1) {

cout << "Queue is empty\n";

} else {

cout << "Removed: " << queue[front] << endl;

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % size;

void display() {

if (front == -1) {

cout << "Queue is empty\n";

} else {

cout << "Queue elements: ";

for (int i = front; i != rear; i = (i + 1) % size) {

cout << queue[i] << " ";

cout << queue[rear] << endl;

};

int main() {

Queue q(5); // Example size

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);
q.enqueue(40);

q.enqueue(50);

q.display();

q.dequeue();

q.display();

return 0;

OUTPUT:

RESULT
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class CircularQueue {

private:

int front, rear, size;

int* queue;

public:

CircularQueue(int s) {

front = rear = -1;

size = s;

queue = new int[s];

void enqueue(int val) {

if ((rear + 1) % size == front) {

cout << "Queue is full\n";

} else {

rear = (rear + 1) % size;

queue[rear] = val;

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

}
void dequeue() {

if (front == -1) {

cout << "Queue is empty\n";

} else {

cout << "Removed: " << queue[front] << endl;

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % size;

void display() {

if (front == -1) {

cout << "Queue is empty\n";

} else {

cout << "Queue elements: ";

for (int i = front; i != rear; i = (i + 1) % size) {

cout << queue[i] << " ";

cout << queue[rear] << endl;

};

int main() {

CircularQueue q(5); // Example size

q.enqueue(10);

q.enqueue(20);

q.enqueue(30);

q.enqueue(40);
q.enqueue(50);

q.display();

q.dequeue();

q.display();

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <queue>

#include <vector>

using namespace std;

class PriorityQueue {

private:

struct compare {

bool operator()(pair<int, int> const& a, pair<int, int> const& b) {

return a.first < b.first; // Greater priority comes first

};

priority_queue<pair<int, int>, vector<pair<int, int> >, compare> pq; // Notice the space between >> here

public:

void enqueue(int val, int priority) {

pq.push(make_pair(priority, val));

void dequeue() {

if (pq.empty()) {

cout << "Queue is empty\n";

} else {
cout << "Removed: " << pq.top().second << endl;

pq.pop();

void display() {

if (pq.empty()) {

cout << "Queue is empty\n";

} else {

priority_queue<pair<int, int>, vector<pair<int, int> >, compare> temp = pq;

cout << "Queue elements with priority: ";

while (!temp.empty()) {

cout << "(" << temp.top().second << ", " << temp.top().first << ") ";

temp.pop();

cout << endl;

};

int main() {

PriorityQueue pq;

pq.enqueue(10, 2);

pq.enqueue(20, 1);

pq.enqueue(30, 3);

pq.enqueue(40, 2);

pq.display();

pq.dequeue();

pq.display();

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

};

class SinglyLinkedList {

private:

Node* head;

public:

SinglyLinkedList() {

head = nullptr;

void insertAtEnd(int val) {

Node* newNode = new Node();

newNode->data = val;

newNode->next = nullptr;

if (head == nullptr) {

head = newNode;
} else {

Node* temp = head;

while (temp->next != nullptr) {

temp = temp->next;

temp->next = newNode;

void display() {

if (head == nullptr) {

cout << "List is empty\n";

} else {

Node* temp = head;

cout << "Singly Linked List: ";

while (temp != nullptr) {

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

temp = temp->next;

cout << endl;

};

int main() {

SinglyLinkedList list;

list.insertAtEnd(10);

list.insertAtEnd(20);

list.insertAtEnd(30);

list.display();

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node* prev;

};

class DoublyLinkedList {

private:

Node* head;

public:

DoublyLinkedList() {

head = nullptr;

void insertAtEnd(int val) {

Node* newNode = new Node();

newNode->data = val;

newNode->next = nullptr;

newNode->prev = nullptr;
if (head == nullptr) {

head = newNode;

} else {

Node* temp = head;

while (temp->next != nullptr) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void displayForward() {

if (head == nullptr) {

cout << "List is empty\n";

} else {

Node* temp = head;

cout << "Doubly Linked List (Forward): ";

while (temp != nullptr) {

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

temp = temp->next;

cout << endl;

void displayBackward() {

if (head == nullptr) {

cout << "List is empty\n";

} else {

Node* temp = head;

while (temp->next != nullptr) {


temp = temp->next;

cout << "Doubly Linked List (Backward): ";

while (temp != nullptr) {

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

temp = temp->prev;

cout << endl;

};

int main() {

DoublyLinkedList list;

list.insertAtEnd(10);

list.insertAtEnd(20);

list.insertAtEnd(30);

list.displayForward();

list.displayBackward();

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

};

class CircularLinkedList {

private:

Node* head;

public:

CircularLinkedList() {

head = nullptr;

void insertAtEnd(int val) {

Node* newNode = new Node();

newNode->data = val;

if (head == nullptr) {

head = newNode;

newNode->next = head;
} else {

Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

void display() {

if (head == nullptr) {

cout << "List is empty\n";

} else {

Node* temp = head;

cout << "Circular Linked List: ";

do {

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

temp = temp->next;

} while (temp != head);

cout << endl;

};

int main() {

CircularLinkedList list;

list.insertAtEnd(10);

list.insertAtEnd(20);

list.insertAtEnd(30);

list.display();

return 0;
}

OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* left;

Node* right;

Node(int val) {

data = val;

left = right = nullptr;

};

class BinaryTree {

public:

Node* root;

BinaryTree() {

root = nullptr;
}

void inorderTraversal(Node* node) {

if (node == nullptr) return;

inorderTraversal(node->left);

cout << node->data << " ";

inorderTraversal(node->right);

};

int main() {

BinaryTree tree;

tree.root = new Node(1);

tree.root->left = new Node(2);

tree.root->right = new Node(3);

tree.root->left->left = new Node(4);

tree.root->left->right = new Node(5);

cout << "In-order Traversal: ";

tree.inorderTraversal(tree.root);

cout << endl;

return 0;

OUTPUT:
RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* left;

Node* right;

Node(int val) {

data = val;

left = right = nullptr;

};

class BinaryTree {

public:

Node* root;

BinaryTree() {
root = nullptr;

void preorderTraversal(Node* node) {

if (node == nullptr) return;

cout << node->data << " ";

preorderTraversal(node->left);

preorderTraversal(node->right);

};

int main() {

BinaryTree tree;

tree.root = new Node(1);

tree.root->left = new Node(2);

tree.root->right = new Node(3);

tree.root->left->left = new Node(4);

tree.root->left->right = new Node(5);

cout << "Pre-order Traversal: ";

tree.preorderTraversal(tree.root);

cout << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* left;

Node* right;

Node(int val) {

data = val;

left = right = nullptr;

};

class BinaryTree {

public:

Node* root;

BinaryTree() {
root = nullptr;

void preorderTraversal(Node* node) {

if (node == nullptr) return;

cout << node->data << " ";

preorderTraversal(node->left);

preorderTraversal(node->right);

};

int main() {

BinaryTree tree;

tree.root = new Node(1);

tree.root->left = new Node(2);

tree.root->right = new Node(3);

tree.root->left->left = new Node(4);

tree.root->left->right = new Node(5);

cout << "Pre-order Traversal: ";

tree.preorderTraversal(tree.root);

cout << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* left;

Node* right;

Node(int val) {

data = val;

left = right = nullptr;

};

class BinaryTree {

public:

Node* root;

BinaryTree() {
root = nullptr;

void postorderTraversal(Node* node) {

if (node == nullptr) return;

postorderTraversal(node->left);

postorderTraversal(node->right);

cout << node->data << " ";

};

int main() {

BinaryTree tree;

tree.root = new Node(1);

tree.root->left = new Node(2);

tree.root->right = new Node(3);

tree.root->left->left = new Node(4);

tree.root->left->right = new Node(5);

cout << "Post-order Traversal: ";

tree.postorderTraversal(tree.root);

cout << endl;

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <list>

#include <queue>

using namespace std;

class Graph {

int V;

list<int>* adj;

public:

Graph(int V) {

this->V = V;

adj = new list<int>[V];

void addEdge(int v, int w) {

adj[v].push_back(w);

void BFS(int s) {

bool* visited = new bool[V];


for (int i = 0; i < V; i++) visited[i] = false;

queue<int> q;

visited[s] = true;

q.push(s);

while (!q.empty()) {

s = q.front();

cout << s << " ";

q.pop();

for (list<int>::iterator i = adj[s].begin(); i != adj[s].end(); ++i) {

if (!visited[*i]) {

visited[*i] = true;

q.push(*i);

};

int main() {

Graph g(4);

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(2, 0);

g.addEdge(2, 3);

g.addEdge(3, 3);
cout << "Breadth-First Traversal starting from vertex 2: ";

g.BFS(2);

cout << endl;

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <vector>

#include <queue>

#include <climits>

using namespace std;

typedef pair<int, int> iPair;

class Graph {

int V;

vector<vector<iPair> > adj; // Adding the space here

public:

Graph(int V) {

this->V = V;

adj.resize(V);

void addEdge(int u, int v, int w) {

adj[u].push_back(make_pair(v, w));

adj[v].push_back(make_pair(u, w));
}

void dijkstra(int src) {

priority_queue<iPair, vector<iPair>, greater<iPair> > pq; // And here

vector<int> dist(V, INT_MAX);

pq.push(make_pair(0, src));

dist[src] = 0;

while (!pq.empty()) {

int u = pq.top().second;

pq.pop();

for (vector<iPair>::iterator i = adj[u].begin(); i != adj[u].end(); ++i) {

int v = (*i).first;

int weight = (*i).second;

if (dist[v] > dist[u] + weight) {

dist[v] = dist[u] + weight;

pq.push(make_pair(dist[v], v));

cout << "Vertex Distance from Source\n";

for (int i = 0; i < V; ++i)

cout << i << "\t" << dist[i] << endl;

}
};

int main() {

Graph g(9);

g.addEdge(0, 1, 4);

g.addEdge(0, 7, 8);

g.addEdge(1, 2, 8);

g.addEdge(1, 7, 11);

g.addEdge(2, 3, 7);

g.addEdge(2, 8, 2);

g.addEdge(2, 5, 4);

g.addEdge(3, 4, 9);

g.addEdge(3, 5, 14);

g.addEdge(4, 5, 10);

g.addEdge(5, 6, 2);

g.addEdge(6, 7, 1);

g.addEdge(6, 8, 6);

g.addEdge(7, 8, 7);

g.dijkstra(0);

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

int binarySearch(int arr[], int left, int right, int key) {

if (right >= left) {

int mid = left + (right - left) / 2;

if (arr[mid] == key)

return mid;

if (arr[mid] > key)

return binarySearch(arr, left, mid - 1, key);

return binarySearch(arr, mid + 1, right, key);

return -1;

int main() {
int arr[] = {2, 3, 4, 10, 40};

int size = sizeof(arr) / sizeof(arr[0]);

int key = 10;

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:

RESULT:
AIM:

PROGRAM:
#include <iostream>

using namespace std;

// Merge function to merge two halves

void merge(int arr[], int left, int mid, int right) {

int n1 = mid - left + 1;

int n2 = right - mid;

int L[n1], R[n2];

for (int i = 0; i < n1; i++)

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

for (int j = 0; j < n2; j++)

R[j] = arr[mid + 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++;

// Merge Sort function

void mergeSort(int arr[], int left, int right) {

if (left >= right) return;

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int arr_size = sizeof(arr)/sizeof(arr[0]);


cout << "Given array is: ";

for (int i = 0; i < arr_size; i++)

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

cout << endl;

mergeSort(arr, 0, arr_size - 1);

cout << "Sorted array is: ";

for (int i = 0; i < arr_size; i++)

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

cout << endl;

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#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 - 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 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 n = sizeof(arr) / sizeof(arr[0]);

cout << "Given array is: ";

printArray(arr, n);

quickSort(arr, 0, n - 1);

cout << "Sorted array is: ";

printArray(arr, n);

return 0;

}
OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <vector>

#include <algorithm>

struct Item {

int value;

int weight;

};

bool compare(const Item& a, const Item& b) {

double r1 = static_cast<double>(a.value) / a.weight;

double r2 = static_cast<double>(b.value) / b.weight;

return r1 > r2;

int knapsack(int capacity, const std::vector<Item>& items) {

std::vector<Item> sortedItems = items;

std::sort(sortedItems.begin(), sortedItems.end(), compare);

int totalValue = 0;
for (size_t i = 0; i < sortedItems.size(); ++i) {

if (capacity >= sortedItems[i].weight) {

capacity -= sortedItems[i].weight;

totalValue += sortedItems[i].value;

} else {

totalValue += sortedItems[i].value * (static_cast<double>(capacity) / sortedItems[i].weight);

break;

return totalValue;

int main() {

std::vector<Item> items;

Item item1 = {60, 10};

Item item2 = {100, 20};

Item item3 = {120, 30};

items.push_back(item1);

items.push_back(item2);

items.push_back(item3);

int capacity = 50;

int maxValue = knapsack(capacity, items);

std::cout << "Maximum value in knapsack: " << maxValue << std::endl;

return 0;

}
OUTPUT:

RESUTL:
AIM:

PROGRAM:
#include <iostream>

#include <vector>

#include <cmath>

#include <algorithm>

const int INF = 1e9;

int n;

std::vector<std::vector<int> > dist;

std::vector<std::vector<int> > dp;

int tsp(int mask, int pos) {

if (mask == (1 << n) - 1)

return dist[pos][0];

if (dp[mask][pos] != -1)

return dp[mask][pos];

int ans = INF;

for (int city = 0; city < n; city++) {

if ((mask & (1 << city)) == 0) {

int newAns = dist[pos][city] + tsp(mask | (1 << city), city);

ans = std::min(ans, newAns);

}
return dp[mask][pos] = ans;

int main() {

std::cout << "Enter number of cities: ";

std::cin >> n;

dist = std::vector<std::vector<int> >(n, std::vector<int>(n));

dp = std::vector<std::vector<int> >((1 << n), std::vector<int>(n, -1));

std::cout << "Enter distances:\n";

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

std::cin >> dist[i][j];

std::cout << "Minimum cost: " << tsp(1, 0) << std::endl;

return 0;

OUTPUT:

RESULT:
AIM:

PROGRAM:
#include <iostream>

#include <vector>

#define N 8

void printSolution(const std::vector<std::vector<int> >& board) {

for (int i = 0; i < N; ++i) {

for (int j = 0; j < N; ++j) {

std::cout << (board[i][j] ? "Q " : ". ");

std::cout << std::endl;

bool isSafe(const std::vector<std::vector<int> >& board, int row, int col) {

for (int i = 0; i < col; ++i)

if (board[row][i])

return false;

for (int i = row, j = col; i >= 0 && j >= 0; --i, --j)


if (board[i][j])

return false;

for (int i = row, j = col; j >= 0 && i < N; ++i, --j)

if (board[i][j])

return false;

return true;

bool solveNQueensUtil(std::vector<std::vector<int> >& board, int col) {

if (col >= N)

return true;

for (int i = 0; i < N; ++i) {

if (isSafe(board, i, col)) {

board[i][col] = 1;

if (solveNQueensUtil(board, col + 1))

return true;

board[i][col] = 0;

return false;

bool solveNQueens() {

std::vector<std::vector<int> > board(N, std::vector<int>(N, 0));

if (solveNQueensUtil(board, 0)) {

printSolution(board);
return true;

} else {

std::cout << "No solution exists" << std::endl;

return false;

int main() {

solveNQueens();

return 0;

OUTPUT:

RESULT:

You might also like