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
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
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); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started