显示链接列表内联

Display linked list inline

本文关键字:列表 链接 显示      更新时间:2023-10-16

我正在尝试显示链接的列表元素,而不是将其显示在新行中。

我最初想到,因为我有/nendl它在新行中显示,所以我删除了它们,它们仍然显示在新线路中。

// display function
    void display()
    {
        Node*current = head;
        while (current != NULL)
        {
            cout << "Queue";
            cout << current->value << " ";
            cout << endl;
            current = current->next;
        }

    }
// adding to queue function
void enqueue(int num) {
        Node *node = new Node(num);
        cout << "Pushing: " << num << endl;
        if (tail != NULL)
        {
            tail->next = node;
        }
        tail = node;
        if (head == NULL)
        {
            head = node;
        }
        display();
    }

每次我将某些东西推到队列时,都会显示要推动的值,以及当前存储在队列中的值。每次我推东西

时都在打印什么
// Results
Pushing 1
Queue: 1
Pushing 2 
Queue: 2
Pushing 3
Queue: 3
// What I want to display
Pushing 1
Queue: 1
Pushing 2
Queue: 1 2
Pushing 3
Queue: 1 2 3

在循环外放置 cout<<endl

void display()
{
    Node*current = head;
    while (current != NULL)
    {
        cout << "Queue";
        cout << current->value << " ";
        current = current->next;
    }
    cout << endl;
}