Linked lists problem

I am getting a Signal received: SIGSEGV (?) with sigcode ? (?)
From process: ?
For program grocerylist, pid 7,286 error from netbeans

here is the funcution that it happens in

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
26
27
28
/**
 * Inserts a new item into the Grocery list.
 * @param thing, being inserted.
 * @return bool, weather or not thing was inserted.
 */
bool GroceryList::insert(GroceryItem item, int spot) {
    bool sucess = false;
    if (spot >= 1 && spot <= length() + 1) {
        GroceryNode *node = new GroceryNode();  // TODO:: something wrong here
        if (node != NULL) {                     // this is where i get seg error
            sucess = true;
            node->item = item;
            if (head == NULL) {
                head = node;
            } else if (spot = 1) {
                node->next = head;
                head = node;
            } else {
                GroceryNode *prev = head;
                for (int i = 0; i <= spot - 2; i++) {
                    prev = prev->next;
                }
                node ->next = prev->next;
                prev -> next = node;
            }
        }
    }
}


and here is where i call the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Adds items to the list
 * @param GrocieList, the list things are added too.
 */
void insertItem(GroceryList &list) {
    cout << "Enter Item: ";
    string name;
    cin >> name;
    cout << endl << "Enter Price: ";
    double price;
    cin >> price;
    GroceryItem item(name, price);
    cout << endl << "Enter Spot: ";
    int spot;
    cin >> spot;
    bool res = list.insert(item, spot);
    // was it inserted //
    if (res) {
        cout << "Got it";
    } else {
        cout << "Sorry, no memory";
    }
}


if i need to post any more of my code let me know
any help would be great thanks
How is class GroceryNode defined? and anything in its constructor?
Just a pointer to NULL
Too much source code to share but if you want it let me know it works fine now.
Topic archived. No new replies allowed.