List in C++(4.5)


List are the type of containers that stores data in sequential manner and allocate noncontiguous memory to the elements. In C++, lists are considered as doubly linked list where insertion and deletion of elements can be done from both the ends and therefore, it is also possible to traverse the list from both the end. For using singly linked list we use the forward list available in C++ STL.

Advantage of using List over vector is that

List are faster in insertion and deletion of elements available in list container if the iterator is positioned to the correct element.

Disadvantage of using List is that

In list it is difficult to fetch the element available in the container directly through its position. Like, if we want to fetch the fourth element then it would be difficult to directly jump to the fourth element instead the iterator needs to reach there from the start or end location.

Functions that are associated with the Lists are

  • Push_front(element) - This function is used to insert element in the starting of the list.

    Syntax -listName.push_front(datatype element)

    Parameter - It takes one parameter as the value to be inserted.

    Return Value - No Return Value.

  • Push_back(element) - This function is used to insert element at the end of the list.

    Syntax - listName.push_back(datatype element)

    Parameter - It takes one parameter as the value to be inserted.

    Return Value - No Return Value.

  • Insert() - This function is used to insert the element at the given position in the list container.

    Syntax -: listName.insert(position, total, element)

    Parameter - It takes three parameters -

    • Position, at which the element needs to be inserted

    • Total, specifies the total number of elements to be inserted

    • Element specifies the element to be inserted.  

  • Return Value - returns iterator pointing to the start of newly inserted elements.

  • Pop_front() - This function is used to remove the element from the start of the list.

    Syntax - listName.pop_front()

    Parameter - No Parameter

    Return Value - No Return Value

  • Front() - This function is used to fetch the first element of the list.

    Syntax - listName.front()

    Parameter - No Parameter

    Return Value - This function returns the direct reference to the first element

  • Pop_back() - This function is used to remove the element from the end of the list.

    Syntax - listName.pop_back()

    Parameter - No Parameter

    Return Value - No Return Value

  • back() - This function is used to fetch the last element of the list.

    Syntax - listName.back()

    Parameter - No Parameter

    Return Value - This function returns the direct reference to the last element

  • size() - This function is used to fetch the total numbers of elements in the list.

    Syntax - listName.size()

    Parameter - No Parameter

    Return Value - This function returns the total number of elements in the list.

  • Resize() - This function is used to resize the total numbers of elements in the list.

    Syntax - listName.resize(int resized_number, value(optional))

    Parameter - it takes two parameters

    • resized_number - exact number up to which the size of container to be increased or decreased

    • value(optional) - it is an optional parameter which will add the value specified to the end of the elements.

  • Return Value -: No return value.

  • Max_size() - This function is used to fetch the maximum numbers of elements a list can have.

    Syntax - listName.max_size()

    Parameter - No Parameter

    Return Value - This function returns the maximum numbers of elements a list can have.

  • Clear() - This function is used to delete all the elements from the list and reset its size to 0.

    Syntax - listName.max_size()

    Parameter - No Parameter

    Return Value - No Return value.

  • remove(element) - This function is used to delete all the elements matching with the element passed in parameter.

    Syntax - listName.remove(element)

    Parameter - it takes single parameter which specifies the element to be deleted from the list container.

    Return Value - No Return value.

  • Remove_if(Function pointer/function object) - This function is used to delete all the elements matching with the element passed in parameter based upon the condition passed in a parameter.

    Syntax - listName.remove_if(Function pointer/function object)

    Parameter - it takes single parameter as the function pointer or function object.

    Return Value - it returns true when all the elements are deleted.

  • Erase() - This function is used to erase the single element as well as multiple elements depending upon the parameter passed to it

    Syntax -: iterator listName.erase(iterator position)iterator listName.erase(iterator First_ele, iterator Last_ele)

    Parameter - In First syntax, it takes parameter position which specifies the position at which the element will be deleted from the list container. In Second syntax, it takes parameter first element and last element which specifies the range from which the elements will be deleted

    Return Value - it returns an iterator pointing to the lastly deleted element.

  • Empty() - This function is used to check whether the list is empty or not.

    Syntax - listName.empty()

    Parameter - No parameter

    Return Value -

    • Return true − when list is empty

    • Return false − when list is not empty.

  • Begin() - This function returns an iterator that points to the first element in the list.

    Syntax - listName.begin()

    Parameter - No parameter

    Return Value - Returns an iterator that points to the first element in the list.

  • End() - This function returns an iterator that points to the last element in the list.

    Syntax - listName.end()

    Parameter - No parameter

    Return Value - Returns an iterator that points to the end element in the list.
    rbegin() - This function returns a reverse iterator that points to the last element in the list.
    Syntax - listName.rbegin()
    Parameter - No parameter
    Return Value - Returns a reverse iterator that points to the end element in the list.

  • rend() - This function returns a reverse iterator that points to the first element in the list.

    Syntax - listName.rend()

    Parameter - No parameter

    Return Value - Returns a reverse iterator that points to the first element in the list.

  • Cbegin() - This function is used return a constant random access iterator which points to the beginning of the list.

    Syntax - listName.cbegin()

    Parameter - No parameter

    Return Value - returns a constant random-access iterator which points to the beginning of the list.

  • Cend()- − This function is used return a constant random access iterator which points to the end of the list.

    Syntax - listName.cend()

    Parameter - No parameter

    Return Value - returns a constant random-access iterator which points to the end of the list.

  • Crbegin() - This function is used return a constant random access reverse iterator which points to the end of the list.

    Syntax - listName.crbegin()

    Parameter - No parameter

    Return Value - returns a constant random access reverse iterator which points to the end of the list.

  • Crend() - This function is used return a constant random access reverse iterator which points to the beginning of the list.

    Syntax - listName.crend()

    Parameter - No parameter

    Return Value - returns a constant random access reverse iterator which points to the beginning of the list.

  • reverse() - This function is used reverse all the elements in the list container.

    Syntax - listName.reverse()

    Parameter - No parameter

    Return Value - No return value

  • unique() - This function is used to remove all the duplicate consecutive elements from the list.

    Syntax - listName.unique(predicate that consider two values to be same)

    Parameter - it takes optional parameter that returns true when two element needs to be considered as same.

    Return Value - No return value

  • emplace() - This function is used to insert the new element at the given position.

    Syntax - listName.emplace(position, value)

    Parameter - It takes two parameter, one that specifies the position at which the element needs to be inserted and another tells the element value that needs to be inserted.

    Return Value - returns an iterator that points to the newly inserted element.

  • Emplace_front() - This function is used to insert the new element at the beginning of the list.

    Syntax - listName.emplace_front(element)

    Parameter - It takes one parameter that specifies the value to be inserted

    Return Value - No return value.

  • Emplace_back() - This function is used to insert the new element at the end of the list.

    Syntax - listName.emplace_back(element)

    Parameter - It takes one parameter that specifies the value to be inserted

    Return Value - No return value.

  • Operator(=) - this operator is used to replace the content of one list by the another list.

    Syntax - listName_1 = listName_2

    Parameter - No parameter

    Return Value - No return value.

  • Swap() - this function is used to swap the content of one list by another list of same type

    Syntax - listName_1.swap(listName_2)

    Parameter - No parameter

    Return Value - No return value.

  • Merge() - this function is used to merge the elements to two lists.

    Syntax - listName_1.merge(listName_2)

    Parameter - No parameter

    Return Value - No return value.

Updated on: 24-Apr-2020

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements