DS File Layout
DS File Layout
OF PROFESSIONAL STUDIES,
RAIPUR
PRACTICAL ON
(CASC-06T)
BCA-II
#include <iostream>
int main() {
int n;
cin >> n;
int matrix[n][n];
} }
return 0;
Output
Programs
Q2. Write a program to perform addition and subtraction on two matrices.
#include<iostream>
int main()
int i,j,a[10][10],b[10][10],c[10][10],d[10][10],n,m;
cin>>n>>m;
for(i=0; i<n;i++)
for(j=0;j<m;j++)
cin>>a[i][j];
for(j=0;j<m;j++ )
cin>>b[i][j];
for(j=0;j<m ;j++)
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
{
Programs
for(j=0; j<m; j++ )
cout<<c[i][j]<<" ";
cout<<"\n";
cout<<d[i][j]<<" ";
cout<<"\n";
return 0;
Output
Programs
Q3. Write a program to perform multiplication on two matrices.
#include <iostream>
int main() {
if (c1 != r2) {
return 0; }
for (int j = 0; j < c2; cout << C[i][j++] << " ");
return 0;
} Output
Programs
Q4. Write a program to perform insertion , deletion of nodes from the end in singly linked list.
#include <iostream>
struct Node {
int data;
Node* next;
};
// Insert at end
if (!head)
head = newNode;
else {
while (temp->next)
temp = temp->next;
temp->next = newNode;
void deleteEnd() {
if (!head) {
return;
if (!head->next) {
delete head;
head = nullptr
return;
while (temp->next->next)
temp = temp->next;
delete temp->next;
Programs
temp->next = nullptr;
// Display list
void display() {
while (temp) {
temp = temp->next;
int main() {
insertEnd(5);
insertEnd(10);
insertEnd(15);
display();
deleteEnd();
display();
return 0;
Output
Programs
Q5. Write a program to perform insertion and deletion of nodes from the end in circular doubly linked list.
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev; };
class CircularDoublyLinkedList {
public:
CircularDoublyLinkedList() : head(nullptr) {}
newNode->data = value;
if (!head) {
head = newNode;
head->next = head;
head->prev = head;
} else {
tail->next = newNode;
newNode->prev = tail;
newNode->next = head;
head->prev = newNode;
} }
void deleteFromEnd() {
if (!head) {
return; }
if (head == tail) {
delete head;
head = nullptr;
} else {
head->prev = newTail;
delete tail;
} }
void display() {
if (!head) {
return; }
do {
temp = temp->next;
private:
Node* head;
}; int main() {
CircularDoublyLinkedList list;
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
Output
Programs
Q6. Write a program to perform push and pop operations in stack, where stack should be created using array.
#include <iostream>
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
} }
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
top--;
} }
void display() {
if(top>=0) {
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty"; }
int main() {
cout<<"4) Exit"<<endl;
do {
cin>>ch;
Programs
switch(ch) {
case 1: {
cin>>val;
push(val);
break; }
case 2: {
pop();
break; }
case 3: {
display();
break; }
case 4: {
cout<<"Exit"<<endl;
break; }
default: {
cout<<"Invalid Choice"<<endl; } }
} while(ch!=4);
return 0;
Output
Programs
Q7. Write a program to perform push and pop operations in stack, where stack should be created using linked list.
#include <iostream>
struct Node {
int data;
newnode->data = val;
newnode->next = top;
top = newnode; }
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
top = top->next; } }
void display() {
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
ptr = ptr->next; } }
cout<<endl; }
int main() {
cout<<"4) Exit"<<endl;
Programs
do {
cin>>ch;
switch(ch) {
case 1: {
cin>>val;
push(val);
break; }
case 2: {
pop();
break; }
case 3: {
display();
break; }
case 4: {
cout<<"Exit"<<endl;
break; }
default: {
cout<<"Invalid Choice"<<endl; } }
} while(ch!=4);
return 0;
Output
Programs
Q8. Write a program to calculate factorial of a given number using stack.
#include <iostream>
#include <stack>
int factorialUsingStack(int n) {
stack<int> s;
[Link](i); }
int result = 1;
while (![Link]()) {
result *= [Link]();
[Link](); }
return result; }
int main() {
int number;
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
cout << "Factorial of " << number << " is: " << result << endl; }
return 0;
Output
Programs
Q9. Write a program to perform insertion and deletion of data items in queue should be implemented by using a linked list.
#include <iostream>
// Node structure
struct Node {
int data;
Node* next; };
// Queue class
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = nullptr;
rear = nullptr; }
newNode->data = value;
newNode->next = nullptr;
if (rear == nullptr) {
} else {
rear->next = newNode;
rear = newNode; }
void dequeue() {
if (front == nullptr) {
return; }
front = front->next;
Programs
if (front == nullptr) {
rear = nullptr; }
delete temp; }
void display() {
if (front == nullptr) {
return; }
temp = temp->next; }
int main() {
Queue q;
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
return 0; }
Output
Programs
Q10. Write a program to perform insertion and deletion of data items in queue should be implemented by using arrays.
#include <iostream>
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val; } }
void Delete() {
return ;
} else {
front++;; } }
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl; } }
int main() {
int ch;
do {
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
Output
Programs
Q11. Write a program to demonstrate functioning of a double ended queue.
#include <iostream>
#include <deque>
int main() {
std::deque<int> deq;
deq.push_front(10);
deq.push_front(20);
deq.push_front(30);
deq.push_back(40);
deq.push_back(50);
deq.push_back(60);
deq.pop_front();
deq.pop_front();
deq.pop_back();
deq.pop_back();
return 0; } Output
Programs
Q12. Write a program to read the postfix arithmetic expression and evaluate its value using the stack.
#include <iostream>
#include <stack>
#include <string>
char operation)
switch (operation) {
case '+':
case '-':
case '*':
case '/':
default:
return 0;
stack<int> stack;
if (isdigit(c)) {
// stack
[Link](c - '0');
Programs
}
else {
[Link]();
[Link]();
int result
[Link](result);
// stack
return [Link]();
int main()
return 0;
Output
Programs
Q13. Write a program to show how to handle the overflow and underflow situation in stack.
#include <iostream>
class Stack {
private:
int arr[MAX];
int top;
public:
Stack() {
top = -1; }
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << "Pushed " << value << " to stack." << endl; } }
void pop() {
if (top < 0) {
} else {
cout << "Popped " << arr[top--] << " from stack." << endl; } }
void display() {
if (top < 0) {
} else {
};
Programs
int main() {
Stack s;
// Testing overflow
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
[Link]();
// Testing underflow
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
Output
Programs
Q14. Write a program to convert infix notation-based expression into the postfix notation-based expression using the stack.
#include <iostream>
#include <stack>
#include <cctype>
return 0; }
stack<char> st;
string res;
if (isalnum(c)) res += c;
else if (c == ')') {
} else {
[Link](c); } }
return res; }
int main() {
string exp;
return 0;
Output
Programs
Q15. write a program to implement the concept of priority based element traversing using priority queue.
#include <iostream>
#include <queue>
#include <vector>
struct Element {
int value;
int priority;
// Constructor
// Custom comparator for the priority queue (higher priority comes first)
struct ComparePriority {
int main() {
[Link](Element(10, 2));
[Link](Element(20, 4));
[Link](Element(30, 1));
[Link](Element(40, 3));
while (![Link]()) {
Element e = [Link]();
cout << "Value: " << [Link] << ", Priority: " << [Link] << endl;
[Link](); }
return 0; } Output
Programs
Q16. Write a program to create binary search tree using the concept of linked list and array , suppose data set will be given
at the run time.
#include <iostream>
struct Node {
int data;
Node* left;
Node* right; };
newNode->data = value;
return newNode; }
if (root == nullptr) {
return createNode(value); }
return root;}
if (root != nullptr) {
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
Programs
int n;
cin >> n;
inorderTraversal(root);
delete[] arr;
return 0;
Output
Programs
Q17. Write a program to create a binary search tree with any data set and traverse the data items in pre-order ,in -order
and post-order manner using recursion.
#include <iostream>
struct Node {
int data;
};
return root;
void inorder(Node* r) { if (r) { inorder(r->left); cout << r->data << " "; inorder(r->right); } }
void preorder(Node* r) { if (r) { cout << r->data << " "; preorder(r->left); preorder(r->right); } }
void postorder(Node* r) { if (r) { postorder(r->left); postorder(r->right); cout << r->data << " "; } }
int main() {
return 0;
Output
Programs
Q18. Write a program to perform deletion of any data item from the binary search tree.
#include <iostream>
struct Node {
int data;
};
return root;
return root;
else {
root->data = temp->data;
return root;
}
Programs
void inorder(Node* root) {
if (root) {
inorder(root->left);
inorder(root->right);
int main() {
inorder(root);
inorder(root);
return 0;
Output
Programs
Q19. Write a program to find the height of any tree.
#include <iostream>
struct Node {
int data;
};
int main() {
return 0;
Output
Programs
Q20. Write a program to create any given undirected graph using the adjacency matrix, and print each node/element with
list of its adjacent elements.
#include <iostream>
#include <vector>
int main() {
int v, e;
while (e--) {
int u, w;
adj[u][w] = adj[w][u] = 1;
return 0;
Output
Programs
Q21. Write a program to traverse the element of given graph according BFS and DFS.
#include <iostream>
#include <vector>
#include <queue>
while (![Link]()) {
int main() {
vector<vector<int>> g(n);
g[0] = {1, 2}; g[1] = {0, 3}; g[2] = {0, 4}; g[3] = {1}; g[4] = {2};
return 0;
Output
Programs
Q22. Write a program to find the minimum spanning tree of any given graph.
#include <vector>
#include <climits>
return idx;
mstSet[u] = true;
cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n";
int main() {
Programs
int V;
cin >> V;
primMST(graph, V);
return 0;
Output
Programs
Q23. Write a program to search any run time given element from the array of 10 elements in the array are unsorted.
#include <iostream>
int main() {
int arr[10];
cout << "Enter 10 elements for the array (unsorted):" << endl;
// Linear search
if(arr[i] == key) {
found = 1;
break;
if(!found) {
return 0;
Output
Programs
Q24. Write a program to demonstrate the binary search.
#include <iostream>
int low = 0;
if (arr[mid] == key)
else
int main() {
int key;
if (result != -1)
cout << "Element found at index " << result << endl;
else
return 0;
} Output
Programs
Q25. Write a program to find the smallest and largest element in any array.
#include <iostream>
int main() {
int n;
cin >> n;
int arr[n];
smallest = arr[i];
largest = arr[i]; }
return 0;
Output
Programs
Q26. Write a program to arrange the data items of any array in ascending order.
#include <iostream>
int main() {
int n, temp;
cin >> n;
int arr[n];
cout << "Enter " << n << " elements:" << endl;
// Swap
temp = arr[j];
arr[j + 1] = temp;
return 0;
} Output
Programs
Q27. Write a program to arrange the data items of any array in descending order using quick sort.
#include <iostream>
return i + 1;
quickSort(arr, pi + 1, high);
for (int i = 0; i < n; i++) cout << arr[i] << " ";
int main() {
quickSort(arr, 0, n - 1);
return 0;
Output
Programs