简体   繁体   English

从队列中删除第一个元素 C++

[英]Removing the first element from a queue C++

I'm trying to remove the first element of the Queue but, this does not seem to work.我正在尝试删除队列的第一个元素,但这似乎不起作用。 Here is my code:这是我的代码:

struct List {
    //string name;
    double price;
    List* next; 
};
List* head = NULL; List* tail = NULL;
struct queue
{
    List* head, * tail;
};
void printAll()
{
    cout << "Data in the queue:\t";
    for (List* temp = head; temp != NULL; temp = temp->next)
    {
        cout << temp->price << "\t";

        if (temp->next == NULL)
        {
            delete temp; // here I don’t know how to write it down
        }
}

Remember that using existing containers is much easier to get results.请记住,使用现有容器更容易获得结果。 In this case, removing the oldest entry in a std::queue would be if (. myQueue.empty()) myQueue;pop();在这种情况下,删除std::queue中最旧的条目将是if (. myQueue.empty()) myQueue;pop(); with added safety that there is actually to delete.增加了实际要删除的安全性。

However, to learn programming, it makes sense to make your own containers and there are even circumstances where own containers perform better, so about your question: when changing anything in a linked list, the pointers have to be updated to still have a consistent linked list, so to remove an entry, the pointer to the entry has to point to the next entry and then the selected entry can be deleted.但是,要学习编程,制作自己的容器是有意义的,甚至在某些情况下,自己的容器性能会更好,所以关于您的问题:当更改链表中的任何内容时,必须更新指针以保持一致的链接列表,所以要删除一个条目,指向该条目的指针必须指向下一个条目,然后才能删除选定的条目。 In case of the first entry, that's the one that head is pointing to, so just point to that with a temporary pointer, let the head pointer point to what the first entry is pointing to and then you can safely delete the first entry via the temporary pointer.如果是第一个条目,那就是 head 指向的那个,所以只需用一个临时指针指向它,让 head 指针指向第一个条目所指向的内容,然后您可以通过临时指针。

If in your example, you actually fill the queue at the head side and remove at the tail side, you would always need to iterate to find the last but one element to set its pointer to null and let tail point to it.如果在您的示例中,您实际上在头端填充队列并在尾端删除,则始终需要迭代以找到最后一个元素以将其指针设置为 null 并让尾指向它。 This is not a good idea on a singly linked list.这在单链表上不是一个好主意。 Fill at the tail and remove at the head side.在尾部填充并在头部侧移除。

Also, make sure to not change data in "print"-functions.此外,请确保不要更改“打印”功能中的数据。 Make functions predictable by not doing more than is announced by the name.通过不做比名称所宣布的更多的事情来使功能可预测。 A better name for the current function could be "finishExample" or "processQueue" which can do an output and cleanup.当前 function 的更好名称可能是“finishExample”或“processQueue”,它可以执行 output 和清理。

If you feel like cheating, or need some inspiration, this gives an example implementation:https://www.geeksforgeeks.org/queue-linked-list-implementation/ where enQueue() fills and deQueue() removes.如果你想作弊,或者需要一些灵感,这里给出了一个示例实现:https://www.geeksforgeeks.org/queue-linked-list-implementation/其中enQueue()填充和deQueue()删除。 (I just googled an example, so I can't say how good it actually is.) (我只是用谷歌搜索了一个例子,所以我不能说它实际上有多好。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM