In Qt you should use QLinkedList in case you need to implement linked list.
It is fast to append, prepend, insert elements into QLinkedList
- O(1), but index lookup is slower than in QList
or QVector
- O(n). This is normal taking into attention you have to iterate through nodes to find something in linked list.
Full algorithmic compexity table can be found here.
Just to insert some elements into QLinkedList
you can use operator <<()
:
QLinkedList<QString> list;
list << "string1" << "string2" << "string3";
To insert elements in the middle of QLinkedList
or modify all or some of its elements you can use Java style or STL style iterators. Here is a simple example how we multiply all the elements of QLinkedList
by 2:
QLinkedList<int> integerList {1, 2, 3};
QLinkedList<int>::iterator it;
for (it = integerList.begin(); it != integerList.end(); ++it)
{
*it *= 2;
}