我试图以内联方式显示链接列表元素,而不是将其放在换行中。
我本来以为是因为我有/n
和endl
在新行中显示,所以我删除了它们,但它们仍在新行中显示。
// 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
答案 0 :(得分:0)
将cout<<endl
放在循环之外:
void display()
{
Node*current = head;
while (current != NULL)
{
cout << "Queue";
cout << current->value << " ";
current = current->next;
}
cout << endl;
}