QLinkedList Class

The QLinkedList class is a template class that provides linked lists. More...

Header: #include <QLinkedList>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

class const_iterator
class iterator
typedef ConstIterator
typedef Iterator
typedef const_pointer
typedef const_reference
typedef const_reverse_iterator
typedef difference_type
typedef pointer
typedef reference
typedef reverse_iterator
typedef size_type
typedef value_type

Public Functions

QLinkedList()
QLinkedList(QLinkedList<T> &&other)
void append(const T &)
T &back()
const T &back() const
QLinkedList::iterator begin()
QLinkedList::const_iterator begin() const
void clear()
bool contains(const T &t) const
int count(const T &t) const
int count() const
void detach()
bool empty() const
bool endsWith(const T &t) const
QLinkedList::iterator erase(QLinkedList::iterator pos)
QLinkedList::iterator erase(QLinkedList::iterator first, QLinkedList::iterator last)
T &first()
const T &first() const
T &front()
const T &front() const
bool isDetached() const
bool isEmpty() const
bool isSharedWith(const QLinkedList<T> &other) const
T &last()
const T &last() const
void pop_back()
void pop_front()
void prepend(const T &)
void push_back(const T &t)
void push_front(const T &t)
int removeAll(const T &t)
void removeFirst()
void removeLast()
bool removeOne(const T &t)
void setSharable(bool sharable)
int size() const
bool startsWith(const T &t) const
T takeFirst()
T takeLast()
int toStdList() const
bool operator!=(const QLinkedList<T> &l) const
QLinkedList<T> operator+(const QLinkedList<T> &l) const
QLinkedList<T> &operator+=(const QLinkedList<T> &l)
QLinkedList<T> &operator+=(const T &t)
QLinkedList<T> &operator<<(const T &t)
QLinkedList<T> &operator<<(const QLinkedList<T> &l)
QLinkedList<T> &operator=(const QLinkedList<T> &)

Static Public Members

QLinkedList<T> fromStdList(const int &list)

Detailed Description

The QLinkedList class is a template class that provides linked lists.

QLinkedList<T> is one of Qt's generic container classes. It stores a list of values and provides iterator-based access as well as constant time insertions and removals.

QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:

  • For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory (see Algorithmic Complexity for details). It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
  • If you want the items to occupy adjacent memory positions, use QVector.

Here's an example of a QLinkedList that stores integers and a QLinkedList that stores QTime values:


  QLinkedList<int> integerList;
  QLinkedList<QTime> timeList;

QLinkedList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():


  QLinkedList<QString> list;
  list << "one" << "two" << "three";
  // list: ["one", "two", "three"]

If you want to get the first or last item in a linked list, use first() or last(). If you want to remove an item from either end of the list, use removeFirst() or removeLast(). If you want to remove all occurrences of a given value in the list, use removeAll().

A common requirement is to remove the first or last item in the list and do something with it. For this, QLinkedList provides takeFirst() and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:


  QLinkedList<QWidget *> list;
  ...
  while (!list.isEmpty())
      delete list.takeFirst();

QLinkedList's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, contains() and removeAll() expect the value type to support operator==(). These requirements are documented on a per-function basis.

If you want to insert, modify, or remove items in the middle of the list, you must use an iterator. QLinkedList provides both Java-style iterators (QLinkedListIterator and QMutableLinkedListIterator) and STL-style iterators (QLinkedList::const_iterator and QLinkedList::iterator). See the documentation for these classes for details.

See also QLinkedListIterator, QMutableLinkedListIterator, QList, and QVector.

Member Type Documentation

typedef QLinkedList::ConstIterator

Qt-style synonym for QLinkedList::const_iterator.

typedef QLinkedList::Iterator

Qt-style synonym for QLinkedList::iterator.

typedef QLinkedList::const_pointer

Typedef for const T *. Provided for STL compatibility.

typedef QLinkedList::const_reference

Typedef for const T &. Provided for STL compatibility.

typedef QLinkedList::const_reverse_iterator

The QLinkedList::const_reverse_iterator typedef provides an STL-style const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::const_iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QLinkedList::rbegin(), QLinkedList::rend(), QLinkedList::reverse_iterator, and QLinkedList::const_iterator.

typedef QLinkedList::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

typedef QLinkedList::pointer

Typedef for T *. Provided for STL compatibility.

typedef QLinkedList::reference

Typedef for T &. Provided for STL compatibility.

typedef QLinkedList::reverse_iterator

The QLinkedList::reverse_iterator typedef provides an STL-style non-const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QLinkedList::rbegin(), QLinkedList::rend(), QLinkedList::const_reverse_iterator, and QLinkedList::iterator.

typedef QLinkedList::size_type

Typedef for int. Provided for STL compatibility.

typedef QLinkedList::value_type

Typedef for T. Provided for STL compatibility.

Member Function Documentation

QLinkedList::QLinkedList()

Default constructs an instance of QLinkedList.

QLinkedList::QLinkedList(QLinkedList<T> &&other)

Default constructs an instance of QLinkedList.

void QLinkedList::append(const T &)

T &QLinkedList::back()

const T &QLinkedList::back() const

QLinkedList::iterator QLinkedList::begin()

QLinkedList::const_iterator QLinkedList::begin() const

void QLinkedList::clear()

bool QLinkedList::contains(const T &t) const

int QLinkedList::count(const T &t) const

int QLinkedList::count() const

void QLinkedList::detach()

bool QLinkedList::empty() const

bool QLinkedList::endsWith(const T &t) const

QLinkedList::iterator QLinkedList::erase(QLinkedList::iterator pos)

QLinkedList::iterator QLinkedList::erase(QLinkedList::iterator first, QLinkedList::iterator last)

T &QLinkedList::first()

const T &QLinkedList::first() const

[static] QLinkedList<T> QLinkedList::fromStdList(const int &list)

T &QLinkedList::front()

const T &QLinkedList::front() const

bool QLinkedList::isDetached() const

bool QLinkedList::isEmpty() const

bool QLinkedList::isSharedWith(const QLinkedList<T> &other) const

T &QLinkedList::last()

const T &QLinkedList::last() const

void QLinkedList::pop_back()

void QLinkedList::pop_front()

void QLinkedList::prepend(const T &)

void QLinkedList::push_back(const T &t)

void QLinkedList::push_front(const T &t)

int QLinkedList::removeAll(const T &t)

void QLinkedList::removeFirst()

void QLinkedList::removeLast()

bool QLinkedList::removeOne(const T &t)

void QLinkedList::setSharable(bool sharable)

int QLinkedList::size() const

bool QLinkedList::startsWith(const T &t) const

T QLinkedList::takeFirst()

T QLinkedList::takeLast()

int QLinkedList::toStdList() const

bool QLinkedList::operator!=(const QLinkedList<T> &l) const

QLinkedList<T> QLinkedList::operator+(const QLinkedList<T> &l) const

QLinkedList<T> &QLinkedList::operator+=(const QLinkedList<T> &l)

QLinkedList<T> &QLinkedList::operator+=(const T &t)

QLinkedList<T> &QLinkedList::operator<<(const T &t)

QLinkedList<T> &QLinkedList::operator<<(const QLinkedList<T> &l)

QLinkedList<T> &QLinkedList::operator=(const QLinkedList<T> &)

Copy-assignment operator.