Dsa File Programs
Dsa File Programs
1
BTCS301-18
Index:
S.N Program Page Sign
o. No.
1 Write a program to insert a new 3-4.
element at the end as well as at a given
position in an array
2. Write a program to delete an element 5-6.
from a given whose value is given or
whose position is given .
3
BTCS301-18
ARYAN JOSHI (116/23)
Program 1:
Write a program to insert a new element at the end as well as at
a given position in an array
#include <iostream>
using namespace std;
void insertAtEnd(int arr[], int &size, int element) {
arr[size] = element;
size++;
}
void insertAtPosition(int arr[], int &size, int element, int position) {
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 displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[100];
int size = 0;
cout << "Enter the number of elements in the array: ";
cin >> size;
cout << "Enter the elements of the array: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
int newElement;
cout << "Enter the element to insert at the end: ";
cin >> newElement;
insertAtEnd(arr, size, newElement);
cout << "Array after inserting at the end: ";
displayArray(arr, size);
4
BTCS301-18
int position;
cout << "Enter the element to insert at a given position: ";
cin >> newElement;
cout << "Enter the position (0 to " << size << "): ";
cin >> position;
insertAtPosition(arr, size, newElement, position);
cout << "Array after inserting at position " << position << ": ";
displayArray(arr, size);
return 0;
}
Output :
5
BTCS301-18
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// Delete by value
int value = 5;
deleteByValue(arr, value);
cout << "Array after deleting value " << value << ": ";
displayArray(arr);
6
BTCS301-18
// Delete by position
int position = 3;
deleteByPosition(arr, position);
cout << "Array after deleting position " << position << ": ";
displayArray(arr);
return 0;
}
Output:
7
BTCS301-18
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int value) {
for (int i = 0; i < size; ++i) {
if (arr[i] == value) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int size = sizeof(arr) / sizeof(arr[0]);
int value = 50;
int result = linearSearch(arr, size, value);
if (result != -1) {
cout << "Element " << value << " found at index " << result << endl;
} else {
cout << "Element " << value << " not found in the array" << endl;
}
return 0;
}
Output:
8
BTCS301-18
Program 4:
Write a program to find the location of an element using a binary
search
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int value) {
int left = 0;
int right = size - 1;
int n;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == value) {
return mid;
}
if (arr[mid] < value) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int size = sizeof(arr) / sizeof(arr[0]);
int value = 50;
int result = binarySearch(arr, size, value);
if (result != -1) {
cout << "Element " << value << " found at index " << result << endl;
} else {
cout << "Element " << value << " not found in the array" << endl;
}
return 0;
}
Output:
9
BTCS301-18
#include <iostream>
#define MAX 10
using namespace std;j
class Stack {
int top;
int arr[MAX];
public:
Stack() {
top = -1;
}
void push(int value) {
if (top == MAX - 1) {
cout << "Stack overflow" << endl;
} else {
top++;
arr[top] = value;
cout << "Pushed " << value << " onto the stack" << endl;
}
}
void pop() {
if (top == -1) {
cout << "Stack underflow" << endl;
} else {
cout << "Popped " << arr[top] << " from the stack" << endl;
top--;
}
}
void display() {
if (top == -1) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack elements are: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
10
BTCS301-18
}
}
};
int main() {
Stack myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.display();
myStack.pop();
myStack.display();
myStack.pop();
myStack.pop(); Output:
myStack.pop();
return 0;
}
11
BTCS301-18
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
} else if (op == '^') {
return 3;
}
return 0;
}
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
string infixToPostfix(string infix) {
stack<char> s;
string postfix = "";
for (char c : infix) {
if (isdigit(c) || isalpha(c)) {
postfix += c;
} else if (c == '(') {
s.push(c);
} else if (c == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
s.pop(); // Remove '(' from stack
} else if (isOperator(c)) {
while (!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top();
s.pop();
12
BTCS301-18
}
s.push(c);
}
}
while (!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
string infix;
cout << "Enter an infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
Output:
13
BTCS301-18
#include <iostream>
#include <stack>
#include <string>
using namespace std;
switch (c) {
case '+':
s.push(val2 + val1);
break;
case '-':
s.push(val2 - val1);
break;
case '*':
s.push(val2 * val1);
break;
case '/':
s.push(val2 / val1);
break;
}
}
}
return s.top();
14
BTCS301-18
}
int main() {
string postfix;
cout << "Enter a postfix expression: ";
cin >> postfix;
return 0;
}
Output:
15
BTCS301-18
#include <iostream>
using namespace std;
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod <<
endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
16
BTCS301-18
#include <iostream>
#define MAX 100
using namespace std;
class Queue {
int front, rear;
int arr[MAX]
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
return (rear == MAX - 1);
}
bool isEmpty() {
return (front == -1 || front > rear);
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue overflow" << endl;
} else {
if (front == -1) front = 0;
arr[++rear] = value;
cout << "Inserted " << value << " into the queue" << endl;
}
}
void dequeue() {
if (isEmpty()) {
cout << "Queue underflow" << endl;
} else {
cout << "Deleted " << arr[front] << " from the queue" << endl;
front++;
17
BTCS301-18
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Queue q
q.enqueue(10); Output:
q.enqueue(20);
q.enqueue(30);
q.display();
q.dequeue();
q.display();
q.dequeue();
q.dequeue();
q.dequeue();
return 0;
}
18
BTCS301-18
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void insertAtBeginning(Node** head, int newData) {
Node* newNode = new Node();
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}
19
BTCS301-18
}
temp->next = newNode;
}
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at beginning\n";
cout << "2. Insert at end\n";
cout << "3. Insert after a given node\n";
cout << "4. Traverse and display list\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the beginning: ";
cin >> value;
insertAtBeginning(&head, value);
cout << "Updated Linked list: ";
displayList(head);
break;
case 2:
cout << "Enter the value to insert at the end: ";
20
BTCS301-18
cin >> value;
insertAtEnd(&head, value);
cout << "Updated Linked list: ";
displayList(head);
break;
case 3:
cout << "Enter the position after which to insert (1-based index): ";
cin >> position;
if (position < 1) {
cout << "Invalid position!" << endl;
break;
}
{
Node* temp = head;
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL) {
cout << "Invalid position!" << endl;
} else {
cout << "Enter the value to insert: ";
cin >> value;
insertAfter(temp, value);
cout << "Updated Linked list: ";
displayList(head);
}
}
break;
case 4:
cout << "Linked list: ";
displayList(head);
break;
case 5:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
21
BTCS301-18
Outpur:
22
BTCS301-18
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void deleteAtBeginning(Node** head) {
if (*head == NULL) {
cout << "List is already empty" << endl;
return;
}
Node* temp = *head;
*head = (*head)->next;
delete temp;
}
void deleteAtEnd(Node** head) {
if (*head == NULL) {
cout << "List is already empty" << endl;
return;
}
if ((*head)->next == NULL) {
delete *head;
*head = NULL;
return;
}
23
BTCS301-18
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
while (true) {
cout << "\nMenu:\n";
cout << "1. Delete at beginning\n";
cout << "2. Delete at end\n";
cout << "3. Delete after a given node\n";
cout << "4. Display list\n";
cout << "5. Exit\n";
24
BTCS301-18
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteAtBeginning(&head);
cout << "Updated Linked list: ";
displayList(head);
break;
case 2:
deleteAtEnd(&head);
cout << "Updated Linked list: ";
displayList(head);
break;
case 3:
cout << "Enter the position after which to delete (1-based index): ";
cin >> position;
if (position < 1) {
cout << "Invalid position!" << endl;
break;
}
{
Node* temp = head;
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
cout << "Invalid position!" << endl;
} else {
deleteAfter(temp);
cout << "Updated Linked list: ";
displayList(head);
}
}
break;
case 4:
cout << "Linked list: ";
displayList(head);
break;
case 5:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
25
BTCS301-18
return 0;
}
Output:
26