Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

ASSIGNMENT NO 2

SUBMITTED TO:

SIR RAZA

(ALGORITHM & DATA STRUCTURE)


SUBMITTED BY:

Hub-e-Ali
SP14-BET-0
Question 1

Implementing Linear Search in Linked List Data Structure


#include<iostream>

#include<conio.h>

#include<stdio.h>

using namespace std;

struct node

int data;

node *next;

};

class lList

node *head,*tail;

public:

lList()

head = NULL;

tail = NULL;

void createNode(int value)

node *temp = new node;

temp->data = value;

temp->next = NULL;

if (head == NULL)
{

head = temp;

tail = temp;

temp = NULL;

} else

tail->next = temp;

tail = temp;

void display()

node *temp = new node;

temp = head;

while (temp != NULL)

cout << temp->data << "\t";

temp = temp->next;

void insertStart(int value)

node *temp = new node;

temp->data = value;

temp->next = head;

head = temp;

void insertPosition(int pos, int value)

{
node *pre = new node;

node *cur = new node;

node *temp = new node;

cur = head;

for (int i = 1; i < pos; i++)

pre = cur;

temp->data = value;

pre->next = temp;

temp->next = cur;

void insertLast(int value)

node *temp = new node;

temp->data = value;

temp->next = NULL;

if (head == NULL) {

head = temp;

tail = temp;

temp = NULL;

} else {

tail->next = temp;

tail = temp;

}
void deleteFirst()

node *temp = new node;

temp = head;

head = head->next;

delete temp;

void deleteLast()

node *current = new node;

node *previous = new node;

current = head;

while (current->next != NULL) {

previous = current;

current = current->next;

tail = previous;

previous->next = NULL;

delete current;

void deletePosition(int pos)

node *current = new node;

node *previous = new node;

current = head;

for (int i = 1; i < pos; i++)

{
previous = current;

current = current->next;

previous->next = current->next;

void search(int value)

int ctr=1;

node *temp=new node;

temp=head;

while(temp!=NULL)

if(value==temp->data)

cout<<"element found on "<<ctr<<" iterations"<<endl;

ctr++;

break;

else

cout<<"element not found on "<<ctr<<" iteration"<<endl;

ctr++;

temp=temp->next;

};

main()
{

lList obj;

int search1,sagn;

cout<<endl<<"Please enter a number you want to search from the list"<<endl<<endl;

cout<<"LIST OF NUMBERS: ";

obj.createNode(25);

obj.createNode(50);

obj.createNode(90);

obj.display();

cout<<endl<<endl<<"Please Enter:"<<endl;

cin>>search1;

cout<<endl<<"Number entered is : "<<search1<<endl;

obj.search(search1);

cout<<"Do you want to search again enter 0 for again or 1 for exit"<<endl;

switch(sagn){

case 0:{

obj.search(search1);

break;

case 1:{

//exit(0);

break;

getch();

Question 2

Implement Binary Search using iteration


#include<iostream>
#include<conio.h>

#include<stdio.h>

using namespace std;

int binarySearch(int arr[], int std, int end, int x)

while (std <= end)

int mid = std + (end-std)/2;

if (arr[mid] == x)

return mid;

if (arr[mid] < x)

std = mid + 1;

else

end = mid - 1;

return -1;

main()

int arr[5];

cout<<"enter element in array";

for(int i=0;i<5;i++)

cin>>arr[i];

}
int n = sizeof(arr)/ sizeof(arr[0]);

int x;

cout<<"enter a number to search";

cin>>x;

int result = binarySearch(arr, 0, n-1, x);

if(result == -1)

cout<<"Element is not present in array";

else

cout<<"Element is present at index "<< result;

getch();

You might also like