Data Structure is All Here You Need
Data Structure is All Here You Need
class Node{
public:
string name;
int value;
Node* next;
Node(string n,int v){
value=v;
name= n;
next= NULL;
}
};
class list{
public:
Node* head;
Node* tail;
list(){
head= NULL;
tail= NULL;
}
void insert(string name,int score){
Node* data = new Node(name,score);
Node* temp;
temp = head;
if(head == NULL){
head = data;
}
else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next= data;
}
}
l1.addinfront("Eve",88);
l1.addinend("Frank",89);
l1.addinpos(2,"Grace",95);
l1.search("Carol");
l1.display();
l1.deleteatfront();
// l1.deleteatend();
l1.display();
return 0;
}
Dry Run:
-----------------------------------------------------------------------
Stack
---------------------------------------------------------------------
#include<iostream>
using namespace std;
class stack{
public:
int max=10;
int top;
int n;
int *mystack;
stack(int e){
mystack = new int[e];
n=e;
top=-1;
}
bool push(int item){
if(top>=(max-1)){
cout<<"stack overflow..!!!!"<<endl;
return false;
}
else{
mystack[++top]=item;
cout<<"Item pushed :"<<item<<endl;
return true;
}
}
int pop(){
if(top<0){
cout<<"stack underflow...!!"<<endl;
return 0;
}
else{
int item= mystack[top--];
return item;
}
}
bool isempty(){
return(top<0);
}
int peek(){
if(top<0){
cout<<"stack is empty...!!!"<<endl;
return 0;
}
else{
int item = mystack[top];
return item;
}
}
int size(){
return top+1;
}
int isfull(){
if(top== n-1){
cout<<"Stack is full"<<endl;
return 0;
}else{
cout<<"stack has very space"<<endl;
return 0;
}
}
};
int main(){
stack s1(3);
s1.push(10);
s1.push(20);
s1.pop();
s1.peek();
s1.size();
s1.isfull();
s1.isempty();
return 0;
}
Dry Run:
---------------------------------------------------------------------
Queue
--------------------------------------------------------------------
#include <iostream>
using namespace std;
class LinearQueue {
int *arr;
int front, rear, capacity;
public:
LinearQueue(int size) {
capacity = size;
arr = new int[capacity];
front = -1;
rear = -1;
}
~LinearQueue() {
delete[] arr;
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue is empty." << endl;
return;
}
cout << "Patron " << arr[front++] << " removed from the queue." << endl;
}
void displayQueue() {
if (front == -1 || front > rear) {
cout << "Queue is empty." << endl;
return;
}
cout << "Current queue: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
LinearQueue q1(5);
q1.enqueue(101);
q1.enqueue(102);
q1.enqueue(103);
q1.displayQueue();
q1.dequeue();
q1.displayQueue();
q1.enqueue(104);
q1.enqueue(105);
q1.enqueue(106);
q1.displayQueue();
q1.dequeue();
q1.displayQueue();
return 0;
}
Dry Run:
--------------------------------------------------------------------
Bubble Sort
---------------------------------------------------------------------
#include<iostream>
using namespace std;
int main(){
int arr[5]={5,3,1,7,4};
int n =5;
for(int i=0;i<n;i++){
for(int j = i+1; j<n;j++){
if(arr[j]<arr[i]){
int temp = arr[i];
arr[i]= arr[j];
arr[j]= temp;
}
}
}
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
Dry Run:
-------------------------------------------------------------------
Selection Sort
--------------------------------------------------------------------
#include<iostream>
using namespace std;
int main(){
int arr[5]={4,7,1,3,9};
int n=5;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(arr[j]<arr[i]){
int temp = arr[i];
arr[i]= arr[j];
arr[j]= temp;
}
}
}
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
Dry Run:
-----------------------------------------------------------------------
Insertion Sort
---------------------------------------------------------------------
#include<iostream>
using namespace std;
int main(){
int arr[5]={3,9,2,0,1};
int n=5;
for(int i=1;i<n;i++){
int current = arr[i];
int j=i-1;
while(arr[j]>current && j>=0){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
}
Dry Run:
-----------------------------------------------------------------------
Merge Sort
----------------------------------------------------------------------
#include<iostream>
using namespace std;
int i=0,j=0,k=l;
int main(){
int arr[5]={5,1,8,3,0};
mergesort(arr,0,4);
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
Dry Run:
Merge Sort Function:
Merge Function:
----------------------------------------------------------------------
Quick Sort:
---------------------------------------------------------------------
#include<iostream>
using namespace std;
if(l<r){
int pi = partition(arr,l,r);
quicksort(arr,l,pi-1);
quicksort(arr,pi+1,r);
}
}
int main(){
int arr[5]={3,1,9,7,0};
quicksort(arr,0,4);
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
return 0;
}
Dry Run:
-------------------------------------------------------------------
Radix Sort:
--------------------------------------------------------------------
Dry Run:
-------------------------------------------------------------------
Binary Search
--------------------------------------------------------------------
Dry Run:
-----------------------------------------------------------------------
Sample BST
----------------------------------------------------------------------
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int d):data(d){
left = right = NULL;
}
};
int main(){
Node* root = NULL;
insert(root,16);
insert(root,5);
insert(root,3);
insert(root,9);
deleteNode(root,3);
cout << root->data<<" ";
inorder(root);
return 0;
}
Dry Run:
Traversal :
PreOrder: 1st Touch
InderOrder: 2nd Touch
PosOrder: 3rd Touch
#include <iostream>
#include <string>
using namespace std;
class BookNode {
public:
string isbn;
string title;
BookNode* left;
BookNode* right;
int main() {
BookNode* root = NULL;
return 0;
}
-------------------------------------------------------------------------------------------------
class Node {
public:
int data;
Node* left;
Node* right;
Node(int d) : data(d), left(nullptr), right(nullptr) {}
};
if (!node->left)
node->left = insertnode(node->left, value);
else if (!node->right)
node->right = insertnode(node->right, value);
else
insertnode(node->left, value);
return node;
}
// Helper to find the rightmost node in a subtree (used for deletion in binary
tree)
Node* findRightmost(Node* root) {
while (root && root->right) {
root = root->right;
}
return root;
}
if (root->data == key) {
// If the node has two children, replace it with the rightmost node in
its left subtree
if (root->left && root->right) {
Node* rightmost = findRightmost(root->left);
root->data = rightmost->data;
root->left = deleteInBinaryTree(root->left, rightmost->data);
}
// If the node has one child or no children, replace it with that child
or nullptr
else {
Node* child = root->left ? root->left : root->right;
delete root;
return child;
}
} else {
root->left = deleteInBinaryTree(root->left, key);
root->right = deleteInBinaryTree(root->right, key);
}
return root;
}
// BST deletion
Node* deleteInBST(Node* root, int key) {
if (!root) return root;
root->data = successor->data;
root->right = deleteInBST(root->right, successor->data);
}
return root;
}
int main() {
// Construct binary tree
Node* root = nullptr;
root = insertnode(root, 9);
root = insertnode(root, 1);
root = insertnode(root, 3);
root = insertnode(root, 2);
root = insertnode(root, 7);
root = insertnode(root, 4);
root = insertnode(root, 5);
return 0;
}
-------------------------------------------------------------------
AVL Tree:
-----------------------------------------------------------------
Exam Question AVL insertion,Deletion and Search : #include<iostream>
using namespace std;
class AVLtree{
public:
int price;
int height;
AVLtree*left;
AVLtree* right;
AVLtree(int p):price(p),height(1),left(NULL),right(NULL){}
};
AVLtree* rightrotation(AVLtree* y){
AVLtree* x = y->left;
AVLtree* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
AVLtree* leftrotation(AVLtree* x){
AVLtree* y = x->right;
AVLtree* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
int max(int a,int b){
int mx=0;
if(a>b)
mx=a;
else
mx=b;
return mx;
}
int height(AVLtree* root){
if(!root)
return -1;
return root->height;
}
int balancefactor(AVLtree* root){
return height(root->left) - height(root->right);
}
AVLtree* insertion(AVLtree* root, int price){
if(!root){
return new AVLtree(price);
}
if(price < root->price){
root->left = insertion(root->left,price);
}else if(price > root->price){
root->right = insertion(root->right,price);
}else{
cout<<" No Duplication is Allowed."<<endl;
}
root->height = 1 + max(height(root->left),height(root->right));
int BF = balancefactor(root);
if (root->price == budget) {
return root;
}
}
AVLtree* minimum(AVLtree * node){
AVLtree* current = node;
while(!current && current->left ==NULL ){
current = current->left;
}
return current;
}
AVLtree* deletenode(AVLtree* root,int key){
if(root == NULL)
return root;
if(key < root->price){
root->left = deletenode(root->left,key);
}else if(key > root->price){
root->right = deletenode(root->right,key);
}else{
if(root->left == NULL || root->right == NULL){
AVLtree* temp;
if(root->left != NULL){
temp = root->left;
}else{
temp = root->right;
}
if(temp ==NULL){
temp = root;
root =NULL;
}else{
root =temp;
}
}else{
AVLtree* temp =minimum(root->right);
root->price = temp->price;
root->right = deletenode(root->right,temp->price);
}
}
if(root == NULL){
return root;
}
root->height = 1 + max(height(root->left),height(root->right));
int BF = balancefactor(root);
if (nearestProduct != NULL) {
cout << "Suggested Product Price: " << nearestProduct->price << endl;
} else {
cout << "No product found within the budget." << endl;
}
return 0;
}
Dry Run:
----------------------------------------------------------------------
2-3 Tree
--------------------------------------------------------------------
1 Parent 2 Child
2 Parent 3 Child
Dry Run:
------------------------------------------------------------------
B-Tree
---------------------------------------------------------------------
If order is 3 then child will be 3 and keys in one node
will be 3-1 = 2
Dry Run:
---------------------------------------------------------------------
Heap
---------------------------------------------------------------------
Exam Question Heap: #include<iostream>
using namespace std;
class Minheap{
public:
int* arr;
int position;
int size;
Minheap(int s):size(s),position(0){
arr = new int[s];
}
int left(int i){
return i *2 +1;
}
int right(int i){
return i *2 +2;
}
int parent(int i){
return (i -1)/2;
}
void insertion(int score){
if(position == size){
cout<<"Heap is full, can not add more in Heap."<<endl;
}
arr[position] = score;
heapifyup(position);
position++;
}
void heapifyup(int pos){
while( pos != 0 && arr[parent(pos)] > arr[pos]){
swap(arr[parent(pos)],arr[pos]);
pos = parent(pos);
}
}
void heapifydown(int curr){
int min = curr;
int l =left(curr);
int r = right(curr);
if(l < position && arr[l] < arr[min]){
min = l;
}
if(r < position && arr[r] < arr[min]){
min = r;
}
if(min != curr){
swap(arr[min],arr[curr]);
heapifydown(min);
}
}
void deletemin(){
if(position == 0){
cout<<"No more scores"<<endl;
}
cout<<arr[0] <<" ";
arr[0] = arr[position -1];
position--;
heapifydown(0);
}
};
int main(){
int scores[7]={ 45,20,35,10,50,30,25 };
Minheap heap(7);
for(int i=0; i<7 ;i++){
heap.insertion(scores[i]);
}
cout << "Top Three Performers:";
for (int i = 0; i < 3; i++) {
heap.deletemin();
}
cout << endl;
----------------------------------------------------------------------
Heap Sort
----------------------------------------------------------------------
#include <iostream>
using namespace std;
largest = left;
largest = right;
if (largest != i) {
heapify(arr, n, i);
}
// Extract elements from heap one by one
swap(arr[0], arr[i]);
heapify(arr, i, 0);
int main() {
int arr[] = {14, 24, 12, 11, 25, 18, 35}; // Input array
heapSort(arr, n);
return 0;
}
Dry Run:
----------------------------------------------------------------------
Priority Queue
----------------------------------------------------------------------
Priority Queue : #include <iostream>
class Node {
public:
};
// PriorityQueue class
class PriorityQueue {
private:
public:
PriorityQueue() : front(nullptr) {}
~PriorityQueue() {
while (front) {
front = front->next;
delete temp;
Node* newNode = new Node(val, pri); // Create a new node with value and priority
// If the queue is empty or the new node has higher priority than the front
} else {
current = current->next;
newNode->next = current->next;
current->next = newNode;
// Remove the element with the highest priority (front of the queue)
void remove() {
return;
// Traverse the queue and print each node's value and priority
while (current) {
cout << "(" << current->value << ", " << current->priority << ") ";
current = current->next;
}
};
int main() {
PriorityQueue pq;
pq.printQueue();
cout << "Element with highest priority: " << pq.peek() << endl;
pq.remove();
pq.printQueue();
pq.remove();
pq.printQueue();
return 0;
}
--------------------------------------------------------------------
Hashing:
---------------------------------------------------------------------
Separate Chain Hashing:
#include<iostream>
#include<string.h>
class Node{
public:
string data;
Node* next;
Node(string d):data(d){
next = NULL;
}
};
int ascii(string word){
int as = 0;
for(int i=0; i< word.length();i++){
as += word[i];
}
return as;
}
class Hashtable{
public:
Node* hashtable[100];
Hashtable(){
for(int i = 0; i< 100;i++){
hashtable[i] = NULL;
}
}
void display(){
for(int i= 0; i<100 ; i++){
if(hashtable[i] != NULL){
cout<< i << " : ";
Node* temp = hashtable[i];
while(temp != NULL){
cout<<temp->data<<" -> ";
temp = temp->next;
}
cout<<" NULL"<<endl;
}
}
}
void search(string key){
for(int i= 0; i< 100; i++){
Node * temp = hashtable[i];
if(hashtable[i] != NULL){
while(temp != NULL){
if(key == temp->data){
cout<< key <<" Found "<< "at :"<< i <<endl;
return;
}
temp = temp->next;
}
}
}
cout<<key <<" is Not Found"<<endl;
}
};
int main(){
Hashtable table;
table.insert("AB","FASTUNI");
table.insert("AB","DHASUFFA");
table.insert("AC","IBA");
table.insert("AD","GIKI");
table.insert("AE","IQRA");
table.insert("AG","SZABIST");
table.insert("AZ","PIAS");
table.display();
table.search("IBA");
table.search("PIAS");
return 0;
Linear Probing:
#include<iostream>
// Linear Probing
class Hashtable{
public:
int * hashtable;
Hashtable(){
hashtable = new int[10];
for(int i=0;i<100 ;i++){
hashtable[i] = -1;
}
}
Hashtable table;
table.insert(1);
table.insert(3);
table.insert(4);
table.insert(5);
table.insert(7);
table.display();
cout<< endl;
cout<<" Remove 4 :"<<endl;
table.remove(4);
table.display();
cout<< endl;
table.search(5);
table.search(6);
}
Double Hashing:
Exam Question Double Hashing: #include<iostream>
#include<string>
class Node {
public:
string key;
int frequency;
bool isoccupied;
Node() {
frequency = 0;
isoccupied = false;
};
class Hashtable {
public:
Node* hashtable;
int tablesize;
int itemcount;
Hashtable() {
tablesize = 10;
itemcount = 0;
hashtable = new Node[tablesize];
int hash = 0;
return hash;
int hash = 0;
void resize() {
tablesize *= 2;
insert(oldtable[i].key, oldtable[i].frequency);
delete[] oldtable;
oldtable = nullptr;
}
void insert(string key, int freq = 1) {
resize();
if (!hashtable[index].isoccupied) {
hashtable[index].key = key;
hashtable[index].frequency = freq;
hashtable[index].isoccupied = true;
itemcount++;
} else {
hashtable[index].frequency += freq;
void display() {
if(hashtable[i].isoccupied) {
cout << hashtable[i].key << " : " << hashtable[i].frequency << endl;
};
int main() {
Hashtable table;
table.insert("hello");
table.insert("world");
table.insert("hello");
table.insert("example");
table.insert("frequency");
table.insert("world");
table.display();
return 0;
Sorting in Hashing:
Sorting in Hashing: #include<iostream>
#include<string>
class Booknode{
public:
string name;
Booknode* next;
Booknode(string n):name(n),next(nullptr){}
};
class hashbookshelf{
public:
hashbookshelf(){
shelves[i] = NULL;
int ascii = 0;
ascii += key[i];
int i = l-1;
i++;
swap(arr[i],arr[j]);
swap(arr[i+1],arr[r]);
return i+1;
if(l<r){
int pi = partition(arr,l,r);
quicksort(arr,l,pi-1);
quicksort(arr,pi+1,r);
if(head == NULL){
return 0;
int count = 0;
while(head != NULL){
array[count++] = head->name;
head = head->next;
return count;
if(count == 0)
return NULL;
temp = temp->next;
return head;
if(shelves[index] == NULL){
shelves[index] = newNode;
else{
temp = temp->next;
temp->next = newNode;
sortshelf(index);
string books[20];
quicksort(books,0,count-1);
shelves[index] = arraytolinkedlist(books,count);
prev = curr;
curr = curr->next;
if(curr != NULL){
if(prev == NULL){
shelves[index] = curr->next;
}else{
prev->next = curr->next;
delete curr;
void displayallbooks(){
string allbooks[100];
int count = 0;
while(curr != NULL){
allbooks[count++] = curr->name;
curr = curr->next;
quicksort(allbooks,0,count-1);
cout<<allbooks[i]<<endl;
};
int main() {
hashbookshelf bookshelf;
bookshelf.addbook("AB");
bookshelf.addbook("CD");
bookshelf.addbook("EF");
bookshelf.addbook("AC");
bookshelf.addbook("AZ");
bookshelf.displayallbooks();
bookshelf.deletebook("AC");
bookshelf.displayallbooks();
return 0;
Dry Run:
----------------------------------------------------------------------
KMP String Algo & Graph Traversal
---------------------------------------------------------------------
Dry Run:
--------------------------------------------------------------------
Bruit Force Algo
-------------------------------------------------------------------
--------------------------------------------------------------
Dijeckstra Algo and Kruskal Algo and Topological
Sorting
---------------------------------------------------------------
-----------------------------------------------------------------
Use Provided Slides for more prep of graphs
---------------------------------------------------------------------
----------------------------------------------------------------------
Best of Luck
----------------------------------------------------------------------