为什么 iostream 在这里不能正常工作?

Why is the iostream not working properly here?

这是我的主要内容:

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;
}

这是输出:

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

但这不是预期的,预期的是:

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

所以这里出了什么问题,为什么会这样?

你写

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

但您仍将值添加到 L1

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

您应该将此行更改为 L2