Mix and incorporate these code with binary search engine BST If root == NULL return NULL; If number == root->data return root->data; If number < root->data return search(root->left) If number > root->data return search(root->right) #include //Represent a node of the singly linked list struct node{ int data; struct node *next; }; //Represent the head and tail of the singly linked list struct node *head, *tail = NULL; //addNode() will add a new node to the list void addNode(int data) { //Create a new node struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->next = NULL; //Checks if the list is empty if(head == NULL) { //If list is empty, both head and tail will point to new node head = newNode; tail = newNode; } else { //newNode will be added after tail such that tail's next will point to newNode tail->next = newNode; //newNode will become new tail of the list tail = newNode; } } //removeDuplicate() will remove duplicate nodes from the list void removeDuplicate() { //Node current will point to head struct node *current = head, *index = NULL, *temp = NULL; if(head == NULL) { return; } else { while(current != NULL){ //Node temp will point to previous node to index. temp = current; //Index will point to node next to current index = current->next; while(index != NULL) { //If current node's data is equal to index node's data if(current->data == index->data) { //Here, index node is pointing to the node which is duplicate of current node //Skips the duplicate node by pointing to next node temp->next = index->next; } else { //Temp will point to previous node of index. temp = index; } index = index->next; } current = current->next; } } } //display() will display all the nodes present in the list void display() { //Node current will point to head struct node *current = head; if(head == NULL) { printf("List is empty \n"); return; } while(current != NULL) { //Prints each node by incrementing pointer printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { //Adds data to the list addNode(1); addNode(2); addNode(3); addNode(2); addNode(2); addNode(4); addNode(1); printf("Originals list: \n"); display(); //Removes duplicate nodes removeDuplicate(); printf("List after removing duplicates: \n"); display(); return 0; }
Mix and incorporate these code with binary search engine
BST
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
#include <stdio.h>
//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
//addNode() will add a new node to the list
void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
//Checks if the list is empty
if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}
//removeDuplicate() will remove duplicate nodes from the list
void removeDuplicate() {
//Node current will point to head
struct node *current = head, *index = NULL, *temp = NULL;
if(head == NULL) {
return;
}
else {
while(current != NULL){
//Node temp will point to previous node to index.
temp = current;
//Index will point to node next to current
index = current->next;
while(index != NULL) {
//If current node's data is equal to index node's data
if(current->data == index->data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp->next = index->next;
}
else {
//Temp will point to previous node of index.
temp = index;
}
index = index->next;
}
current = current->next;
}
}
}
//display() will display all the nodes present in the list
void display() {
//Node current will point to head
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
//Adds data to the list
addNode(1);
addNode(2);
addNode(3);
addNode(2);
addNode(2);
addNode(4);
addNode(1);
printf("Originals list: \n");
display();
//Removes duplicate nodes
removeDuplicate();
printf("List after removing duplicates: \n");
display();
return 0;
}
![15 node* binarySearch (int searchItem)
16 {
// first is pointing to beginning of node list
int first = 0;
17
18
19
// points to end
int last = 5;
20
21
22
// points to middle
int mid;
23
24
25
26
bool found = false;
27
// this will compare and set found when it finds it
while (first <= last && !found)
{
mid =
28
29
30
(first + last) / 2;
31
32
// this = your node list
if (this->at(mid)
found = true;
else if (this->at(mid) > searchItem)
last = mid 1;
33
34
searchItem)
==
35
36
37
else
first mid + 1;
38
39
40
}
41
if (found)
return mid;
42
43
44
else
45
return -1;
46 }](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F157e66f5-e794-4af2-988e-1885b818a80a%2F3e6b8dc1-d0c1-4d31-8309-3c5337f8eba5%2F509ax0e_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)