0% found this document useful (0 votes)
11 views25 pages

DSA File Hitesh 138

It is file of data structures and algorithms can be edited to create your own file according to your experiments. It gives you template for practical file

Uploaded by

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

DSA File Hitesh 138

It is file of data structures and algorithms can be edited to create your own file according to your experiments. It gives you template for practical file

Uploaded by

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

Data Structure & Algorithms

Lab Practical File


BTCS303-18

Submitted To:- Submitted By:-


Mr. Chandra Shekar Tiwari Dinesh
B.Tech CSE A (3rd Sem)
Class Roll no. 10102/24
Uni. Roll no.2427790
2427790 BTCS303-18

Index:

S.N Program Page Sign


o. No.
1 Write a program to insert a new element at the 1-2
end as well as at a given position in an array
2. Write a program to delete an element from a given 3-4
whose value is given or whose position is given .
3. Write a program to find the location of a given 5
element using linear search
4. Write a program to find the location of an element 6
using a binary search
5. Write a program to implement push and pop 7-8
operations on a stack using linear array.
6. Write a program to convert an infix expression to a 9-10
postfix expression using stacks .
7. Write a program to evaluate a postfix expression 11-12
using stack.
8. Write a recursive function for Tower of Hanoi 13
program.
9. Write a to implement insertion and deletion 14-15
operations in a queue using linear array .
Write a menu driven program to perform following 16-19
10. operations in a single linked list :
1. insertion at the beginning
2.insertion at end
3.insertion after a given node
4.traversing a linked list
11. Write a menu give me a menu driven program to 20-23
perform following operations in a single linked list :
1. deletion at the beginning
2.deletion at end
3.deletion after a given node
2427790 BTCS303-18
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);

int position;

1
2427790 BTCS303-18
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 :

2
2427790 BTCS303-18
Program 2: Write a program to delete an element from a given
whose value is given or whose position is given .

#include <iostream>
#include <vector>
using namespace std;

void deleteByValue(vector<int>& arr, int value) {


for (auto it = arr.begin(); it != arr.end(); ++it) {
if (*it == value) {
arr.erase(it);
break;
}
}
}

void deleteByPosition(vector<int>& arr, int position) {


if (position >= 0 && position < arr.size()) {
arr.erase(arr.begin() + position);
}
}

void displayArray(const vector<int>& arr) {


for (int num : arr) {
cout << num << " ";
}
cout << endl;
}

int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};

cout << "Original array: ";


displayArray(arr);

// Delete by value
int value = 5;
deleteByValue(arr, value);
cout << "Array after deleting value " << value << ": ";
displayArray(arr);

// Delete by position
int position = 3;
deleteByPosition(arr, position);
cout << "Array after deleting position " << position << ": ";

3
2427790 BTCS303-18
displayArray(arr);

return 0;
}

Output:

4
2427790 BTCS303-18
Program 3: Write a program to find the location of a given element
using linear search

#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:

5
2427790 BTCS303-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:

6
2427790 BTCS303-18
Program 5: Write a program to implement push and pop
operations on a stack using linear array.

#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;
}
}
};
int main() {
Stack myStack;
myStack.push(10);
myStack.push(20);
7
2427790 BTCS303-18
myStack.push(30);
myStack.display();
myStack.pop();
myStack.display();
myStack.pop();
myStack.pop();

myStack.pop();
return 0;
}

Output:

8
2427790 BTCS303-18
Program 6: Write a program to convert an infix expression to a
postfix expression using stacks .

#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();
}
s.push(c);
}
}
while (!s.empty()) {
postfix += s.top();
s.pop();
9
2427790 BTCS303-18
}
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:

10
2427790 BTCS303-18
Program 7: Write a program to evaluate a postfix expression using
stack.

#include <iostream>
#include <stack>
#include <string>
using namespace std;

int evaluatePostfix(string postfix) {


stack<int> s;

for (char c : postfix) {


if (isdigit(c)) {
s.push(c - '0');
} else {
int val1 = s.top();
s.pop();
int val2 = s.top();
s.pop();

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

int main() {
string postfix;
cout << "Enter a postfix expression: ";
cin >> postfix;

11
2427790 BTCS303-18
int result = evaluatePostfix(postfix);
cout << "The result of the postfix expression is: " << result << endl;

return 0;
}

Output:

12
2427790 BTCS303-18
Program 8: Write a recursive function for Tower of Hanoi program.

#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:

13
2427790 BTCS303-18
Program 9: Write a to implement insertion and deletion
operations in a queue using linear array .

#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++;
}
}
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;
14
2427790 BTCS303-18
}
}
};
int main() {
Queue q
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display();
q.dequeue();
q.display();
q.dequeue();
q.dequeue();
q.dequeue();
return 0;
}
Output:

15
2427790 BTCS303-18
Program 10: Write a menu driven program to perform following
operations in a single linked list :
1. insertion at the beginning
2.insertion at end
3.insertion after a given node
4.traversing a linked list

#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;
}

void insertAtEnd(Node** head, int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

void insertAfter(Node* prevNode, int newData) {


if (prevNode == NULL) {
cout << "The given previous node cannot be NULL" << endl;
return;
}
Node* newNode = new Node();
newNode->data = newData;
newNode->next = prevNode->next;
16
2427790 BTCS303-18
prevNode->next = newNode;
}

void displayList(Node* node) {


while (node != NULL) {
cout << node->data << " -> ";
node = node->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = NULL;
int choice, value, position;

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: ";
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;
}
17
2427790 BTCS303-18
{
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;
}
Output:

18
2427790 BTCS303-18

19
2427790 BTCS303-18
Program 11: Write a menu driven program to perform following
deletion operations
in a single linked list:
1. Deletion at the beginning
2. Deletion at end
3. Deletion after a given node

#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;
}

Node* temp = *head;


while (temp->next->next != NULL) {
temp = temp->next;
}

delete temp->next;
temp->next = NULL;
}

20
2427790 BTCS303-18
void deleteAfter(Node* prevNode) {
if (prevNode == NULL || prevNode->next == NULL) {
cout << "No node exists to delete after the given node" << endl;
return;
}

Node* temp = prevNode->next;


prevNode->next = temp->next;
delete temp;
}
void displayList(Node* node) {
while (node != NULL) {
cout << node->data << " -> ";
node = node->next;
}
cout << "NULL" << endl;
}
void createSampleList(Node** head) {
*head = new Node();
(*head)->data = 10;
(*head)->next = new Node();
(*head)->next->data = 20;
(*head)->next->next = new Node();
(*head)->next->next->data = 30;
(*head)->next->next->next = NULL;
}
int main() {
Node* head = NULL;
createSampleList(&head);

int choice, position;

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";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
deleteAtBeginning(&head);
cout << "Updated Linked list: ";
21
2427790 BTCS303-18
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;
}
}

return 0;
}

Output:

22
2427790 BTCS303-18

23

You might also like