DS Code
DS Code
#include <stdlib.h>
#include <iostream>
// Create a node
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
if (prev_node == NULL) {
return;
}
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
last->next = new_node;
return;
// Delete a node
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
// Search a node
current = current->next;
return false;
int temp;
if (head_ref == NULL) {
return;
} else {
index = current->next;
temp = current->data;
current->data = index->data;
index->data = temp;
index = index->next;
current = current->next;
node = node->next;
// Driver program
int main() {
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
printList(head);
deleteNode(&head, 3);
printList(head);
int item_to_find = 3;
if (searchNode(&head, item_to_find)) {
} else {
sortLinkedList(&head);
printList(head);
}
KRUSAL ALGO
#include <bits/stdc++.h>
class DSU {
int* parent;
int* rank;
public:
DSU(int n)
parent[i] = -1;
rank[i] = 1;
// Find function
int find(int i)
if (parent[i] == -1)
return i;
// Union function
int s1 = find(x);
int s2 = find(y);
if (s1 != s2) {
parent[s1] = s2;
parent[s2] = s1;
else {
parent[s2] = s1;
rank[s1] += 1;
};
class Graph {
int V;
public:
Graph(int V) { this->V = V; }
edgelist.push_back({ w, x, y });
void kruskals_mst()
sort(edgelist.begin(), edgelist.end());
DSU s(V);
int ans = 0;
"constructed MST"
<< endl;
int w = edge[0];
int x = edge[1];
int y = edge[2];
if (s.find(x) != s.find(y)) {
s.unite(x, y);
ans += w;
cout << x << " -- " << y << " == " << w
<< endl;
};
// Driver code
int main()
Graph g(4);
g.addEdge(0, 1, 10);
g.addEdge(1, 3, 15);
g.addEdge(2, 3, 4);
g.addEdge(2, 0, 6);
g.addEdge(0, 3, 5);
// Function call
g.kruskals_mst();
return 0;
PRIM’S ALGO
#include <bits/stdc++.h>
#define V 5
return min_index;
cout << parent[i] << " - " << i << " \t"
// matrix representation
int parent[V];
// Key values used to pick minimum weight edge in cut
int key[V];
bool mstSet[V];
// vertex.
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
printMST(parent, graph);
// Driver's code
int main()
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
#include <iostream>
#include <queue>
#include <vector>
vector<bool>& visited)
queue<int> q;
visited[startNode] = true;
q.push(startNode);
while (!q.empty()) {
q.pop();
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
adjList[u].push_back(v);
int main()
int vertices = 5;
addEdge(adjList, 0, 1);
addEdge(adjList, 0, 2);
addEdge(adjList, 1, 3);
addEdge(adjList, 1, 4);
addEdge(adjList, 2, 4);
// Mark all the vertices as not visited
"0: ";
bfs(adjList, 0, visited);
return 0;
DFS
#include <bits/stdc++.h>
class Graph {
public:
// reachable from v
void DFS(int v);
};
adj[v].push_back(w);
void Graph::DFS(int v)
// print it
visited[v] = true;
// to this vertex
list<int>::iterator i;
if (!visited[*i])
DFS(*i);
// Driver code
int main()
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
// Function call
g.DFS(2);
return 0;
// improved by Vishnudev C
HEAP
#include <iostream>
#include <vector>
*b = *a;
*a = temp;
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
largest = l;
largest = r;
if (largest != i)
swap(&hT[i], &hT[largest]);
heapify(hT, largest);
if (size == 0)
hT.push_back(newNum);
else
hT.push_back(newNum);
heapify(hT, i);
}
void deleteNode(vector<int> &hT, int num)
int i;
if (num == hT[i])
break;
hT.pop_back();
heapify(hT, i);
int main()
vector<int> heapTree;
insert(heapTree, 3);
insert(heapTree, 4);
insert(heapTree, 9);
insert(heapTree, 5);
insert(heapTree, 2);
printArray(heapTree);
deleteNode(heapTree, 4);
printArray(heapTree);