Nabia Noor Roll No. 063 Bsit 3 Semester Section A
Nabia Noor Roll No. 063 Bsit 3 Semester Section A
PROGRAMS
PROGRAM 01:
Write a Program to compute the sum of values of elements of a linear array having 10
elements?
#include<iostream>
class temp
private:
int abc[10],sum;
public:
temp()
sum=0;
input()
cin>>abc[i];
print()
for(int i=0;i<=9;i++)
sum=sum+abc[i];
cout<<"sum="<<sum;
};
main()
temp obj;
obj.input();
obj.print();
OUTPUT
PROGRAM 02:
Write a Program to print out even values stored in a linear array ABC having 10 elements?
#include<iostream>
class temp
private:
int abc[10];
public:
input()
for(int i=0;i<=9;i++)
cin>>abc[i];
print()
for(int i=0;i<=9;i++)
if (abc[i]%2==0);
cout<< "abc[i]"<<"\n";
};
main()
temp obj;
obj.input();
obj.print();
OUTPUT
PROGRAM 03:
Write a Program to add six values into elements at the end of array ‘Temp’ that has only 4
items stored in its first four elements?
#include<iostream>
class temp
private:
int abc[10];
public:
assign()
abc[0]=66;
abc[1]=72;
abc[2]=36;
abc[3]=78;
input()
for(int i=4;i<=9;i++)
cin>>abc[i];
print()
for(int i=0;i<=9;i++)
cout<<abc[i]<<endl;
}};
main()
temp obj;
obj.assign();
obj.input();
obj.print();
OUTPUT
PROGRAM 04:
Write a Program to insert a value M at location pos in array ABC having N elements?
#include<iostream>
class temp
private:
int abc[5];
public:
assign(int p[])
{
for (int i=0;i<=3;i++)
abc[i]=p[i];
for(int i=4;i>=loc;i--)
abc[i+1]=abc[i];
abc[loc]=val;
print(int n)
for(int i=0;i<=n;i++)
cout<<abc[i]<<endl;};
};
main()
temp obj;
obj.assign(a);
obj.print(3);
cin>>n;
if(pos>=5)
cout<<"invalid location";
return 0;
obj.insert(pos,n);
obj.print(4);
OUTPUT:
PROGRAM 05:
Write a Program to delete the value at location K of an array “country” having N
elements?
#include<iostream>
class temp
private:
int abc[5];
public:
assign(int p[])
abc[i]=p[i];
del(int loc)
for(int i=loc;i<=4;i++)
abc[i]=abc[i+1];
abc[4]=0;
print()
for(int i=0;i<=4;i++)
cout<<abc[i]<<endl;};
};
main()
temp obj;
obj.assign(a);
obj.print();
cin>>n;
cin>>pos;
if(pos>=5)
cout<<"invalid location";
return 0;
obj.del(pos-1);
obj.print();
OUTPUT:
PROGRAM 06:
#include <iostream>
#include <conio.h>
class stack
private:
int top;
int S [10];
public:
stack()
{
top=-1;
push(int n)
if(top==0)
cout<<"stack overflow";
return 0;
top++;
S[top]=n;
int pop()
int data;
if(top==-1)
cout<<"stack is empty";
return NULL;
data=S[top];
top--;
return data;
print()
{
if(top==-1)
cout<<"stack is empty";
return NULL;
cout<<S[i]<<endl;
};
int main()
stack obj;
while(opt!=3)
cout<<"1: push\n";
cout<<"2: pop\n";
cout<<"3: exit\n";
cin>>opt;
switch(opt)
case 1:
obj.push(val);
obj.print();
break;
case 2:
cout<<"value"<<obj.pop()<<"is popped";
obj.print();
break;
return 0;
OUTPUT:
PROGRAM 07:
#include <iostream>
class que
private:
int F, R;
int QA[10];
public:
que()
F=-1; R=-1;
insert(int n)
if(R>=9)
cout<<"que is full";
return 0;
else
R++;
QA[R]=n;
if (F==-1) F=0;
int del()
int data;
if(F==-1)
cout<<"que is empty";
return NULL;
}
data=QA[F];
if(F == R) F = R = -1;
else F++;
return data;
print()
if(F==-1)
cout<<"que is empty";
return NULL;
cout<<QA[i]<<endl;
};
main()
que obj;
while(opt!=3)
cout<<"1: insert\n";
cout<<"2: delete\n";
cout<<"3: exit\n";
cin>>opt;
switch(opt)
case 1:
cin>>val;
obj.insert(val);
obj.print();
break;
case 2:
cout<<"value"<<obj.del()<<"is popped";
obj.print();
break;
OUTPUT
PROGRAM 09:
Write a Program to delete items into priority queue consisting of a two-dimensional array
ABC with 3 rows and 10 columns?
#include<iostream>
#include<conio.h>
class que
private:
int F1,R1,F2,R2,F3,R3;
int QA[3][10];
public:
que()
{
F1=-1;R1=-1;
F2=-1;R2=-1;
F3=-1;R3=-1;
void insert(int,int);
void add(int,int&,int&,int);
del(int);
int remove(int&,int&,int);
void print(int);
void ppp(int&,int&,int);
};
main()
que obj;
int opt,val,pro;
while(opt!=3)
cout<<"insert item\n";
cout<<"delete item\n";
cout<<"exit\n";
cin>>opt;
switch(opt)
{
case 1:
cin>>val;
cout<<"enter priority";
cin>>pro;
obj.insert(val,pro);
obj.print(pro);
getch();
switch(p)
case 1:
add(n,R1,F1,0);
break;
case 2:
add(n,R2,F2,1);
break;
case 3:
add(n,R2,F2,2);
break;
if(R>=9)
getch();
return;
else
R++;
QA[i][R]=x;
if(F==-1)
F=0;
que::del(int p)
switch(p)
{
case 1:
remove(R1,F1,0);
break;
case 2:
remove(R2,F2,1);
break;
case 3:
remove(R2,F2,2);
break;
int data;
if(F==-1)
cout<<"Queue is empty";
getch();
return NULL;
data=QA[i][F];
if(F==R)
F=R=-1;
else F++;
return data;
void que::print(int p)
switch(p)
case 1:
ppp(R1,F1,0);
break;
case 2:
ppp(R2,F2,1);
break;
case 3:
ppp(R2,F2,2);
break;
if(F==-1)
cout<<"queue is empty";
return;
for(int c=F;c<=R;c++)
cout<<QA[i][c]<<"\t";
OUTPUT:
PROGRAM 10:
Write an algorithm to find a value ITEM from an array ABC consisting of N elements?
#include <iostream>
class seq
void search(int);
};
int main()
{
seq obj;
int item;
obj.input();
cin>>item;
obj.search(item);
void seq::input(void)
cin>> a[i];
void seq::search(int n)
int i, loc=-1;
i=0;
while(i<=9)
if(n==a[i])
loc=i+1;
cout<<"Data found in location = "<<loc;
break;
i++;
if(loc==-1)
OUTPUT:
PROGRAM 11:
public :
void input()
cin>> a[i];
void sort()
int t;
for(int u=4;u>=1;u--)
for(int i=0;i<u;i++)
if(a[i]>a[i+1])
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
void print()
cout<<a[i]<<endl;
};
int main()
bubble obj;
obj.input();
obj.sort();
obj.print();
OUTPUT:
PROGRAM 12:
class selection
public :
void input()
cin>>a[i];
void sort()
for(int u=0;u<4;u++)
min=a[u];
loc=u;
for(int i=u;i<=4;i++)
if(min>a[i])
{
min=a[i];
loc=i;
t=a[loc];
a[loc]=a[u];
a[u]=t;
cout<<endl;
void print()
cout<<a[i]<<endl;
};
int main()
selection obj;
obj.input();
obj.sort();
obj.print();
}
OUTPUT:
PROGRAM 13:
class insertion
{
private: int a[5];
public :
void input()
{
cout<<"Enter 5 values "<<endl;
for (int i=0; i<=4; i++)
cin>>a[i];
}
void sort()
{
int temp, i, c;
for(c=0;c<4;c++)
{
temp=a[c];
for(i=c; i>0 && temp< a[i-1];i--)
a[i]=a[i-1];
a[i]=temp;
}
}
void print()
{
cout<<"Sorted array "<<endl;
for(int i=0; i<=4; i++)
cout<<a[i]<<endl;
}
};
int main()
{
insertion obj;
obj.input();
obj.sort();
obj.print();
}
OUTPUT:
PROGRAM 14:
Write a program to sort an array AB consisting of N elements using merge sort?
#include <iostream>
using namespace std;
class merging
{
private: int ab[10], a[5], b[5], r, s;
public :
void input()
{
cout<<"Enter 10 values "<<endl;
for (int i=0; i<=9; i++)
cin>>ab[i];
}
void sort()
{
r=(9+0)/2;
s=r+1;
int i;
for(i=0;i<=r;i++)
a[i]=ab[i];
for(i=s;i<=9;i++)
b[i-s]=ab[i];
int temp, c;
for(c=0; c<=r; c++)
{
temp=a[c];
for(i=c; i>0 && temp<a[i-1]; i--)
a[i]=a[i-1];
a[i]=temp;
}
for(c=0; c<s; c++)
{
temp=b[c];
for(i=c; i>0 && temp<b[i-1]; i--)
b[i]=b[i-1];
b[i]=temp;
}
//merging
int L1, L2, L;
L1=0;
L2=0;
L=0;
while(L1<=r && L2<s)
{
if(a[L1] < b[L2])
{
ab[L]=a[L1];
L++;
L1++;
}
else
{
ab[L]=b[L2];
L++;
L2++;
}
}
if(L1 > r)
for(i=0; i<s-L2; i++)
ab[L+i]=b[L2+i];
else
for(i=0; i<r-L1; i++)
ab[L+i]=a[L1+i];
}
void print()
{
cout<<"Sorted array "<<endl;
for(int i=0; i<=9; i++)
cout<<ab[i]<<endl;
}
};
int main()
{
merging obj;
obj.input();
obj.sort();
obj.print();
}
OUTPUT:
PROGRAM 15:
Write a program to create a linked list consisting of ten nodes and enter values into the list
items?
#include <iostream>
struct node
int data;
node *link;
};
class list
public: list() {
start=NULL;
void append(int n)
if(start==NULL)
start=new node;
start-> data=n;
start-> link=NULL;
else
cur=start;
while(cur->link!=NULL)
cur=cur->link;
temp=new node;
temp->data=n;
temp->link=NULL;
cur->link=temp;
}
}
void print()
cur=start;
while(cur->link!=NULL)
cout<<cur->data<<endl;
cur=cur->link;
cout<<cur->data<<endl;
};
int main()
list obj;
int val, p;
cin>>val;
obj.append(val);
obj.print();
}
OUTPUT:
PROGRAM 17:
Write a Program to count the total number of items or nodes in a linked list?
#include <iostream>
struct node
int data;
node*link;
};
class list
private:
node*start,*cur,*temp;
public:
list()
start=NULL;
add_item(int n)
if(start==NULL)
start=new node;
start->data=n;
start->link=NULL;
else
cur=start;
while(cur->link!=NULL)
cur=cur->link;
temp=new node;
temp->data=n;
temp->link=NULL;
cur->link=temp;
int count()
int c=0;
cur=start;
while(cur->link!=NULL)
c++;
cur=cur->link;
return c+1;
};
main()
list obj;
int val;
cin>>val;
obj.add_item(val);
cout<<"total items="<<obj.count();
OUTPUT:
PROGRAM 18:
#include<iostream>
struct node
int data;
node*link;
};
class list
private:
node*start,*cur,*temp;
public:
list()
start=NULL;
add_item(int n)
node*temp;
if(start==NULL)
start=new node;
start->data=n;
start->link=NULL;
else
cur=start;
while(cur->link!=NULL)
cur=cur->link;
temp->data=n;
temp->link=NULL;
cur->link=temp;
del(int n)
cur=temp=start;
while(cur->link!=NULL)
if(cur->data==n)
temp->link=cur->link;
delete cur;
break;
temp=cur;
cur=cur->link;
print()
{
cout<<"data in link list\n";
cur=start;
while(cur->link!=NULL)
cout<<cur->data<<endl;
cur=cur->link;
cout<<cur->data<<endl;
};
main()
list obj;
int val;
for(int i=1;i<=5;i++)
cin>>val;
obj.add_item(val);
cin>>val;
obj.del(val);
obj.print();
}
OUTPUT:
PROGRAM 20:
#include<iostream>
struct node
node*p;
int data;
node*n;
};
class list
private:
node*start,*cur,*temp;
public:
list()
start=NULL;
add_item(int x)
if(start==NULL)
start=new node;
start->p=NULL;
start->data=x;
start->n=NULL;
else
node*prev;
cur=start;
while(cur->n!=NULL)
cur=cur->n;
prev=temp;
temp=new node;
temp->p=prev;
temp->data=x;
temp->n=NULL;
cur->n=temp;
print()
cur=start;
while(cur->n!=NULL)
cout<<cur->data<<endl;
cur=cur->n;
cout<<cur->data<<endl;
};
main()
{
list obj;
int val;
cin>>val;
obj.add_item(val);
obj.print();
OUTPUT:
PROGRAM 21:
#include<iostream>
#include<conio.h>
struct node{
int data;
node *left;
node *right;
};
class tree
private:
node *start,*cur,*temp;
public:
tree()
start=NULL;
};
int main()
{
tree obj;
int val,s,opt,c=1;
while(c)
cout<<"3:Exit \n\n";
cin>>opt;
switch(opt)
case 1:
cin>>val;
obj.create(val);
break;
case 2:
cin>>s;
obj.search(s);
break;
case 3:
c=0;
break;
if(start==NULL)
start=new node;
start->data=x;
start->left=NULL;
start->right=NULL;
else{
node *parent;
cur=start;
while(cur!=NULL)
if(cur->data==x)
return;
if(cur->data<x)
{parent=cur;cur=cur->right;
else{
parent=cur;cur=cur->left;
temp=new node;
temp->data=x;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
parent=temp;
else if(parent==NULL)
parent->left=temp;
else
parent->right=temp;
void tree::search(int x)
if(start==NULL)
cout<<"tree is empty";
getch();
return;
}
else{
cur=start;
while(cur!=NULL)
if(cur->data==x)
cout<<"data found";
getch();
return;
if(cur->data<x)
cur=cur->right;
else
cur=cur->left;
if(cur==NULL)
getch();
}
OUTPUT:
PROGRAM 22:
#include<iostream>
struct node
int data;
node*left;
node*right;
};
class tree
private:
public:
tree()
start = NULL;
}
void create(int x);
};
main()
tree obj;
while(c)
switch(opt)
case 1:
cin>>val;
obj.create(val);
break;
case 2:
cin>>s;
obj.search(s);
break;
void tree::create(int x)
if(start==NULL)
start->data = x;
start->left = NULL;
start->right = NULL;
else
node*parent;
cur=start;
while(cur!=NULL)
if(cur->data == x)
{
return;
if(cur->data < x)
{parent=cur; cur=cur->right;}
else
temp->data = x;
temp->left = NULL;
temp->right = NULL;
if (parent ==NULL)
parent=temp;
else if(parent->data>x)
parent->left=temp;
else
parent->right = temp;
void tree::search(int x)
if(start == NULL)
{
cout<<"Tree is empty";
return;
else
cur=start;
while(cur!=NULL)
if(cur->data == x)
cout<<"data found";
if(cur->data < x)
cur=cur->right;
else
cur=cur->left;
if(cur == NULL)
}
OUTPUT:
PROGRAM 23:
Write a Program for deleting a leaf node from a binary search tree?
#include<iostream>
#include<conio.h>
struct node
int data;
node *left;
node *right;
};
class tree
private:
public:
tree()
start = NULL;
void create(int);
void inorder(void);
void inord(node*s);
};
main()
tree obj;
int val,p;
cin>>val;
obj.create (val);
obj.inorder();
getch();
void tree::inorder()
{
inord(start);
void tree::inord(node*s)
if(s!=NULL)
inord(s->left);
cout<<s->data<<"\t";
inord(s->right);
void tree::create(int x)
if(start==NULL)
start ->data = x;
start->left= NULL;
start->right= NULL;
else
{
node* parent;
cur = start;
while(cur!=NULL)
if(cur->data== x)
if(x>cur->data)
{parent=cur; cur=cur->right;}
else
temp->data =x;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
parent = temp;
else if (parent->data>x)
parent->left=temp;
else
parent->right=temp;
}
OUTPUT:
PROGRAM 24:
#include<iostream>
#include<conio.h>
struct node
int data;
node *left;
node *right;
};
class tree
private:
int top;
public:
tree()
start = NULL;
void preorder(void);
};
int main()
tree obj;
int val,p;
cin>>val;
obj.create (val);
obj.preorder();
getch();
void tree::preorder()
int top = 0;
cout<<"\nPrint data in preorder\n\n";
node *stack[10];
stack[top]=start;
while(top>=0)
cur = stack[top];
top--;
while(cur!=NULL)
cout<<cur->data<<endl;
if(cur->right!=NULL)
top++;
stack[top]= cur->right;
void tree::create(int x)
if(start==NULL)
start ->data = x;
start->left= NULL;
start->right= NULL;
else
node* parent;
cur = start;
while(cur!=NULL)
if(cur->data== x)
if(x>cur->data)
parent=cur; cur=cur->right;}
else
temp->data =x;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
parent = temp;
else if (parent->data>x)
parent->left=temp;
else
parent->right=temp;
OUTPUT:
PROGRAM 25:
#include<conio.h>
struct node
int data;
node *left;
node *right;
};
class tree
private:
int top;
public:
tree()
start = NULL;
void create(int);
void inorder(void);
void inord(node*s);
};
main()
{
tree obj;
int val,p;
cin>>val;
obj.create (val);
obj.inorder();
getch();
void tree::inorder()
inord(start);
void tree::inord(node*s)
if(s!=NULL)
inord(s->left);
cout<<s->data<<"\t";
inord(s->right);
}
void tree::create(int x)
if(start==NULL)
start ->data = x;
start->left= NULL;
start->right= NULL;
else
node* parent;
cur = start;
while(cur!=NULL)
if(cur->data== x)
if(x>cur->data)
{parent=cur; cur=cur->right;}
else
temp->data =x;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
parent = temp;
else if (parent->data>x)
parent->left=temp;
else
parent->right=temp;
OUTPUT:
PROGRAM 25:
#include<iostream>
#include<conio.h>
using namespace std;
struct node
int data;
node *left;
node *right;
};
class tree
private:
int top;
public:
tree()
start = NULL;
void create(int);
void postorder(void);
void post(node*s);
};
int main()
tree obj;
int val,p;
cout<<"enter ten value \n";
cin>>val;
obj.create (val);
obj.postorder();
getch();
void tree::postorder()
post(start);
void tree::post(node*s)
if(s!=NULL)
post(s->left);
post(s->right);
cout<<s->data<<"\t";
void tree::create(int x)
{
if(start==NULL)
start ->data = x;
start->left= NULL;
start->right= NULL;
else
node* parent;
cur = start;
while (cur!=NULL)
if(cur->data== x)
if(x>cur->data)
{parent=cur; cur=cur->right;}
else
temp->data =x;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
parent = temp;
else if (parent->data>x)
parent->left=temp;
else
parent->right=temp;
OUTPUT: