Skip to content

Commit

Permalink
searching_single_linkedlist
Browse files Browse the repository at this point in the history
  • Loading branch information
PreetiBaskey committed Oct 20, 2021
1 parent 98eb2d6 commit c5f24fe
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
42 changes: 42 additions & 0 deletions algorithms/searching and sorting/binary search/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<iostream>
#include<conio.h>
using namespace std;

int BinarySearch(int a[],int n,int data){
int l,r;
int mid=0;
l = 0;
r = n -1;
while(l < r){
mid = (l + r)/2;
if(data == a[mid])
return mid;
else if(data < a[mid])
r = mid - 1;
else
l = mid + 1;
}
return -1;
}

int main(){
int arraySize, data, result;
cout<<"\nEnter Array Size :- ";
cin>>arraySize;
int a[arraySize];
cout<<"\nEnter Array Elements :- ";
for(int i = 0; i < arraySize; i++){
cin>>a[i];
}
cout<<"\nYour Array elements are :- \n";
for(int i = 0; i < arraySize; i++){
cout<<" "<<a[i]<<" ";
}
cout<<"\n\n";
cout<<"Enter Data You want to Search :- ";
cin>>data;
result = BinarySearch(a,arraySize,data);
cout<<"\n"<<data<<" is found at position "<<result;
getch();
}

31 changes: 31 additions & 0 deletions algorithms/searching and sorting/linear search/LinearSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
#include<conio.h>
using namespace std;

int main(){
int arraySize, data,j;
cout<<"\nEnter Array Size :- ";
cin>>arraySize;
int a[arraySize];
cout<<"\nEnter Array Elements :- ";
for(int i = 0; i < arraySize; i++){
cin>>a[i];
}
cout<<"\nYour Array elements are :- \n";
for(int i = 0; i < arraySize; i++){
cout<<" "<<a[i]<<" ";
}
cout<<"\n\n";
cout<<"Enter Data You want to Search :- ";
cin>>data;
for(j = 0; j < arraySize; j++){
if(a[j] == data){
cout<<"\nElement found at index :- "<<j;
break;
}
}
if(j == arraySize){
printf("Element is not found");
}
getch();
}
114 changes: 114 additions & 0 deletions data structures/linked lists/SinglyLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node *link;
};
struct node* root = NULL;
void append()
{
int ele;
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
cout<<"Enter element :- ";
cin>>ele;
temp -> data = ele;
temp ->link = NULL;
if(root == NULL)
{
root = temp;
}
else
{
struct node *p;
p = root;
while(p->link != NULL)
{
p = p -> link;
}
p -> link = temp;
}
}
void length()
{
int count = 0;
struct node *temp;
temp = root;
while(temp != NULL)
{
count = count + 1;
temp = temp -> link;
}
cout<<"Length is :- "<<count;
}
void inFirst()
{
int ele;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
cout<<"Enter element :- ";
cin>>ele;
temp -> data = ele;
temp -> link = root;
root = temp;
}
void display()
{
struct node* p;
p = root;
if(root == NULL)
{
cout<<"No elements";
}
else
{
while(p != NULL)
{
cout<<p -> data<<" ";
p = p -> link;
}
}
}
void atMiddle()
{
int data, findEle;
struct node *temp, *p, *q;;
temp = (struct node*)malloc(sizeof(struct node));
cout<<"Enter data :- ";
cin>>data;
temp -> data = data;
p = root;
cout<<"Enter element after which you want to insert :- ";
cin>>findEle;
while(p -> data != findEle)
{
p = p -> link;
}
q = p -> link;
temp -> link = q;
p -> link = temp;
cout<<data<<" inserted successfully\n";
}
int main()
{
int choice;
do
{
cout<<"\n1.Append\n2.Length\n3.In First\n4.Display\n5.At Middle\n6.Exit\n";
cout<<"Enter your choice :- ";
cin>>choice;
switch(choice)
{
case 1: append(); break;
case 2: length(); break;
case 3: inFirst(); break;
case 4: display(); break;
case 5: atMiddle(); break;
case 6: exit(0);
default: cout<<"Wrong input";
}
}while(choice != 6);
getch();
}

0 comments on commit c5f24fe

Please sign in to comment.