Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C... Build a linked list of 100 nodes, each containing an integer between 0 a

ID: 3693635 • Letter: I

Question

In C...

Build a linked list of 100 nodes, each containing an integer between 0 and 99. Each node will contain a unique integer and all integers between 0 and 99 will be included in the list. Search the list for the node that contains a particular value entered by the user from the keyboard, and returns the location in the linked list where the sought number (the key) was found. Assume the list is unordered (which of course, is not the case here), and search the entire linked list until the node containing the desired number is found. For output, print out a line stating that the value sought was found the nth node. For example:

Please enter the number to be sought in the list: >> 15

The number 15 was found in node #15

Note: that the list must be implemented as a linked list and NOT as an array!

I have this but for every and any input i recieve the output "Number not found", what is wrong with the code?

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

struct node{
   int data;
   struct node *next;      
};

void insert(struct node **start, int item){
   struct node *newNode = (struct node *)malloc(sizeof(struct node *));
   newNode->data = item;
   newNode->next = NULL;
   if(*start == NULL){
       *start = newNode;
       return;
   }
   struct node *temp = *start;
   while(temp->next != NULL){
       temp = temp->next;
   }
   temp->next = newNode;
}

int main(){
   srand(time(0));
   struct node *start = NULL;
   int i;
   for(i = 1; i <= 10; i++) insert(&start, rand() % 100);
   printf("Please enter the number to be sought in the list: >> ");
   int find;
   scanf("%d", &find);
   int index = -1;
   struct node *temp = start;
   i = 0;
   while(temp->next != NULL){
       i++;
       if(temp->data == find){
           index = i;
           break;
       }
       temp = temp->next;
   }
   if(index == -1){
       printf("Number not found ");
   }
   else printf("The number %d was found in node #%d ", find, index);
}

Explanation / Answer

//Your code was not working because generating hundered random does not mean they will hundred distinct number from o to 99

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
struct node{
int data;
struct node *next;
};
void insert(struct node **start, int item){
struct node *newNode = (struct node *)malloc(sizeof(struct node *));
newNode->data = item;
newNode->next = NULL;
if(*start == NULL){
*start = newNode;
return;
}
struct node *temp = *start;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
int getrandom()
{
srand(time(0));
return rand() % 100;
}
int main(){

struct node *start = NULL;
int i,a[100],rt,j;
for(i = 0; i <100; i++)a[i]=i;
//shuffling the array
for(i = 0; i <100; i++)
{
rt = getrandom();
j = a[i];
a[i] = a[rt];
a[rt]=j;
}
for(i = 0; i < 100; i++)
{
insert(&start,a[i]);
}
printf("Please enter the number to be sought in the list: >> ");
int find;
scanf("%d", &find);
int index = -1;
struct node *temp = start;
i = 0;
while(temp->next != NULL){
printf("%d ",temp->data);
i++;
if(temp->data == find){
index = i;
break;
}
temp = temp->next;
}
printf(" ");
if(index == -1){
printf("Number not found ");
}
else printf("The number %d was found in node #%d ", find, index);
}