0% found this document useful (0 votes)
29 views

Dsa File Programs

The document is a lab practical file for a Data Structure and Algorithm course, authored by Aryan Joshi. It includes a series of programming exercises that cover various data structure operations such as insertion, deletion, searching, stack and queue implementations, and recursive functions. Each program is accompanied by code snippets and expected outputs, demonstrating practical applications of data structures.

Uploaded by

aryanjoshi00
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)
29 views

Dsa File Programs

The document is a lab practical file for a Data Structure and Algorithm course, authored by Aryan Joshi. It includes a series of programming exercises that cover various data structure operations such as insertion, deletion, searching, stack and queue implementations, and recursive functions. Each program is accompanied by code snippets and expected outputs, demonstrating practical applications of data structures.

Uploaded by

aryanjoshi00
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/ 26

BTCS301-18

Data Structure And Algorithm


Lab Practical file
BTCS301-18

Submitted To:- Submitted By:-


Mr Chandra Shekar Tiwari Aryan Joshi
B.Tech CSE A (3rd Sem)
Class Roll no. 116/23
Uni. Roll no.2301601

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. Write a program to find the location of a 7.


given element using linear search

4. Write a program to find the location of 8.


an element using a binary search

5. Write a program to implement push and 9-10.


pop operations on a stack using linear
array.

6. Write a program to convert an infix 11-


expression to a postfix expression using
stacks .
12.

7. Write a program to evaluate a postfix 13-


expression using stack.
14
8. Write a recursive function for Tower of 15.
Hanoi program.
9. Write a to implement insertion and 16-
deletion operations in a queue using
linear array .
17.
Write a menu driven program to 18-
perform following operations in a
10. single linked list :
21
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 22-


program to perform following
operations in a single linked list :
25
1. deletion at the beginning
2
BTCS301-18
2.deletion at end
3.deletion after a given node

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

ARYAN JOSHI (116/23)


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

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

ARYAN JOSHI (116/23)


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:

8
BTCS301-18

ARYAN JOSHI (116/23)

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

ARYAN JOSHI (116/23)


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

ARYAN JOSHI (116/23)


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

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

ARYAN JOSHI (116/23)


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

14
BTCS301-18
}

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

int result = evaluatePostfix(postfix);


cout << "The result of the postfix expression is: " << result << endl;

return 0;
}

Output:

15
BTCS301-18

ARYAN JOSHI (116/23)


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:

16
BTCS301-18

ARYAN JOSHI (116/23)


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

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

ARYAN JOSHI (116/23)


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;

19
BTCS301-18
}
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;
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: ";
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

ARYAN JOSHI (116/23)


Program 11:
write a menu give me a menu driven program to perform
following 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) {

23
BTCS301-18
temp = temp->next;
}

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

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

You might also like