0% found this document useful (0 votes)
15 views13 pages

Dsa Sam

Uploaded by

ron123421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Dsa Sam

Uploaded by

ron123421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Task 1: Write a program to insert a new element at end as well as at a given

position in an array.

Code:

#include <iostream>
using namespace std;

void display(int arr[], int size) {


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

void insertAtPosition(int arr[], int& size, int position, int element, int
capacity) {
if (size >= capacity) {
cout << "Array is full. Cannot insert element." << endl;
return;
}

if (position < 0 || position > size) {


cout << "Invalid position." << endl;
return;
}

for (int i = size; i > position; i--) {


arr[i] = arr[i - 1];
}

arr[position] = element;
size++;
}

void insertAtEnd(int arr[], int& size, int element, int capacity) {


if (size >= capacity) {
cout << "Array is full. Cannot insert element." << endl;
return;
}

arr[size] = element;
size++;
}

int main() {
const int capacity = 10;
int arr[capacity] = {1, 2, 3, 4, 5};
int size = 5;

cout << "Original Array: ";


display(arr, size);

// Insert at end
insertAtEnd(arr, size, 6, capacity);
cout << "After Inserting 6 at the end: ";
display(arr, size);

// Insert at a given position


insertAtPosition(arr, size, 2, 10, capacity);
cout << "After Inserting 10 at position 2: ";
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;

void display(int arr[], int size) {


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

void deleteByValue(int arr[], int& size, int value) {


int pos = -1;
for (int i = 0; i < size; i++) {
if (arr[i] == value) {
pos = i;
break;
}
}

if (pos == -1) {
cout << "Element " << value << " not found in array." << endl;
return;
}

for (int i = pos; i < size - 1; i++) {


arr[i] = arr[i + 1];
}
size--;
}

void deleteByPosition(int arr[], int& size, int position) {


if (position < 0 || position >= size) {
cout << "Invalid position." << endl;
return;
}

for (int i = position; i < size - 1; i++) {


arr[i] = arr[i + 1];
}
size--;
}

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

cout << "Original Array: ";


display(arr, size);

// 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 linearSearch(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[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4;

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


if (result != -1) {
cout << "Element " << key << " found at index " << result << endl;
} else {
cout << "Element " << key << " not found." << endl;
}

return 0;
}

Output:

Element 4 found at index 3


Task 4: Write a program to find the location of a given element using Binary
Search.

Code:

#include <iostream>
using namespace std;

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


int low = 0, high = size - 1;

while (low <= high) {


int mid = (low + high) / 2;

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;

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


if (result != -1) {
cout << "Element " << key << " found at index " << result << endl;
} else {
cout << "Element " << key << " not found." << endl;
}

return 0;
}

Output:

Element 4 found at index 3


Task 5: Write a program to implement push and pop operations on a stack
using linear array.

Code:

#include <iostream>
using namespace std;

class Stack {
private:
int top;
int arr[10];

public:
Stack() : top(-1) {}

void push(int value) {


if (top >= 9) {
cout << "Stack overflow." << endl;
return;
}
arr[++top] = value;
}
void pop() {
if (top < 0) {
cout << "Stack underflow." << endl;
return;
}
cout << "Popped: " << arr[top--] << endl;
}
void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();
s.pop();
s.display();
return 0;
}

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;

int precedence(char op) {


if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
string infixToPostfix(string infix) {
stack<char> s;
string postfix = "";

for (char c : infix) {


if (isalnum(c)) {
postfix += c;
} else if (c == '(') {
s.push(c);
} else if (c == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
s.pop();
} else if (isOperator(c)) {
while (!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top();
s.pop();
}
s.push(c);
}
}
while (!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
string infix = "a+b*(c^d-e)^(f+g*h)-i";
cout << "Infix: " << infix << endl;
cout << "Postfix: " << infixToPostfix(infix) << endl;
return 0;
}

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;

Node(int value) : data(value), left(nullptr), right(nullptr) {}


};

class BinarySearchTree {
private:
Node* root;

void insert(Node*& node, int value) {


if (!node) {
node = new Node(value);
return;
}
if (value < node->data)
insert(node->left, value);
else
insert(node->right, value);
}

void preOrder(Node* node) {


if (node) {
cout << node->data << " ";
preOrder(node->left);
preOrder(node->right);
}
}

void inOrder(Node* node) {


if (node) {
inOrder(node->left);
cout << node->data << " ";
inOrder(node->right);
}
}

void postOrder(Node* node) {


if (node) {
postOrder(node->left);
postOrder(node->right);
cout << node->data << " ";
}
}

public:
BinarySearchTree() : root(nullptr) {}

void insert(int value) {


insert(root, value);
}

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);

cout << "BFS Traversal: ";


while (!q.empty()) {
int current = q.front();
q.pop();
cout << current << " ";

for (int neighbor : adjList[current]) {


if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
cout << endl;
}
};
int main() {
Graph g(6);

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) {}

void addEdge(int u, int v) {


adjList[u].push_back(v);
adjList[v].push_back(u); // For undirected graph
}

void dfs(int start) {


vector<bool> visited(vertices, false);
stack<int> s;
s.push(start);

cout << "DFS Traversal: ";


while (!s.empty()) {
int current = s.top();
s.pop();

if (!visited[current]) {
cout << current << " ";
visited[current] = true;

for (auto it = adjList[current].rbegin(); it !=


adjList[current].rend(); ++it) {
if (!visited[*it]) {
s.push(*it);
}
}
}
}
cout << endl;
}
};

int main() {
Graph g(6);

g.a ddEdge(0, 1);


g.a ddEdge(0, 2);
g.a ddEdge(1, 3);
g.a ddEdge(2, 3);
g.a ddEdge(3, 4);
g.a ddEdge(4, 5);

g.dfs(0);
return 0;
}

Output:

DFS Traversal: 0 2 3 4 5 1

You might also like