user12425978
user12425978

Reputation:

Why is the iostream not working properly here?

Here is my main:

int main()
{
    LinkedList<int> L1; 
    LinkedList<int> L2;
    int val,k;

    cout<<"\nPlease enter int values to add to the list L1 (-1 to stop):\n";
    cin>>val;
    while(val != -1)
    {
        L1.InsertBeg(val);
        cin>>val;
    }
    L1.PrintList();

    cout << "\nPlease enter int values to add to the list L2 (-1 to stop):\n";
    cin >> k;
    while (k != -1)
    {
        L1.InsertBeg(k);
        cin >> k;
    }
    L1.PrintList();

    return 0;
}

and here is the output:

Please enter int values to add to the list L1 (-1 to stop): 1 2 3 -1 [ 3 ]--->[ 2 ]--->[ 1 ]--->NULL Please enter int values to add to the list L2 (-1 to stop): 4 5 6 -1 [ 6 ]--->[ 5 ]--->[ 4 ]--->[ 3 ]--->[ 2 ]--->[ 1 ]--->NULL

But it's not the expected, the expected one is:

Please enter int values to add to the list L1 (-1 to stop): 1 2 3 -1 [ 3 ]--->[ 2 ]--->[ 1 ]--->NULL Please enter int values to add to the list L2 (-1 to stop): 4 5 6 -1 [ 6 ]--->[ 5 ]--->[ 4 ]--->NULL

so what is wrong here, why does this happen?

Upvotes: 0

Views: 57

Answers (1)

Brando Jason
Brando Jason

Reputation: 58

You write

cout << "\nPlease enter int values to add to the list L2 (-1 to stop):\n";

but you still add values into L1

while (k != -1)
    {
        L1.InsertBeg(k);
        cin >> k;
    }

you should change this lines as L2

Upvotes: 1

Related Questions