简体   繁体   English

在单链接列表中恒定时间实现Popback

[英]Implementing Popback in constant time in a singly linked list

I wanted to know if it was possible( It should be as std::list seems to do it ) to implement a PopBack() operation on a singly linked list in constant time and how? 我想知道是否有可能( 在std :: list看来应该这样做 )在恒定时间内对单链接列表实施PopBack()操作,以及如何进行?

I am assuming that we store head and tail pointers. 我假设我们存储头和尾指针。 In such a case, PushBack(), PushFront(), PopFront() can be easily implemented in constant time. 在这种情况下,可以很容易地在恒定时间内实现PushBack(),PushFront(),PopFront()。 But can't think of the way to implement PopBack() with the same running time. 但是无法想到在相同的运行时间下实现PopBack()的方法。

You can not implement pop_back for a single linked list in a constant time. 您不能在恒定时间内为单个链接列表实现pop_back To do this you need to know the previous element before the last that is the last element shall contain a reference to the previous element. 为此,您需要在最后一个元素之前知道先前的元素,即最后一个元素应包含对先前元素的引用。 If so then you will have a double linked list. 如果是这样,那么您将有一个双向链接列表。

You can inplement push_back in a constant time. 您可以在固定时间内添加push_back One of my proposals for C++ Standard was to introduce std::x_forward_list that will support push_back member function. 我对C ++ Standard的建议之一是引入std::x_forward_list ,它将支持push_back成员函数。 In this case you will be able to use such a single linked list to simulate a queue. 在这种情况下,您将能够使用这样的单个链接列表来模拟队列。

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

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