Professional Documents
Culture Documents
PROGRAMMES :
WAP to find the mean and median of no stored in array
#include <iostream>
using namespace std;
return (double)sum/(double)n;
}
// Function for calculating median
double findMedian(int a[], int n)
{
// First we sort the array
for (int i=0; i<n ; ++i)
{
cout<<a[i]<<" ";
}
// Driver program
int main()
{
int a[] = {100,200,300,400,500,600,700,800};
int n = sizeof(a)/sizeof(a[0]);
cout << "Mean = " << findMean(a, n) << endl;
cout << "Median = " << findMedian(a, n) << endl;
return 0;
}
arr[n] = key;
return (n + 1);
}
// Driver Code
int main()
{
int arr[20] = {100,200,300,400,500,600};
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 226;
cout << "\n Before Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i]<< " ";
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
#include <iostream>
using namespace std;
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 100,200,300,400,500 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
if(result == -1)
cout<<"Element is not present in array";
else
cout<<"Element is present at index " <<result;
return 0;
}
4. WAP to sort an array.
#include<iostream>
using namespace std;
int main()
{
int arr[]={400,500,200,100,300};
int n=5;
cout<<"unsorted array : ";
for (int i = 0; i < n; ++i)
{
cout<<arr[i]<<" ";
}
for (int i=0;i<n;++i)
{
for (int j=i+1; j<n; ++j)
{
if(arr[j]<arr[i])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
// Driver code
int main()
{
int arr1[] = {1, 3, 5, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr3[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
return 0;
}
6. WAP to store mark obtained by 10 students in 5 subjects in 2
dimensional array
#include<iostream>
using namespace std;
int main()
{
int arr[10][5];
for(int i=0;i<10;i++)
{
cout<<"Enter Marks of student "<<i+1<<": ";
for(int j=0;j<5;j++)
{
cin>>arr[i][j];
}
cout<<endl;
}
return 0;
}
7. WAP to implement the linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LL
{
Node* head = NULL;
public:
KK(int new_data)
{
Node* n = new Node;
n->data = new_data;
n->next = nullptr;
head = n;
}
void insertNode(int new_data)
{
Node* temp = head;
while(temp->next != nullptr)
{
temp = temp->next;
}
Node* n = new Node;
n->data = new_data;
n->next = nullptr;
temp->next = n;
}
void deleteNode(int position)
{
if(position == 0)
{
Node* temp = head;
head = head->next;
delete temp;
}
Node* temp = head;
Node* prev = head;
while(position > 0 && temp->next != nullptr)
{
prev = temp;
temp = temp->next;
position--;
}
if(position != 0)
{
cout << "Error";
return;
}
prev->next = temp->next;
delete temp;
}
int search(int query)
{
Node* temp = head;
int position = 0;
while(temp != nullptr)
{
if(temp->data == query)
{
return position;
}
position++;
temp = temp->next;
}
return position;
}
void print()
{
Node* temp = head;
while(temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
}
};
int main()
{
KK* myLinkedList = new KK(1);
myLinkedList->insertNode(100);
myLinkedList->insertNode(200);
myLinkedList->insertNode(300);
myLinkedList->insertNode(400);
myLinkedList->insertNode(500);
myLinkedList->print();
cout << endl;
return 0;
}
class Node
{
public:
int data;
Node* next;
};
class KK
{
Node* head = nullptr;
public:
KK(int new_data)
{
Node* n = new Node;
n->data = new_data;
n->next = nullptr;
head = n;
}
void insertNode(int new_data)
{
Node* temp = head;
while(temp->next != nullptr)
{
temp = temp->next;
}
Node* n = new Node;
n->data = new_data;
n->next = nullptr;
temp->next = n;
}
void deleteNode(int position)
{
if(position == 0)
{
Node* temp = head;
head = head->next;
delete temp;
}
Node* temp = head;
Node* prev = head;
while(position > 0 && temp->next != nullptr)
{
prev = temp;
temp = temp->next;
position--;
}
if(position != 0)
{
cout << "Error";
return;
}
prev->next = temp->next;
delete temp;
}
void print()
{
Node* temp = head;
while(temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
}
};
int main()
{
KK* myLinkedList = new KK(1);
myLinkedList->insertNode(100);
myLinkedList->insertNode(200);
myLinkedList->insertNode(300);
myLinkedList->insertNode(400);
myLinkedList->insertNode(500);
myLinkedList->print();
cout << endl;
myLinkedList->deleteNode(300);
myLinkedList->print();
return 0;
}
// Structure of a node.
struct node
{
int data;
struct node* next;
node(int x)
{
data=x;
next=NULL;
}
};
int main()
{
// Making a linked list for testing purpose.
}
10. WAP to reverse a linked list
#include<iostream>
struct node
{
int data;
struct node *next;
};
public:
int a[MAX];
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1))
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
cout << x<<endl;
return true;
}
}
int Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--];
cout<<"\n";
return x;
}
}
int Stack::peek()
{
if (top < 0)
{
cout << "Stack is Empty";
return 0;
}
else
{
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int main()
{
class Stack s;
s.push(100);
s.push(200);
s.push(300);
s.push(400);
cout << s.pop();
return 0;
}
14. WAP to implement stack using linked list.
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *head;
int push(int obj);
int pop();
int display();
int main()
{
char ch,c;
int del,obj,sol;
do{
cout<<"\n PUSH/INSERTION-1/n POP/DELETION-2/DISPLAY-3";
cin>>ch;
switch(ch)
{
case '1':
cout<<"\n enter element in stack";
cin>>obj;
push(obj);
break;
case '2':
del=pop();
if(del!=-1)
cout<<"\n element deleted = "<<del;
break;
case '3':
display();
break;
default:
cout<<"\n oops wrong operation\n you can try again";
}
cout<<"\n wanna try again";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
int push(int obj)
{
node *temp;
temp=new node;
if(temp==NULL)
{
cout<<"memory not allocated";
exit(0);
}
if(head==NULL)
{
temp->next=head;
temp->data=obj;
head=temp;
}
else
{
node *trv;
trv=head;
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=temp;
temp->data=obj;
temp->next=NULL;
}
return 0;
}
int pop()
{
if(head==NULL)
{
cout<<"\n stack is empty";
return -1;
}
else
{
node *trv,*pre;
trv=head;
while(trv->next!=NULL)
{
pre=trv;
trv=trv->next;
}
pre->next=NULL;
int temp=trv->data;
return temp;
}
}
int display()
{
if(head==NULL)
{
cout<<"\n stack is empty";
}
else
{
node *trv;
trv=head;
while(trv!=NULL)
{
cout<<trv->data<<" ";
trv=trv->next;
}
}
return 0;
}
15.
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
default:
printf("Wrong Choice: please see the options");
}
}
}
}
struct QNode
{
int data;
QNode* next;
QNode(int d)
{
data = d;
next = NULL;
}
};
struct Queue
{
QNode *front, *rear;
Queue()
{
front = rear = NULL;
}
void enQueue(int x)
{
QNode* temp = new QNode(x);
if (rear == NULL)
{
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
void deQueue()
{
if (front == NULL)
return;
QNode* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
delete (temp);
}
};
int main()
{
Queue q;
q.enQueue(100);
q.enQueue(200);
q.deQueue();
q.enQueue(300);
q.enQueue(400);
q.enQueue(1000);
q.deQueue();
cout << "Queue Front : " << (q.front)->data << endl;
cout << "Queue Rear : " << (q.rear)->data;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct Queue
{
// Initialize front and rear
int rear, front;
// Circular Queue
int size;
int *arr;
Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}
else
{
rear++;
arr[rear] = value;
}
}
return data;
}
q.displayQueue();
q.enQueue(1000);
q.enQueue(2000);
q.enQueue(3000);
q.displayQueue();
q.enQueue(20);
return 0;
}
18. WAP to implement priority queue using linked list.
#include <stdio.h>
#include <stdlib.h>
// Node
typedef struct node {
int data;
} Node;
return temp;
}
// Driver code
int main()
{
// Create a Priority Queue
// 7->4->5->6
Node* pq = newNode(500, 1);
push(&pq, 400, 2);
push(&pq, 300, 3);
push(&pq, 200, 0);
while (!isEmpty(&pq)) {
printf("%d ", peek(&pq));
pop(&pq);
}
return 0;
}
public:
Deque()
{
front = rear = NULL;
Size = 0;
}
// Operations on Deque
void insertFront(int data);
void insertRear(int data);
void deleteFront();
void deleteRear();
int getFront();
int getRear();
int size();
bool isEmpty();
void erase();
};
Size++;
}
}
dq.deleteRear();
cout << "After deleting rear element new rear"
<< " is: " << dq.getRear() << endl;
dq.deleteFront();
cout << "After deleting front element new "
<< "front is: " << dq.getFront() << endl;
return 0;
}
return 0;
}
21. WAP to construct a binary search tree.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *left, *right;
};
Node* getNode(int data)
{
Node *newNode = new Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node *lvlOrd(Node *root , int data)
{
if(root==NULL)
{
root = getNode(data);
return root;
}
if(data <= root->data)
root->left = lvlOrd(root->left, data);
else
root->right = lvlOrd(root->right, data);
return root;
}
Node* makeTree(int arr[], int n)
{
if(n==0)
return NULL;
Node *root= NULL;
for(int i=0;i<n;i++)
root = lvlOrd(root , arr[i]);
return root;
}
void inord(Node* root)
{
if (!root)
return;
inord(root->left);
cout << root->data << " ";
inord(root->right);
}
int main()
{
int arr[] = {100,200,300,400,500,600,700,800,900};
int n = sizeof(arr) / sizeof(arr[0]);
Node *root = makeTree(arr, n);
cout << "Inorder Traversal: ";
inord(root);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
// Function to calculate distance
float distance(int x1, int y1, int x2, int y2)
{
// Calculating distance
return sqrt(pow(x2 - x1, 2) +
pow(y2 - y1, 2) * 1.0);
}
// Drivers Code
int main()
{
cout << distance(3, 4, 4, 3);
return 0;
}
return graph;
}
return subsets[i].parent;
}
// Driver code
int main()
{
/* Let us create following weighted graph
10
0--------1
|\|
6| 5\ |15
|\|
2--------3
4 */
int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph
Graph* graph = createGraph(V, E);
KruskalMST(graph);
return 0;
}