So I am tryng to figure out Linked Lists.
Explanation of code:
gen_arr function generated random array
generatePP generated linked list with elements from array
del deletes head after executing main function before return
search is looking for elemenbt 0 that doesnt exists in linked list so the function goes through every element in list
If you guys can help me out and how to implement search function. Thank you guys :)
Line 26: n is uninitialized. You should move this line to right after line 29.
You don't need to if inside generatePP. In all cases, you can just execute
1 2
temp->next = *head;
*head = temp;
search() should take value you're searching for as a parameter: void search(struct node *head, int value)
Some suggestions:
- instead of passing the address of head to generatePP and del, pass a reference to it.
- Use for loops to walk the list. This will help separate the "walking the list" code from the "doing something with each item" code. For example:
1 2 3 4 5 6 7 8 9 10 11 12
void
search(struct node *head)
{
int seek = 0;
for (; head; head = head->next) {
if (head->data == seek) {
cout << "Pronadeno" << endl;
return;
}
}
cout << "Element ne postoji" << endl;
}