17
Level up your coding skills with the C++ Standard Template Library (STL): List BY JOYJIT CHOUDHURY

List

Embed Size (px)

Citation preview

Page 1: List

Level up your coding skills with

the C++ Standard Template

Library (STL):

List

BY JOYJIT CHOUDHURY

Page 2: List

List

List is a sequence container that allows constant time

insertion and deletions anywhere in the container

It allows iterations in both directions

Fast random access is not supported

List containers are implemented as Doubly Linked List

Defined in the header <list>

Belongs to the namespace std

Page 3: List

Construction of a list

Page 4: List

Accessing the elements

Member functions front() and back(), returns a

reference to the first and last element of the list,

respectively

front() back()

Page 5: List

Accessing the elements

front() and back() return a reference, so not only can

you view the first and last element, you can also modify

them

list<int> mylist

Page 6: List

Insert elements

Member functions push_front() and push_back(), inserts an element

to the beginning and the end of the list, respectively

Page 7: List

Delete elements

Member functions pop_front() and pop_back(), removes the first

and the last element of the list, respectively

Page 8: List

Size of the list

Member function size() returns the number of elements in the list

Time Complexity : O(n)

Page 9: List

Is the list empty?

Member function empty() returns true if the list is empty (i.e. list size is 0),

else returns false

Time Complexity : O(1)

It’s a preferred way to check

whether a list is empty,

instead of checking whether

the size of the list is 0, since

this takes constant time

Page 10: List

So, how do you Traverse a list?

The thing about containers is, you hardly know anything about it’s internal

mechanisms

All the internal details regarding :

How the memory is allocated

Where and how the data elements are stored

How the insertion, access and removal of individual elements take place

It’s all kept hidden from the user.

So, what if you had to access the individual elements, maybe even modify

them?

For that purpose, containers have what is known as Iterators

Page 11: List

What are Iterators?

An iterator is an object, pointing to some element in a range of

elements (usually, a container), which has the ability to iterate through

the elements in that range through a set of operators

(increment (++) operator, dereference (*) operator, etc)

You can think of them as an abstraction of pointers

What that means is, they are like pointers in certain ways, but not

exactly

Page 12: List

Iterators

The best way to understand iterators, would be through an example

list<int> myList

myList.begin()

Page 13: List

Iterators

list<int> myList

myList.begin() myList.end()

Page 14: List

Insert elements anywhere in the list Member function insert(), inserts new elements before the element at

the specified position. The position is specified by passing an iterator as a

parameter.

Page 15: List

Delete elements from anywhere

Member function erase(), removes from the list, either a single element from

the specified position, or all the elements in the specified range [first, last).

Page 16: List

That’s not all. There are many other functions

and techniques that could come in handy.

Read about them on cplusplus.com or

cppreference.com or somewhere else. Just

google it!

Page 17: List