Linked list problem

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.

insert code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
            if (head == NULL) {
                head = node;
            } else 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;
}


retrieve code:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool GroceryList::retrieve(GroceryItem &thing, int spot) {
    bool gotIt = false;
    
    if (spot >= 1 && spot <= length()) {
        GroceryNode *temp = head;
        for (int move = 1; move <= spot; move++) {
            temp = temp->next; //determine spot of requested item
        }
        thing.setItem(temp->item.getName(), temp->item.getPrice());
        gotIt = true;
    }
    return gotIt;
}


I have all my source code if u need any more thanks.
I got some help from my teacher and it works great now and it was somthing stupid. i had to make all the <= or >= into just < or > and change this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
            if (head == NULL) {
                head = node;
            } else 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;
}


to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
            } else if (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;
}


Thanks everyone
The source code is too big to share here but it works fine now if you want the source code let me know.
Topic archived. No new replies allowed.