Data Structure With C++
Data Structure With C++
#include <stdio.h>
int main() {
int arr[7], n = 7, i, j;
printf("Enter 7 numbers:\n");
scanf("%d", &arr[i]);
// Swap elements
arr[j + 1] = temp;
printf("\n");
return 0;
}
ii. Insertion Sort
#include<stdio.h>
int main() {
printf("Enter 7 numbers:\n");
key = arr[i];
arr[j + 1] = arr[j];
arr[j + 1] = key;
return 0;
}
2. Implementation of Searching Techniques
i. Linear Search
#include <stdio.h>
int main() {
printf("Enter 7 numbers:\n");
scanf("%d", &arr[i]);
scanf("%d", &key);
if (arr[i] == key) {
found = 1;
if (!found) {
return 0;
}
ii. Binary Search
#include <stdio.h>
int main() {
scanf("%d", &arr[i]);
scanf("%d", &key);
// Binary Search
if (arr[mid] == key) {
else
// If not found
printf("Element %d
not found in the
array.\n", key);
return 0;
}
3. Implement program for Modulo Division as hashing method.
#include <stdio.h>
#define TABLE_SIZE 5
if (table[index] == -1)
table[index] = key;
else
printf("\nHash Table:\n");
if (table[i] != -1)
else
int main() {
table[i] = -1;
insert(table, keys[i]);
display(table);
return 0;
}
4. Implementation of Singly Linked List.
#include <iostream>
// Node structure
struct Node {
};
class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() {
head = nullptr;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
}
}
newNode->data = value;
newNode->next = head;
head = newNode;
if (head == nullptr) {
return;
if (head->data == value) {
head = head->next;
delete temp;
return;
temp = temp->next;
if (temp->next == nullptr) {
} else {
temp->next = temp->next->next;
delete nodeToDelete;
void display() {
if (head == nullptr) {
return;
temp = temp->next;
~SinglyLinkedList() {
Node* nextNode;
nextNode = current->next;
delete current;
current = nextNode;
};
int main() {
SinglyLinkedList list;
list.insertEnd(10);
list.insertEnd(20);
list.insertEnd(30);
list.insertEnd(40);
list.display();
list.insertBegin(5);
list.display();
// Delete an element
list.deleteNode(20);
list.display();
list.deleteNode(100);
return 0;
}
5. Implementation of Stack operations using array or linked list.
Array list
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
if (top == MAX - 1)
printf("Stack Overflow!\n");
else
stack[++top] = value;
void pop() {
if (top == -1)
printf("Stack Underflow!\n");
else
void display() {
if (top == -1)
printf("Stack is empty.\n");
else {
printf("Stack: ");
printf("\n");
int main() {
do {
scanf("%d", &choice);
if (choice == 1) {
scanf("%d", &value);
push(value);
} else if (choice == 2)
pop();
else if (choice == 3)
display();
printf("Exiting...\n");
return 0;
}
Linked List
#include <iostream>
// Node structure
struct Node {
int data;
Node* next;
};
class StackLinkedList {
private:
Node* top;
public:
StackLinkedList() {
top = nullptr;
// Push operation
newNode->data = value;
newNode->next = top;
top = newNode;
// Pop operation
int pop() {
if (top == nullptr) {
return -1;
top = top->next;
return poppedValue;
// Peek operation
int peek() {
if (top == nullptr) {
return -1;
return top->data;
bool isEmpty() {
void display() {
if (top == nullptr) {
return;
temp = temp->next;
~StackLinkedList() {
top = top->next;
delete temp;
};
int main() {
StackLinkedList stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.display();
stack.display();
stack.push(50);
stack.display();
return 0;
}
6. Implementation of Stack Applications.
a. Postfix Evaluation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
typedef struct {
int data[MAX];
int top;
} Stack;
if (s->top == MAX - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
s->data[++(s->top)] = value;
int pop(Stack* s) {
if (s->top == -1) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
return s->data[(s->top)--];
Stack s;
if (isdigit(expr[i])) {
push(&s, expr[i] - '0');
} else {
op2 = pop(&s);
op1 = pop(&s);
switch (expr[i]) {
case '/':
if (op2 == 0) {
exit(EXIT_FAILURE);
break;
default:
exit(EXIT_FAILURE);
i++;
return pop(&s);
int main() {
char expr[MAX];
scanf("%s", expr);
printf("Result: %d\n",
evaluatePostfix(expr));
return 0;
}
7. Implementation of Linear Queue operations using array or linked list
Arraylist
#include <iostream>
class Queue {
private:
int* arr;
int front;
int rear;
int capacity;
public:
Queue(int size) {
capacity = size;
front = -1;
rear = -1;
~Queue() {
delete[] arr;
bool isEmpty() {
bool isFull() {
if (isFull()) {
cout << "Queue is full!" << endl;
return;
if (front == -1) {
arr[++rear] = value;
void dequeue() {
if (isEmpty()) {
return;
if (front == rear) {
} else {
front++;
int getFront() {
if (isEmpty()) {
return -1;
return arr[front];
int size() {
if (isEmpty()) {
return 0;
}
return rear - front + 1;
};
int main() {
Queue q(5);
q.enqueue(1);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
cout << "Front element after dequeue: " << q.getFront() << endl;
return 0;
}
LinkedList
#include <iostream>
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
bool isEmpty() {
newNode->data = value;
newNode->next = nullptr;
if (isEmpty()) {
} else {
rear->next = newNode;
rear = newNode;
}
// Remove an element from the front of the queue
void dequeue() {
if (isEmpty()) {
return;
front = front->next;
delete temp;
if (front == nullptr) {
int getFront() {
if (isEmpty()) {
return -1;
return front->data;
int size() {
int count = 0;
count++;
temp = temp->next;
return count;
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
cout << "Front element after dequeue: " << q.getFront() << endl;
return 0;
}
8. Implementation of BST and its traversal techniques
a. Inorder
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
};
if (root == nullptr) {
} else {
return root;
cout << root->data << " "; // Visit root (current node)
int main() {
inorderTraversal(root); // Output: 20 30 40 50 60 70 80
return 0;
}
b. Preorder
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
};
if (root == nullptr) {
} else {
return root;
if (root == nullptr) {
return; // Base case: if the node is null, return
cout << root->data << " "; // Visit root (current node)
int main() {
preorderTraversal(root); // Output: 50 30 20 40 70 60 80
return 0;
}
c. PostOrder
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
};
if (root == nullptr) {
} else {
return root;
if (root == nullptr) {
return; // Base case: if the node is null, return
cout << root->data << " "; // Visit root (current node)
int main() {
postorderTraversal(root); // Output: 20 40 30 60 80 70 50
return 0;
}
9. Implementation of Graph Traversal Technique.
a. Depth First Search
#include <iostream>
#include <vector>
class Graph {
public:
// Constructor
Graph(int vertices) {
this->vertices = vertices;
adjList.resize(vertices);
// Function to perform Depth First Search (DFS) starting from a given vertex
cout << vertex << " "; // Process the current node (print it)
// Function to call DFS for all unvisited nodes (for disconnected graphs)
void dfsTraversal() {
vector<bool> visited(vertices, false); // Initialize visited array with false
if (!visited[i]) {
};
int main() {
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
cout << "Depth First Search (DFS) Traversal of the graph: ";
g.dfsTraversal();
return 0;
}
b. Breadth First Search
#include <iostream>
#include <vector>
#include <queue>
class Graph {
public:
// Constructor
Graph(int vertices) {
this->vertices = vertices;
adjList.resize(vertices);
// Function to perform Breadth First Search (BFS) starting from a given vertex
visited[startVertex] = true;
q.push(startVertex);
while (!q.empty()) {
cout << vertex << " "; // Process the current node (print it)
// Function to call BFS for all unvisited nodes (for disconnected graphs)
void bfsTraversal() {
if (!visited[i]) {
};
int main() {
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
cout << "Breadth First Search (BFS) Traversal of the graph: ";
g.bfsTraversal();
return 0;
}
10. Create a Minimum Spanning Tree using Kruskal’s Algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
struct Edge {
int u, v, weight;
this->u = u;
this->v = v;
this->weight = weight;
};
class DisjointSet {
public:
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
int find(int u) {
if (parent[u] != u) {
return parent[u];
}
if (root_u != root_v) {
parent[root_v] = root_u;
parent[root_u] = root_v;
} else {
parent[root_v] = root_u;
rank[root_u]++;
};
});
// If including this edge does not form a cycle, add it to the MST
if (dsu.find(e.u) != dsu.find(e.v)) {
dsu.unionSets(e.u, e.v);
mst.push_back(e);
}
}
int totalWeight = 0;
cout << e.u << " -- " << e.v << " == " << e.weight << "\n";
totalWeight += e.weight;
cout << "Total weight of MST: " << totalWeight << endl;
int main() {
vector<Edge> edges;
edges.push_back(Edge(0, 1, 4));
edges.push_back(Edge(0, 2, 3));
edges.push_back(Edge(1, 2, 1));
edges.push_back(Edge(1, 3, 2));
edges.push_back(Edge(2, 3, 4));
edges.push_back(Edge(3, 4, 2));
edges.push_back(Edge(4, 5, 6));
edges.push_back(Edge(2, 5, 5));
kruskal(n, edges);
return 0;