This is my first linked list and it is a grocery list now several of the parts are working fine but i would like some tips on my insert method cause i am not sure it works, it adds things but i don't think it puts them in the spot u tell it to. Also my retrieve method gives me a segmentation error and i can't find it.
bool GroceryList::insert(GroceryItem item, int spot) {
bool sucess = false;
if (spot >= 1 && spot <= length() + 1) {
GroceryNode *node = new GroceryNode(); // make node if valid spot
if (node != NULL) {
sucess = true;
node->item.setItem(item.getName(), item.getPrice()); // set item value in node
} elseif (spot == 1) { // here is what i fixed else if (spot = 1) to if (spot == 1)
node->next = head;
head = node; // place node in list
} else {
GroceryNode *prev = head;
for (int i = 0; i < (spot - 2); i++) {
prev = prev->next;
}
node ->next = prev->next;
prev -> next = node;
}
}
}
return sucess;
}