all dsa program
all dsa program
#include <iostream>
using namespace std;
class sparse_matrix {
const static int max = 100;
int **data;
int row, col;
int len;
public:
sparse_matrix(int r, int c) {
row = r;
col = c;
len = 0;
data = new int *[max];
for (int i = 0; i < max; i++) {
data[i] = new int[3]; // 3 for row, column, and value
}
}
sparse_matrix transpose() {
sparse_matrix result(col, row);
result.len = len;
delete[] count;
delete[] index;
return result;
}
void print() {
cout << "\n Dimension: " << row << " x " << col;
cout << "\n Sparse Matrix:\n Row\tColumn\tValue\n";
for (int i = 0; i < len; i++) {
cout << data[i][0] << "\t" << data[i][1] << "\t" << data[i][2] << endl;
}
}
~sparse_matrix() {
for (int i = 0; i < max; i++) {
delete[] data[i];
}
delete[] data;
}
};
int main() {
sparse_matrix a(4, 4);
sparse_matrix b(4, 4);
a.insert(1, 2, 10);
a.insert(1, 4, 12);
a.insert(3, 3, 5);
a.insert(4, 1, 15);
a.insert(4, 2, 12);
b.insert(1, 3, 8);
b.insert(2, 4, 23);
b.insert(3, 3, 9);
b.insert(4, 1, 20);
b.insert(4, 2, 25);
return 0;
}
1b
#include<iostream>
using namespace std;
double evaluate_polynomial(double x,int degree,double coeffs[]){
double result=0;
for(int i=degree;i>=0;i--)
{
result=result *x+coeffs[i];
}
return result;
}
int main()
{
int degree;
double x,coeffs[50],result;
cout<<"Enter the degree of the polynomial :";
cin>>degree;
cout<<"Enter the value of X :";
cin>>x;
cout<<"Enter the cofficient of the polynomial in ascending order :";
for(int i=0;i<=degree;i++){
cin>>coeffs[i];
}
result=evaluate_polynomial(x,degree,coeffs);
cout<<"Result :"<<result;
return 0;
#include<iostream>
2a
using namespace std;
struct Node
{
int data;
Node* next;
};
Node *head=NULL;
void insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
void Delete(int n)
{
Node* temp1=head;
if(n==1)
{
head=temp1->next;
2a
delete temp1;
return;
}
for(int i=0;i<n-2;i++){
temp1=temp1->next;
Node* temp2=temp1->next;
temp1->next=temp2->next;
delete temp2;
}
}
void display()
{
Node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
int choice,x,n;
while(1){
cout<<"1.Insert \n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Exit\n";
cout<<"Enter your choice :";
cin>>choice;
switch(choice){
case 1: cout<<"Enter the element :";
cin>>x;
insert(x);
break;
case 2: cout<<"Enter the element you want to delete :";
cin>>n;
Delete(n);
break;
case 3: display();
break;
case 4: exit(0);
default:cout<<"Invalid input";
}
}
return 0;
}
2b
#include<iostream>
#include<limits>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
int count;
public:
LinkedList() {
head = NULL;
count = 0;
}
~LinkedList() {
Node* current = head;
Node* nextNode;
while (current != NULL) {
nextNode = current->next;
delete current;
current = nextNode;
}
}
void Create() {
int data;
cout << "Enter the data for the node: ";
while (!(cin >> data)) {
cout << "Invalid input! Please enter an integer: ";
cin.clear(); // Clear error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore invalid input
}
Node* newNode = new Node();
newNode->data = data;
newNode->next = head;
head = newNode;
count++;
}
void reverse() {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
2b
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
int countNodes() {
return count;
}
void display() {
Node* current = head;
if (current == NULL) {
cout << "The list is empty." << endl;
return;
}
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
int choice;
LinkedList list;
while (true) {
cout << "1. Create Node" << endl;
cout << "2. Reverse list" << endl;
cout << "3. Search Element" << endl;
cout << "4. Count Nodes" << endl;
2b
cout << "5. Display" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
list.Create();
break;
case 2:
list.reverse();
break;
case 3:
int key;
cout << "Enter the element to be searched: ";
cin >> key;
int index = list.search(key);
if (index == -1) {
cout << "Element not found." << endl;
} else {
cout << "Element found at index: " << index << endl;
}
break;
case 4:
cout << "Number of nodes: " << list.countNodes() << endl;
break;
case 5:
list.display();
break;
case 6:
cout << "Exiting program..." << endl;
return 0;
default:
cout << "Invalid Choice. Please enter a valid choice." << endl;
}
}
return 0;
}
3a
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
node* prev;
};
if (!head) {
head = temp;
} else {
node* ptr = head;
while (ptr->next) {
ptr = ptr->next;
}
ptr->next = temp;
temp->prev = ptr;
}
}
void mergelists() {
if (!head1 || !head2) return;
3a
node* ptr1 = head1;
while (ptr1->next) {
ptr1 = ptr1->next;
}
ptr1->next = head2;
if (head2) {
head2->prev = ptr1;
}
}
while (ptr) {
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
int main() {
// Insert elements into the first list
insert(5, head1);
insert(2, head1);
insert(4, head1);
insert(7, head1);
sortlist(head1); // Sort the first list
printlist(head1);
insert(3, head2);
insert(1, head2);
insert(6, head2);
insert(9, head2);
sortlist(head2);
printlist(head2);
mergelists();
printlist(head1);
return 0;
}
3b
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
node* prev;
};
if (head == NULL) {
newnode->prev = NULL;
head = newnode;
return;
}
last->next = newnode;
newnode->prev = last;
}
if (position == 0) {
head = temp->next;
3b
if (head != NULL)
head->prev = NULL;
delete temp;
return;
}
delete temp->next;
temp->next = next;
if (next != NULL) {
next->prev = temp;
}
}
void printlist() {
node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
int choice, data, position;
while (true) {
cout << "1. Insert at front" << endl;
cout << "2. Insert at end" << endl;
cout << "3. Delete a node" << endl;
cout << "4. Print the list" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice : " << endl;
switch (choice) {
case 1:
cout << "Enter data: ";
cin >> data;
push(data);
3b
break;
case 2:
cout << "Enter data: ";
cin >> data;
insert(data);
break;
case 3:
cout << "Enter position: ";
cin >> position;
deletenode(position);
break;
case 4:
printlist();
break;
case 5:
return 0;
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}
#include<iostream>
4a
using namespace std;
struct node {
int data;
node* next;
node* prev;
};
class circularlinkedlist{
private:
node* head;
public:
circularlinkedlist()
{
head = NULL;
}
if(head != NULL)
{
node* temp = head;
while(temp->next != head)
{
temp = temp->next;
}
temp->next = newnode;
newnode->prev = temp;
}
else
{
head = newnode;
newnode->next = newnode;
newnode->prev = newnode;
}
}
if(head != NULL)
{
node* temp = head;
while(temp->next != head)
{
temp = temp->next;
}
temp->next = newnode;
newnode->prev = temp;
newnode->next = head;
head->prev = newnode;
}
else
{
head = newnode;
newnode->next = newnode;
newnode->prev = newnode;
}
}
void deleteatend()
4a
{
if(head == NULL)
{
return;
}
while(temp->next != head)
{
temp = temp->next;
}
void deleteatbeginning()
{
if(head == NULL)
{
return;
}
void Display()
{
4a
int main()
{
circularlinkedlist cll;
cll.insertAtEnd(1);
cll.insertAtEnd(2);
cll.insertAtEnd(3);
cll.insertatbeginning(0);
cll.deleteatend();
cll.deleteatbeginning();
cll.Display();
return 0;
}
#include<iostream> 4b
#include<math.h>
struct node {
int coeff, powx, powy;
struct node* next;
};
// Function to create a new node and add it to the circular linked list
void create_node(int c, int p1, int p2, struct node** temp) {
struct node* r;
struct node* z = *temp;
r = new node(); // Use 'new' instead of 'malloc' in C++
r->coeff = c;
r->powx = p1;
r->powy = p2;
if (z == NULL) {
4b
(*temp) = r;
(*temp)->next = (*temp);
} else {
r->next = z->next;
z->next = r;
(*temp) = r;
}
}
do {
result += temp->coeff * pow(x, temp->powx) * pow(y, temp->powy);
temp = temp->next;
} while (temp != poly);
return result;
}
int main() {
struct node* poly1 = NULL, *poly2 = NULL, *result = NULL;
int choice, c, p1, p2, x, y;
while (true) {
cout << "\nMenu:\n";
cout << "1. Create first polynomial\n";
cout << "2. Create second polynomial\n";
cout << "3. Display first polynomial\n";
cout << "4. Display second polynomial\n";
cout << "5. Add polynomials\n";
cout << "6. Evaluate polynomial\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter coefficient, power of x, and power of y for the term: ";
cin >> c >> p1 >> p2;
create_node(c, p1, p2, &poly1);
break;
case 2:
cout << "Enter coefficient, power of x, and power of y for the term: ";
cin >> c >> p1 >> p2;
create_node(c, p1, p2, &poly2);
break;
4b
case 3:
cout << "First polynomial: ";
display_polynomial(poly1);
break;
case 4:
cout << "Second polynomial: ";
display_polynomial(poly2);
break;
case 5:
add_polynomials(poly1, poly2, &result);
cout << "Result of addition: ";
display_polynomial(result);
break;
case 6:
cout << "Enter value of x and y: ";
cin >> x >> y;
cout << "Evaluation result: " << evaluate_polynomial(poly1, x, y) << endl;
break;
case 7:
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
};
#include<iostream> 5a
#include<stack>
using namespace std;
while (!s.empty()) {
postfix += s.top();
s.pop();
}
int main() {
string infix = "((a+(b*c)-d))";
string postfix;
postfix = convert(infix);
return 0;
}
5b
#include<iostream>
#include<stack>
using namespace std;
string infixtopostfix(string infix)
{
stack<char> s;
string prefix=" ";
for(int i=0;i<infix.length();i++)
if(isalnum(infix[i]))
{
prefix+=infix[i];
}
else{
while(!s.empty() && s.top()!='(' && s.top()!=')')
{
prefix +=s.top();
s.pop();
}
s.push(infix[i]);
}
while(!s.empty())
{
prefix+=s.top();
s.pop();
}
return prefix;
}
int main()
{
string infix="A*B+C*D";
cout<<"Infix :"<<infix<<endl;
cout<<"postfix :"<<infixtopostfix(infix);
return 0;
#include<iostream> 6a
using namespace std;
class circularqueue{
int * queue,size,front,rear;
public:
circularqueue(int s){
size=s;
queue=new int[size];
front = rear = -1;
}
6a
void enqueue(int x);
int dequeue();
void display();
};
void circularqueue::enqueue(int x)
{
if(front==0 && rear==size-1)
{
cout<<"Queue is full\n";
return ;
}
else if(front== -1)
{
front=rear=0;
}
else if(rear==size-1 && front!=0){
rear=0;
}
else{
rear++;
}
queue[rear]=x;
}
int circularqueue::dequeue(){
if(front==-1){
cout<<"Queue is empty \n";
return -1;
}
int x=queue[front];
if(front==rear)
{
front=rear=-1;
}
else if(front==size-1){
front =0;}
else{
front ++;
}
return x;
}
void circularqueue::display()
{
if(front== -1)
{
cout<<"Queue is empty\n";
return;
}
6a
if(rear>=front)
{
for(int i=front;i<rear;i++)
cout<<queue[i]<<" ";
}
else
{for(int i=front;i<size;i++)
cout<<queue[i]<<" ";
for(int i=0;i<=rear;i++)
cout<<queue[i]<<" ";
}
}
int main(){
circularqueue q(5);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
q.display();
q.dequeue();
q.dequeue();
q.display();
return 0;
}
#include <iostream> 6b
using namespace std;
struct node {
int data;
node* next;
struct Queue {
node* front, *rear;
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.dequeue(); // Removes 10 from the queue
q.dequeue(); // Removes 20 from the queue
q.enqueue(40); // Adds 40 to the queue
if (q.rear != NULL) {
cout << "Queue rear: " << q.rear->data << endl;
} else {
cout << "Queue is empty" << endl;
}
return 0;
}
7a
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
while (!s.empty())
{
int node = s.top();
s.pop();
int main()
{
int v = 5; // Number of vertices
int e = 4; // Number of edges
7a
adj = vector<vector<int> >(v, vector<int>(v, 0)); // Initialize adjacency matrix
return 0;
}
#include <bits/stdc++.h> 7b
using namespace std;
// BFS Traversal
void bfs(int start, vector<bool>& visited) {
vector<int> q;
q.push_back(start);
visited[start] = true;
while (!q.empty()) {
int vis = q[0];
cout << vis << " ";
q.erase(q.begin()); // Dequeue
// DFS Traversal
7b
int main() {
int v = 5; // Number of vertices
int e = 4; // Number of edges
// Adding edges
addedge(0, 1);
addedge(0, 2);
addedge(0, 3);
addedge(0, 4);
// DFS Traversal
cout << "DFS traversal starting from vertex 0: ";
dfs(0, visited);
cout << endl;
// BFS Traversal
cout << "BFS traversal starting from vertex 0: ";
bfs(0, visited);
cout << endl;
return 0;
}
8a
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
void init() {
for (int i = 0; i < MAX; i++)
id[i] = i;
}
int root(int x) {
while (id[x] != x) {
id[x] = id[id[x]];
x = id[x];
}
return x;
}
if (root(x) != root(y)) {
minimumcost += cost;
cout << x << " - " << y << " : " << p[i].first << endl;
union1(x, y);
}
}
return minimumcost;
}
int main() {
8a
int x, y;
long long weight, minimumcost;
init();
sort(p, p + edges);
minimumcost = kruskal(p);
cout << "Minimum cost of the spanning tree: " << minimumcost << endl;
return 0;
}
#include<stdio.h> 8b
#include<limits.h>
#include<stdbool.h>
#define V 9
int mindistance(int dist[], bool sprser[])
{
int min = INT_MAX, min_index;
dist[src] = 0;
sprser[u] = true;
printsolution(dist);
}
int main()
{
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 4, 0, 0, 0, 2, 0, 1, 6},
{0, 0, 4, 14, 2, 0, 1, 0, 7},
{0, 0, 0, 0, 0, 1, 0, 2, 3},
{8, 11, 0, 0, 1, 0, 2, 0, 0},
{0, 0, 2, 0, 6, 7, 3, 0, 0}
};
dijikstra(graph, 0);
return 0;
}
9a
#include <iostream>
using namespace std;
int i = 0, j = 0, k = l;
int m = l + (r - l) / 2;
mergesort(arr, l, m);
mergesort(arr, m + 1, r);
merge(arr, l, m, r);
9a
}
}
int main() {
mergesort(arr, 0, n - 1);
return 0;
}
#include<iostream> 9b
using namespace std;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
heapsort(arr, n);
return 0;
}
#include<iostream> 10a
using namespace std;
int sequentialsearch(int array[],int size,int key)
{
for(int i=0;i<size;i++)
{
if(array[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
int array[]={1,2,3,4,5};
int size=sizeof(array)/sizeof(array[0]);
int key=3;
int index=sequentialsearch(array,size,key);
if(index!=-1)
{
cout<<"key found at index :"<<index;
}
else{
cout<<"key not found:";
}
return 0;
}
11a
#include<iostream>
using namespace std;
struct node {
int data;
node* left;
node* right;
};
int main() {
node* root = getnewnode(1);
root->left = getnewnode(2);
root->right = getnewnode(3);
root->left->left = getnewnode(4);
root->left->right = getnewnode(5);
return 0;
}
11b
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct node {
int data;
node* left, * right;
};
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* current = q.front();
q.pop();
cout << current->data << " ";
if (current->left != NULL)
q.push(current->left);
if (current->right != NULL)
q.push(current->right);
}
}
int main() {
node* root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
cout << "Height of tree: " << height(root) << endl;
cout << "Leaf nodes of original tree: ";
printleafnodes(root);
cout << endl;
node* mirrorroot = mirror(root);
cout << "Original tree (level-wise): ";
printlevelwise(root);
cout << endl;
cout << "Mirror image (level-wise): ";
printlevelwise(mirrorroot);
cout << endl;
return 0;
}