Home > Blockchain >  This code doesn't return output. I need help please
This code doesn't return output. I need help please

Time:12-14

I write a code that will ask for 10 integer input and search a number using binary search in linked list but my code didn't return value. The code shown bellow:

#include<stdio.h>
#include<stdlib.h>

//Node of a Linked List
struct Node{
int data;
struct Node *next;
};

//function to create a new node
struct Node* newNode(int key){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}

//function to insert a node at the beginning of the Linked List
void insertAtBeginning(struct Node **head, int key){
struct Node* temp = newNode(key);
temp->next = *head;
*head = temp;
}

//function to search an element in a Linked List using Binary Search
int binarySearch(struct Node* head, int key){
if(head == NULL) 

return 0;

int l = 0, r = 0;

//find the length of the Linked List
struct Node* temp = head;
while(temp != NULL){
    r  ;
    temp = temp->next;
}

//perform binary search on the Linked List
while(l<=r){
    int mid = l   (r-l)/2;
    temp = head;
    for(int i=0;i<mid;i  ){
        temp = temp->next;
    }
    if(temp->data == key) return 1;
    else if(temp->data > key) r = mid - 1;
    else l = mid   1;
}
return l;
}

int main(){
struct Node* head = NULL;

int key;
printf("Enter 10 integers: \n");
for(int i=0;i<10;i  ){
    scanf("%d", &key);
    insertAtBeginning(&head, key);
}

printf("Enter the element to be searched: ");
scanf("%d", &key);

if(binarySearch(head, key)) 
printf("Element Found\n");
else printf("Element not Found\n");

return 0;
}

My code should return "Element found" or "Element not Found"

CodePudding user response:

In binary searching fonction, r should be the max index ; in the code it is the length of the list, which is the max 1 since indexing start at 0.

Also, the function should return 0 when nothing is found: here it returns the min index l.

//function to search an element in a Linked List using Binary Search
int binarySearch(struct Node* head, int key){
    if(head == NULL) 
        return 0;

    int l = 0, r = 0;

    //find the length of the Linked List
    struct Node* temp = head;
    while(temp != NULL){
        r  ;
        temp = temp->next;
    }
    r--; // the index of the last element is length - 1

    //perform binary search on the Linked List
    while(l<=r){
        int mid = l   (r-l)/2
        temp = head;
        for(int i=0;i<mid;i  ){
            temp = temp->next;
        }
        if(temp->data == key) return 1;
        else if(temp->data > key) r = mid - 1;
        else l = mid   1;
    }
    return 0; // returns false when nothing found
}

Note that to perform a binary search, the data searched have to be sorted. This is not the case here except if the 10 numbers are already sorted on input.

About performance, binary searching in a linked list is a waste of time/cpu/energy: the list will be scanned many times since there's no direct access to an element.

  • Related