Del Data Structure With C++ Part3
Del Data Structure With C++ Part3
linear search (we have to visit to each of the elements and we then have to compare
the element with searched element)
Binary search(first of all arrage all the elements in the ascending order and then
perform search that is binary search)
steps to perform binary search
#include<iostream>
#include<conio.h>
using namespace std
int binarySearch(int arr[],int key, int low, int high)
{
while(low<=high)
{
int mid=(low+high)/2;
if(arr[mid]==key)
return mid;
if(arr[mid]<key)
low=mid+1;
else
high=mid-1
}
return -1
}
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int size=sizeof(arr);
int result=binarySearch(arr,5,0,size-1);
if(result==-1)
{
cout<<"element not found";
}
else
{
cout<<element found at<<result;
}
return 0;
}
LinkedList?
it is collection of data and the data is stored in a node form where each node
contains data and address of next node
steps
1 create a structure
struct node
{
int data;
struct node *next;
}
Singly LinkedList
one=malloc(sizeof(struct node));
two=malloc(sizeof(struct node));
three=malloc(sizeof(struct node));
one->data=1;
two->data=2;
three->data=3;
one->next=two;
two->next->three;
three->next=NULL;
head=one;
2. doubly linkedList----we add a pointer to the previous node in a doubly-
LinkedList. that why we can go in both the directions the backword direction or in
the forward direction.
struct node
{
int data;
struct node *next;
struct node *prev;
}
// we have to allocate the memory to each of the nodes of doubly linked list;
one=malloc(sizeof(struct node));
two=malloc(sizeof(struct node));
three=malloc(sizeof(struct node));
one->data=1;
two->data=2;
three->data=3;
one->next=two;
one->prev=NULL;
two->next=three;
two->prev=one;
three->next=NULL;
three->prev=two;
//now save the address of the first node in the head node;
head=one;
//allocating the memory to each of the nodes in the circular singly linkedList
one=malloc(sizeof(struct node));
two=malloc(sizeof(struct node));
three=malloc(sizeof(struct node));
//assign the values to each of the nodes in the circular singly linkedList
one->data=1;
two->data=2;
three->data=3;
one->next=two;
two->next=three;
three->next=one;
assignment
Implement the circular LinkedList in Doubly LinkedList fashion
//create a structure
struct node
{
int data;
struct node *next;
struct node * prev;
}
one=malloc(sizeof(struct node));
two=malloc(sizeof(struct node));
three=malloc(sizeof(struct node));
//assign the values
one->data=1;
two->data=2;
three->data=3;
one->next=two;
one->prev=NULL;
two->next=three;
three->prev=two;
three->next=one;
head=one;
operations on LinkedList
1. create a new node and allocate the memory to the new node;
2. store the data
3. visit to last node
4. change the next of last node to the newly created node
newNode->data-4;
newNode->next=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNode;
for(int i=2;i<position;i++)
{
if(temp->next!=NULL)
{
temp=temp->next;
}
}
newNode->next=temp->next;
temp->next=newNode;
head=head->next;
b. delete from the last
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
assignment
sorting
type of sorting
1. bubble sort
steps
1. 12,1,3,2,5,9,4
iterations=size-1
Selection sort---->whatever the array is, we have to select the smallest element
from the unsorted array then we have to put that element at the beginning
Tree-----
graph----