DSA Practical File
DSA Practical File
Practical File
Date of Submission
Total (30)
Exp. Date Title of Experiment Sign. of
No. Faculty
Date of Submission
Total (30)
Exp. Date Title of Experiment Sign. of
No. Faculty
#include <iostream.h>
#include <conio.h>
void main()
{
int LA[100], item, k, i, n, j;
clrscr();
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array:" << endl;
for (i = 0; i < n; i++)
{
cin >> LA[i];
}
cout << "The original array elements are:" << endl;
for (i = 0; i < n; i++)
{
cout << "LA[" << i << "] = " << LA[i] << endl;
}
cout << "Enter the location where the element is to be inserted: "
cin >> k;
cout << "Enter the element to be inserted: ";
cin >> item;
for (j = n - 1; j >= k; j--)
{
LA[j + 1] = LA[j];
}
LA[k] = item;
n++;
cout << "Array elements after insertion:" << endl;
for (i = 0; i < n; i++)
{
cout << "LA[" << i << "] = " << LA[i] << endl;
}
cout << "Enter the location of the element to be deleted: ";
cin >> k;
for (i = k; i < n - 1; i++) {
LA[i] = LA[i + 1];
}
n--;
cout << "Array elements after deletion:" << endl;
for (i = 0; i < n; i++) {
cout << "LA[" << i << "] = " << LA[i] << endl;
}
getch();
}
Output:
2 WAP to search an element in the array using Linear Search.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int arr[10], n, key, i, found = 0;
cout << "Enter the number of elements in the array (max 10): ";
cin >> n;
cout << "Enter " << n << " elements:" << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
if(!found) {
cout << "Element " << key << " not found in the array." << endl;
}
getch();
}
Output:
3 WAP to search an element in the array using Binary Search.
#include <iostream>
using namespace std;
int main()
{
int a[5], i, n,item,mid,beg,end;
cout << "How many values you want to enter:";
cin >> n;
cout <<"enter" <<n<<" values:";
for (i=0; i<5;i++)
{
cin>> a[i];
}
cout <<"array is";
for (i=0; i<5; i++)
{
cout<<a[i]<<" ";
}
cout<<" enter item=";
cin>> item;
{
int beg=0, end=n-1, mid;
mid = int((beg + end)/2);
while ((beg <= end) && a[mid]!= item)
{
if (a[mid] <item)
beg = mid + 1;
else end = mid-1;
mid = int ((beg+mid)/2);
}
if (a[mid] = item)
cout << " element found at" << mid;
else
cout<<" element not found!";
return 0;
}
}
Output:
4 WAP to demonstrate the working of Bubble Sort.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int arr[10], n, i, j, temp;
cout << "Enter " << n << " elements:" << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
getch();
}
Output:
5 WAP to demonstrate the working of Insertion Sort.
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
insertionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
getch();
}
Output:
6 WAP to demonstrate the working of Selection Sort.
#include <iostream.h>
#include <conio.h>
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int A[10][10], B[10][10], result[10][10];
int rowA, colA, rowB, colB;
if (colA != rowB) {
cout << "Matrix multiplication is not possible. Number of colum
getch();
return;
}
cout << "Enter elements of matrix A:\n";
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
cin >> A[i][j];
}
}
getch();
}
Output:
8 Write a menu driven program to implement following operations
on the singly linked list.
Insert a node at the front
Insert a node at the end
Insert a node such that linked list is in ascending order
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void displayList() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty." << endl;
return;
}
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void main() {
clrscr();
int choice, value;
while (1) {
cout << "\nMenu: ";
cout << "\n1. Insert a node at the front";
cout << "\n2. Insert a node at the end";
cout << "\n3. Insert a node in ascending order";
cout << "\n4. Display the list";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert at the front: ";
cin >> value;
insertAtFront(value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(value);
break;
case 3:
cout << "Enter the value to insert in ascending order: ";
cin >> value;
insertInAscendingOrder(value);
break;
case 4:
displayList();
break;
case 5:
cout << "Exiting..." << endl;
getch();
return;
default:
cout << "Invalid choice, please try again." << endl;
}
}
}
Output:
9 Write a menu driven program to implement following.
Delete first node of the linked list
Delete a node before specified position
Delete a node after specified position.
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void deleteFirstNode() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
cout << "First node deleted successfully." << endl;
}
void displayList() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty." << endl;
return;
}
void main() {
clrscr();
int choice, value, position;
while (1) {
cout << "\nMenu: ";
cout << "\n1. Delete the first node";
cout << "\n2. Delete a node before a specified position";
cout << "\n3. Delete a node after a specified position";
cout << "\n4. Display the list";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
deleteFirstNode();
break;
case 2:
cout << "Enter the position before which you want to delete a
cin >> position;
deleteBeforeNode(position);
break;
case 3:
cout << "Enter the position after which you want to delete a n
cin >> position;
deleteAfterNode(position);
break;
case 4:
displayList();
break;
case 5:
cout << "Exiting..." << endl;
getch();
return;
default:
cout << "Invalid choice, please try again." << endl;
}
}
}
Output:
10 WAP to add two Polynomials using Linked List.
#include<iostream.h>
#include<conio.h>
struct Node {
int coefficient;
int exponent;
Node* next;
};
void main() {
clrscr();
Node *poly1 = NULL, *poly2 = NULL;
insertNode(poly1, 3, 3);
insertNode(poly1, 4, 2);
insertNode(poly1, 2, 1);
insertNode(poly1, 1, 0);
insertNode(poly2, 5, 3);
insertNode(poly2, 2, 2);
insertNode(poly2, 6, 0);
cout << "Polynomial 1: ";
displayPolynomial(poly1);
cout << "Polynomial 2: ";
displayPolynomial(poly2);
getch();
}
Output:
11 WAP to demonstrate stack that performs following operations
using array.
PUSH
POP
#include<iostream.h>
#include<conio.h>
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
void push(int value) {
if (top == MAX - 1) cout << "Stack Overflow! Unable to push " << value << end
else { arr[++top] = value; cout << "Pushed " << value << " onto the stack." <
}
void pop() {
if (top == -1) cout << "Stack Underflow! No elements to pop." << endl;
else { cout << "Popped " << arr[top--] << " from the stack." << endl; }
}
void display() {
if (top == -1) cout << "Stack is empty." << endl;
else { cout << "Stack elements: "; for (int i = top; i >= 0; i--) cout << arr
}
};
void main() {
clrscr();
Stack s;
s.push(10); s.push(20); s.push(30);
s.display();
s.pop();
s.display();
s.push(40); s.push(50); s.push(60);
s.display();
s.pop();
s.display();
getch();
}
Output:
12 Write a C++ program that uses stack operations to convert a
given infix expression into its postfix Equivalent, Implement the
stack using an array.
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#define MAX 50
class Stack {
char arr[MAX];
int top;
public:
Stack() { top = -1; }
void push(char c) {
if (top == MAX - 1) {
cout << "Stack Overflow!" << endl;
} else {
top++;
arr[top] = c;
}
}
char pop() {
if (top == -1) {
return ’\0’;
} else {
char temp = arr[top];
top--;
return temp;
}
}
char peek() {
if (top == -1) return ’\0’;
return arr[top];
}
int getPrecedence(char c) {
if (c == ’+’ || c == ’-’) return 1;
if (c == ’*’ || c == ’/’) return 2;
if (c == ’^’) return 3;
return 0;
}
bool isOperand(char c) {
return isalpha(c) || isdigit(c);
}
if (isOperand(current)) {
postfix[k++] = current;
}
postfix[k] = ’\0’;
}
void main() {
clrscr();
infixToPostfix(infix, postfix);
getch();
}
Output:
13 WAP to implement QUEUE using arrays that performs follow-
ing operations.
INSERT
DELETE
DISPLAY
#include <iostream.h>
#include <conio.h>
#define MAX 5
class Queue {
int arr[MAX];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
void deleteQueue() {
if (front == -1) {
cout << "Queue is empty!" << endl;
} else {
cout << "Deleted element: " << arr[front] << endl;
front++;
if (front > rear) {
front = rear = -1;
}
}
}
void display() {
if (front == -1) {
cout << "Queue is empty!" << endl;
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
void main() {
clrscr();
Queue q;
int choice, value;
do {
cout << "1. Insert" << endl;
cout << "2. Delete" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
q.insert(value);
break;
case 2:
q.deleteQueue();
break;
case 3:
q.display();
break;
case 4:
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);
getch();
}
Output:
14 WAP to implement Tree Traversals on Binary Trees.
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = NULL;
}
};
class BinaryTree {
public:
Node* root;
BinaryTree() {
root = NULL;
}
void inorderTraversal() {
inorder(root);
}
void preorderTraversal() {
preorder(root);
}
void postorderTraversal() {
postorder(root);
}
char choice;
cout << "Insert left or right of " << node->data << "? (l/r): ";
cin >> choice;
if (choice == ’l’ || choice == ’L’) {
node->left = insertNode(node->left, value);
} else if (choice == ’r’ || choice == ’R’) {
node->right = insertNode(node->right, value);
} else {
cout << "Invalid choice!" << endl;
}
return node;
}
};
void main() {
clrscr();
BinaryTree tree;
int value;
char more;
do {
cout << "Enter value to insert: ";
cin >> value;
tree.insertNode(tree.root, value);
cout << "Do you want to insert more nodes? (y/n): ";
cin >> more;
} while (more == ’y’ || more == ’Y’);
getch();
}
Output:
15 Write a program that perform operations on Doubly Linked List.
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = next = NULL;
}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() {
head = NULL;
}
void deleteFromBeginning() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
delete temp;
}
}
void deleteFromEnd() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->prev != NULL) {
temp->prev->next = NULL;
}
delete temp;
}
}
void display() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
void displayReverse() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
}
};
void main() {
clrscr();
DoublyLinkedList dll;
int choice, value;
do {
cout << "Menu: \n";
cout << "1. Insert at the beginning\n";
cout << "2. Insert at the end\n";
cout << "3. Delete from the beginning\n";
cout << "4. Delete from the end\n";
cout << "5. Display the list\n";
cout << "6. Display the list in reverse\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert at the beginning: ";
cin >> value;
dll.insertAtBeginning(value);
break;
case 2:
cout << "Enter value to insert at the end: ";
cin >> value;
dll.insertAtEnd(value);
break;
case 3:
dll.deleteFromBeginning();
break;
case 4:
dll.deleteFromEnd();
break;
case 5:
dll.display();
break;
case 6:
dll.displayReverse();
break;
case 7:
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 7);
getch();
}
Output: