Lab 5
Lab 5
Lab Report#5
Class: BSSE-3A(Morning)
Roll No:11982
CODE:
#include<iostream>
int main(){
int count=0;
int a[n];
for(int i=0;i<n;i++){ //We use a loop which insert values in our Array.
a[i]=(i+1);
int num;
int s=0;
int e=(n-1); //we set end point, so the system search up to this point.
while(s<e){ //we use a loop to check whether the inserted number is greater than or less
than inserted number.
break;
else if(num>a[mid]){ //if the number to be searched is greater than mid nmber
than we changes the starting point.
if(num<a[mid]){ //f the number to be searched is less than mid nmber than we
changes the ending point
e=mid-1;
mid=(s+e)/2;
count++;
if(num!=a[mid]){//if num is not equal to mid number system generate output of not
found.
cout<<"Not Found";
}}
OUTPUT:
Q2. Wirte a program to insert number at any position of a list using link list.
CODE:
#include<iostream>
int num;
node* next_add;
};
node* head;
node* tail;
void Fetch(int x){ //this functions get values from main function and create a list.
node* temp=new node();// a temporary constructor of node type is created, which has
address of first number.
temp->num=x;
temp->next_add=NULL;
if(head==NULL){ //we put a condition that if head is null than it means this value is our
first value of the list.
head=temp;
tail=temp;
else{ //if head is not null the statements in else are executed.
tail->next_add=temp;
tail=temp;
}}
void Insert(int x, int p,int l){// this loop take value and position from main program to input that
value in list as per required position.
if(p>l){ //if position is greater than 1 than these if statements are executed.
temp2->num=x;
if(p==0){ //if position is equals to 0 than this node is inserted at starting point.
temp2->next_add=head;
head=temp2;
node* temp3=temp2;
temp2=head;
for(int i=1;i<=(p-1);i++){
temp2=temp2->next_add;
temp3->next_add=temp2->next_add;
temp2->next_add=temp3;
}}}
temp1=temp1->next_add;
}}
int main(){
head==NULL;
tail==NULL;
int n,value,pos;
int a[100];
cout<<"Data==>>",cin>>value;
Fetch(value);
show();
cout<<"\nEnter the number and position where you want to insert your number:\n";
cout<<"Data==>>",cin>>value;
cout<<"Position==>>",cin>>pos;
Insert(value,pos,n);
show();
}
OUTPUT: