Documente Academic
Documente Profesional
Documente Cultură
SUBMITTED TO:-HARPREET
MAM
EXPERIMENT-1
AIM: -WAP to transverse element an array
SOURCE CODE:-
#include<iostream.h>
int main()
int a[40];
int i,n;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i];
return 0;
}
OUTPUT:-
#include<iostream.h>
int main()
int a[20],n,insert, i, p;
cin>>n;
cin>>a[i];
cin>>insert;
cin>>p;
a[i]=a[i-1];
a[p]=insert;
cout<<"Element inserted";
cout<<a[i]<<" ";
}
return 0;
0UTPUT:-
int main()
int a[40];
int i,n,insert=70,p=3;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i];
for(i=p;i<n;i++)
a[i]=a[i+1];
}
cout<<"new array is";
for(i=0;i<n-1;i++)
cout<<a[i]<<" ";
return 0;}
OUTPUT:-
SOURCE CODE: -
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}
OUTPUT:-
}
else if(arr[middle] == search)
{
cout<<search<<" found at location
"<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in
the list.";
}
getch();
}
OUTPUT:-
EXPERIMENT-7
AIM: Program to sort an array of integers in ascending order using
bubble sort.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
OUTPUT: -
Result:-I have leatrnt how to arrange an element in ascending
order using bubble sort in which steps are repeated.
EXPERIMENT-8
AIM:- Write a program to sort of integers in ascending order using insertion sort.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
Output:-
Result:-I have learnt to arrange element in ascending order using
insertion sort.Insertion sort means sort an array one item at a
time.
EXPERIMENT-9
AIM:- Write a program to sort an array of integers in ascending order using selection
Sort.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[100], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
OUTPUT:-
Result:-I have learnt how to write a program using selection sort. In it the
smallest element is selected from the unsorted array and swapped with the
leftmost element.
EXPERIMENT-10
AIM:-Write a program to insert an element in a stack using array.
Source code:-
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";
getch();
exit(1);
}
cout<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
getch();
}
OUTPUT:-
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";
getch();
exit(1);
}
cout<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
getch();
}
OUTPUT:-
EXPERIMENT-12
AIM:- Write a program to convert infix to postfix expression.
Source code:-
cout<<"+num2 = "<<+num2<<endl;
cout<<"-num1 = "<<-num1<<"\t";
cout<<"-num2 = "<<-num2;
getch();
}#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"\nIf:-\n\nnum1 = "<<num1<<"\tnum2 =
"<<num2<<endl<<endl;
cout<<"\nThen:-\n\nnum1 = "<<num1<<"\t";
cout<<"num2 = "<<num2<<endl;
cout<<"+num1 = "<<+num1<<"\t";
OUTPUT:-
EXPERIMENT-13
Aim: Write a program to find factorial of a no. using recursion.
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i, fact=1;
cout<<"Enter a number : ";
cin>>num;
for(i=num; i>0; i--)
{
fact=fact*i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}
OUTPUT:-
int main() {
cout<<"## Insert elements to queue ##\n";
for (int i = 1; i <= 10; i++){
cout<<"Enter Element:";
Insert();
}
return 0;
}
Output:-
Result:-I have learnt how to insert an element using linear queue means
organized form.
EXPERIMENT-15
Aim:- Write a program to delete an element in a linear queue.
Source Code:-
#include <iostream>
using namespace std;
int queue[9], n = 9, front = - 1, rear = - 1;
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() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;;
}
}
int main() {
cout<<"## Insert elements to queue ##\n";
for (int i = 1; i <= 10; i++){
cout<<"Enter Element:";
Insert();
}
cout<<"\nInserted elements in queue\n";
for (int i = front; i <= rear; i++){
cout<<queue[i]<<" ";
}
return 0;
}
OUTPUT:-
Result:- I have learnt how to delete an element of linear queue means which
are present in sequential form.
EXPERIMENT-16
AIM:- WAP to insert element in circular queue.
SOURCE CODE:-
#include <iostream>
return;
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
cqueue[rear] = val ;
void displayCQ() {
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
if (f <= r) {
cout<<cqueue[f]<<" ";
f++;
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
cout<<endl;
int main() {
cout<<"1)Insert\n";
cout<<"2)Display\n";
cout<<"3)Exit\n";
do {
cin>>ch;
switch(ch) {
case 1:
cin>>val;
insertCQ(val);
break;
case 2:
displayCQ();
break;
case 3:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
} while(ch != 3);
return 0;
OUTPUT:-
Result:-I have learnt how to insert an element in circular queue
means in which operation is performed on the basis of first in first
out.
EXPERIMENT-17
AIM:- WAP to implement quick sort of array.
SOURCE CODE:-
#include<iostream>
#include<cstdlib>
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
index = low;
pivot = high;
swap(&a[i], &a[index]);
index++;
swap(&a[pivot], &a[index]);
return index;
n = rand();
swap(&a[high], &a[pvt]);
}
int QuickSort(int a[], int low, int high)
int pindex;
return 0;
int main()
int n, i;
cin>>n;
int arr[n];
cin>>arr[i];
QuickSort(arr, 0, n-1);
cout<<"->"<<arr[i];
return 0;
OUTPUT:-
Result:-I have learnt to implement a program using quick sort. It
gives result on n logn complexity.
EXPERIMENT-18
AIM:- WAP to transverse through linked list.
SOURCE CODE:-
#include <iostream>
using namespace std;
int main() {
node *head = NULL;
for (int i = 0; i < 10; i++) {
node *newEntry = new node;
newEntry->data = i;
newEntry->next = head;
head = newEntry;
}
printList(head);
return 0;
}
OUTPUT:-
EXPERIMENT-19
AIM:- WAP to insert an element in linked list
SOURCE CODE:-
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int size = 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
while (pos--) {
if (pos == 0) {
Node* temp = getNode(data);
temp->next = *current;
*current = temp;
}
else
current = &(*current)->next;
}
size++;
}
}
int main()
{
Node* head = NULL;
head = getNode(3);
head->next = getNode(5);
head->next->next = getNode(8);
head->next->next->next = getNode(10);
size = 4;
data = 1, pos = 1;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 1 at position 1: ";
printList(head);
return 0;
}
OUTPUT:-
Result :-I have learnt about linked list and how to insert an
element in linked list.
EXPERIMENT-20
AIM:- WAP to search an element in linked list
SOURCE CODE:-
#include<iostream>
#include<conio.h>
#include<malloc.h>
struct node
int data;
first, *nw;
int search(int);
int main()
int no,i,item,pos;
first.next=NULL;
nw=&first;
printf("\n");
for(i=0;i< no;i++)
scanf("%d",&nw->data);
nw=nw->next;
nw->next=NULL;
nw=&first;
while(nw->next!=NULL)
printf("%d\t",nw->data);
nw=nw->next;
printf("\n");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
else
getch();
return 0;
int count=1;
nw=&first;
while(nw->next!=NULL)
if(nw->data==item)
break;
else
count++;
nw=nw->next;
return count;
OUTPUT:-
Result:-I have learnt about how to search an element which
are in linked list.
EXPERIMENT-21
AIM:- WAP to delete element in linked list
SOURCE CODE:-
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
struct Node
int data;
};
next = current->next;
free(current);
current = next;
*head_ref = NULL;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
int main()
push(&head, 2);
push(&head, 4);
push(&head, 1);
push(&head, 42);
push(&head, 5);
deleteList(&head);
OUTPUT:-
class Graph
{
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::BFS(int s)
{
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty())
{
s = queue.front();
cout << s << " ";
queue.pop_front();
int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
OUTPUT
EXPERIMENT-23
AIM:- WAP to implement deapth the first search on graph
SOURCE CODE:-
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
OUTPUT:-
Result:- I have learnt about depth search. It is based on
stack. I have also learnt about implement program in depth
seach.
EXPERIMENT-24
AIM- WAP to insert element in binary search tree.
SOURCE CODE
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int key;
struct Node* left, *right;
};
inorder(temp->left);
cout << temp->key << " ";
inorder(temp->right);
}
while (!q.empty()) {
struct Node* temp = q.front();
q.pop();
if (!temp->left) {
temp->left = newNode(key);
break;
} else
q.push(temp->left);
if (!temp->right) {
temp->right = newNode(key);
break;
} else
q.push(temp->right);
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);
return 0;
}
Output-
Result-I have learnt about how to insert elements in binary search tree.
EXPERIMENT-25
AIM- WAP to delete an element in binary search tree.
SOURCE CODE
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
deleteTree(node->left);
deleteTree(node->right);
int main()
{
node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
deleteTree(root);
root = NULL;
return 0;
}
Output:-
Result:- I have learnt how to delete elements from Binary search tree.
EXPERIMENT-26
AIM- WAP to implement in order transversing in binary search tree.
SOURCE CODE
#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void inordertrav();
void inorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::inordertrav()
{
inorder(root);
}
void bst::inorder(tree_node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<" "<<ptr->data<<" ";
inorder(ptr->right);
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20); cout<<"inorder"<<endl;
b.inordertrav();
return 0;
}
Output:-
#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void preordertrav();
void preorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::preordertrav()
{
preorder(root);
}
void bst::preorder(tree_node *ptr)
{
if(ptr!=NULL)
{
cout<<" "<<ptr->data<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<endl<<"preorder"<<endl;
b.preordertrav();
return 0;
}
Output:-
#include<iostream>
using namespace std;
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void postordertrav();
void postorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::postordertrav()
{
postorder(root);
}
void bst::postorder(tree_node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->data<<" ";
}
}
int main()
{
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<endl<<"postorder"<<endl;
b.postordertrav();
return 0;
}
Ouutput:-