Dsa Sam
Dsa Sam
position in an array.
Code:
#include <iostream>
using namespace std;
void insertAtPosition(int arr[], int& size, int position, int element, int
capacity) {
if (size >= capacity) {
cout << "Array is full. Cannot insert element." << endl;
return;
}
arr[position] = element;
size++;
}
arr[size] = element;
size++;
}
int main() {
const int capacity = 10;
int arr[capacity] = {1, 2, 3, 4, 5};
int size = 5;
// Insert at end
insertAtEnd(arr, size, 6, capacity);
cout << "After Inserting 6 at the end: ";
display(arr, size);
return 0;
}
Output:
Original Array: 1 2 3 4 5
After Inserting 6 at the end: 1 2 3 4 5 6
After Inserting 10 at position 2: 1 2 10 3 4 5 6
Task 2: Write a program to delete an element from a given whose value is
given or whose position is given.
Code:
#include <iostream>
using namespace std;
if (pos == -1) {
cout << "Element " << value << " not found in array." << endl;
return;
}
int main() {
int arr[10] = {1, 2, 3, 4, 5};
int size = 5;
// Delete by value
deleteByValue(arr, size, 3);
cout << "After Deleting value 3: ";
display(arr, size);
// Delete by position
deleteByPosition(arr, size, 1);
cout << "After Deleting at position 1: ";
display(arr, size);
return 0;
}
Output:
Original Array: 1 2 3 4 5
After Deleting value 3: 1 2 4 5
After Deleting at position 1: 1 4 5
Task 3: Write a program to find the location of a given element using Linear
Search.
Code:
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4;
return 0;
}
Output:
Code:
#include <iostream>
using namespace std;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4;
return 0;
}
Output:
Code:
#include <iostream>
using namespace std;
class Stack {
private:
int top;
int arr[10];
public:
Stack() : top(-1) {}
Output:
Stack elements: 10 20 30
Popped: 30
Stack elements: 10 20
Task 6: Write a program to convert an infix expression to a postfix expression
using stacks.
Code:
#include <iostream>
#include <stack>
#include <cctype>
using namespace std;
Output:
Infix: a+b*(c^d-e)^(f+g*h)-i
Postfix: abcd^e-fgh*+^*+i-
Task 7,8,9: Program to traverse a Binary Search Tree in Pre-order, In-order,
and Post-order
Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
class BinarySearchTree {
private:
Node* root;
public:
BinarySearchTree() : root(nullptr) {}
void displayPreOrder() {
cout << "Pre-order: ";
preOrder(root);
cout << endl;
}
void displayInOrder() {
cout << "In-order: ";
inOrder(root);
cout << endl;
}
void displayPostOrder() {
cout << "Post-order: ";
postOrder(root);
cout << endl;
}
};
int main() {
BinarySearchTree bst;
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
bst.insert(60);
bst.insert(80);
bst.displayPreOrder();
bst.displayInOrder();
bst.displayPostOrder();
return 0;
}
Output:
Pre-order: 50 30 20 40 70 60 80
In-order: 20 30 40 50 60 70 80
Post-order: 20 40 30 60 80 70 50
Task 10: Program for Graph Traversal Using BFS
Code:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Graph {
private:
int vertices;
vector<vector<int>> adjList;
public:
Graph(int v) : vertices(v), adjList(v) {}
void addEdge(int u, int v) {
adjList[u].push_back(v);
adjList[v].push_back(u); // For undirected graph
}
void bfs(int start) {
vector<bool> visited(vertices, false);
queue<int> q;
visited[start] = true;
q.push(start);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 3);
g.a ddEdge(3, 4);
g.a ddEdge(4, 5);
g.b fs(0);
return 0;
}
Output:
BFS Traversal: 0 1 2 3 4 5
Task 11: Program for Graph Traversal Using DFS
Code:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Graph {
private:
int vertices;
vector<vector<int>> adjList;
public:
Graph(int v) : vertices(v), adjList(v) {}
if (!visited[current]) {
cout << current << " ";
visited[current] = true;
int main() {
Graph g(6);
g.dfs(0);
return 0;
}
Output:
DFS Traversal: 0 2 3 4 5 1