0% found this document useful (0 votes)
36 views7 pages

Lab 5

The document contains code for performing binary search on an array and inserting a number into a linked list at a specified position. The binary search code uses a recursive approach to search for a target number in a sorted array. It calculates the mid-point at each step and compares the target to the mid element to update the search range. The linked list insertion code defines a node structure and uses it to create a linked list by passing values to a Fetch function. The Insert function takes a value and position as arguments and inserts the new node at that position by updating references between nodes. The show function prints out the final list by traversing from head to tail.

Uploaded by

Umar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

Lab 5

The document contains code for performing binary search on an array and inserting a number into a linked list at a specified position. The binary search code uses a recursive approach to search for a target number in a sorted array. It calculates the mid-point at each step and compares the target to the mid element to update the search range. The linked list insertion code defines a node structure and uses it to create a linked list by passing values to a Fetch function. The Insert function takes a value and position as arguments and inserts the new node at that position by updating references between nodes. The show function prints out the final list by traversing from head to tail.

Uploaded by

Umar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Modern Languages

Subject: Data Structure and Algorithms

Lab Report#5

Submitted to: Aqib Adeel

Submitted by: Umar Ali

Class: BSSE-3A(Morning)

Roll No:11982

Date: October 3,2019


Q1. Write a program to perform Binary search in an Array.

CODE:

#include<iostream>

using namespace std;

int main(){

int count=0;

const int n=100;

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;

cout<<"Enter a number yoou want to find between 1 and 100:\n",cin>>num;

int s=0;

int e=(n-1); //we set end point, so the system search up to this point.

int mid=(s+e)/2; //than we set mid point of the array.

while(s<e){ //we use a loop to check whether the inserted number is greater than or less
than inserted number.

if(num==a[mid]){ //than we put a condition that if the searched value is equal to


mid value than it generates an output that required number is found.

cout<<"Number is found at "<<a[mid]<<"\n";

break;

else if(num>a[mid]){ //if the number to be searched is greater than mid nmber
than we changes the starting point.

s=(mid+1); //now our starting point is changed.


}

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++;

cout<<"Number of counts are:"<<count<<endl;

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>

using namespace std;

struct node{ //we create a structure named node.

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.

cout<<"Invalid entry....Please try between 0 and "<<l<<" next time:\n";

else{ //otherwise this else is executed.

node* temp2=new node();.//here we create a temporary pointer of node type and


assign it the value of node to be inserted in the list.

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;

else{ //otherwise it is inserted at required position entered by the user.

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;

}}}

void show(){ //this function displays the list we created.

node* temp1; //again we initialize a temporary pointer of node type.

temp1=head; //than assign head value to it.

cout<<"Your list is:\n";

while(temp1!=NULL){ //this loop is executed until temp1 is not equal to NULL.


cout<<temp1->num<<" ";

temp1=temp1->next_add;

}}

int main(){

head==NULL;

tail==NULL;

int n,value,pos;

cout<<"Enter the length of your list:",cin>>n;

int a[100];

cout<<"Enter data in your list:\n";

for(int i=0;i<n;i++){//this loop get values from the user.

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:

You might also like