0

I am trying to print the index of a multi linked list. Each node has two elements- a warehouse number and a tool number. I am printing all the tools in each warehouse. I am having a problem with iterating correctly through the list.

I am not getting the correct values and am having trouble finding the problem in my method.

struct Node
{
    int WarehouseNumber;
    int ToolNumber;
    struct Node *n;
}

void showWarehouses()
{
    int tempvalue;
    bool flag = false;
    struct Node *s;
    s = start;
    if (start == NULL)  
    {
        cout<<"Unable";
        return;
    }
    s->WarehouseN = tempvalue;
    cout<<"Warehouse "<<tempvalue<< ": Tool ";
    while(s != NULL)
        {
            if (s->WarehouseN == tempvalue)
            {
            flag = true;
            cout<< s->ToolN <<" ";
            s = s->next;
            }
    }
}
lnjblue
  • 140
  • 1
  • 13

3 Answers3

1

You haven't assigned any values to tempvalue so it causes undefined behavior. Read this post.

Also based on what you have in struct Node and your code, I think you can have something like this picture in the program and you want to print them.

Lined List

So to top it off I would write something like this code:

void showWarehouses()
{
    int tempvalue=1;
    bool flag, cont;
    struct Node *s;
    if (start == NULL)
    {
        cout << "Unable";
        return;
    }

    cont = true;
    while (cont)
    {
        cont = false, flag = false;
        s = start;
        while (s)
        {
            if (s->WarehouseN == tempvalue){
                cont = true;
                if (!flag){
                    cout << "Warehouse " << tempvalue << ": Tool ";
                    flag = true;
                }
                cout << s->ToolN << " ";
            }
            s = s->next;
        }
        cout << endl;
        tempvalue++;
    }
}
Community
  • 1
  • 1
AKJ88
  • 713
  • 2
  • 10
  • 20
0

i am assuming that ->warehouseN and ->toolN get the warehouse and tool nums accordingly. This is for right after your if statement.

struct Node *temp;
while (s != NULL){
    temp = s;
    cout << "Warehouse " << s->warehouseN << ": Tool ";
    while (s!= NULL) {
        cout << s->toolN << " ";
        s = s-> next;
    }
    s = temp->next;
    cout << endl;
}

also, you should probably initiate s before setting it as start

R Nar
  • 5,465
  • 1
  • 16
  • 32
  • I want to search the whole list for the same warehouse number and print all tools associated with that warehouse, consecutively. The code above would print everything separate. – lnjblue Oct 02 '15 at 21:34
0

If this:

s = start;
struct Node *s;

compiles, you have an s in a wider scope that just got set to start before being hidden inside showWarehouses by another variable named s. That would be a bad thing.

Anyway, The immediate result is showWarehouses's s is never initialized and it's probably dumb unluck that the program doesn't crash. Since s is not initialized, the rest of the program is printing garbage so the output being wrong is to be expected.

user4581301
  • 33,082
  • 7
  • 33
  • 54