C++

Std List C++

Std list is a sort of container that is used in the insertion and removal of items from the top and the bottom as well. It is known to be a sequenced container that is used to allocate non-contiguous memory. The list contains the container available with the standard template library in C++ (STL). Std list allows us to insert or delete items from anywhere in the container. It acts as a doubly-linked list. We cannot store multidimensional arrays in the list. This article will contain information regarding list creation and its work in entering and deleting data.

Reasons for using std list

There could be many reasons for using a list in C++, but one of the main reasons is that std::list works better than other sequence containers like arrays of vectors. They show better performance while inserting, removing, or moving data from any position in the list. Std list also performs effectively with algorithms that can perform such operations.

List syntax

For defining a list in C++ program, we need to import the <list> header file first. The syntax is given below:

Template <class Type, class Alloc_obj =allocator <T> > class list;

The description of the above-used parameters in the syntax is given:

  • T: It defines the data type of the items present inside the list.
  • Alloc_obj: It shows the type of the object of an allocator. It used the allocator class template by default. It uses a simple model of memory allocation and also is value-dependent.

Working on the list in C++

Now we have described some important features of Lists in C++.

As we have described above, lists are implemented so that operations like insertion and removal of elements and other access can be done bi-directionally. Hence act as a doubly-linked list. The elements to be stored in the list can be stored at any location in the memory allocated. All the items in the list connect with each other by using the link double-linked list of the previous items. In C++, lists have a slow traversal in comparison to vectors. But once the item is found, the insertion and deleting items are easy to do.

A list is not considered a good option for the small number of elements. It carries more space in the memory compared to other containers that keep track of preceding and the previous items by connection through the links. The lists in C++ provide the facility of increasing or decreasing its size dynamically.

Functions/ operations of list

Some of the commonly used operators of the list are written below.

  • List::end(): This function works in such a way that it returns the iterator that points to the last element of a list.
  • Back: The function accesses the last element of the list.
  • Empty: It assures whether the list is empty or not.
  • Pop_front: It removes or pops out items from the front of the list
  • Pop_back: It removes the items like pop_front but from the back. It pops out the last item and also decreases the list by 1.

Implementation of the list in C++

Example 1

This is a sample implementation of the list. We will see how a list is created. To implement the program, we first need the header file to be included in the source code libraries.

#include <list>

By using this header file, we will perform all the functions related to lists. Moreover, we have also used the ‘algorithm’ header file to utilize its functions. The main program is called. The source code contains the program logic inside it. First, create a list with the name ‘my_list’ having 6 items in it. Then we will display all the values inside the list on the console terminal. For this purpose, a FOR loop is used. This loop will iterate over the items of the list.

We will compile the code and then execute it on the terminal to display the result. Use a G++ compiler for this purpose.

$ g++ -o file file.c

$ ./file

Example 2

This program contains almost all the main operations/functions of lists described above in the article.

Firstly all the libraries are included, and then we have also added an iterator library to initialize the iterator of the list. We have created a separate function for taking the print of the values. This will take the object of the list we created in the main program; later on, we will see that. An iterator is created.

List <int> :: iterator it;

All the elements will be displayed, respectively, using a FOR loop and utilizing begin() and end() functions inside the loop.

Now we will throw light on the main program. We have created two lists. Use the loop to enter the values. In list 1, we will enter the values from the back. While list 2 will be entertained by pushing the values from the front. After that, all the elements of list1 are displayed, so the showlist() function is called along with list 1 and the object.

Similarly, the same process is for list 2. Then we will display the elements present at the front and backside of list 1 to ensure it contains the elements in a forward direction.

The next operation is to pop out all the values of list 1 from the front and the back. All the values are displayed in ascending order first and then descending order.

Now we will check the resultant values from the Ubuntu terminal.

Example 3

In this example, the same functions are used but in a different manner. First, the list is created with 5 elements in it. Then with the help of an object, we will push new items, one from the back and one from the front side.

Mlist.push_back(6);

Mlist.push_front(0);

All the items are then printed with those elements that are pushed previously.

Then one item from the back is removed. Again display all the list items after removing through the pop_back() function.

The next function deals with searching a particular item and then inserting a new item in the place before it. For instance, 1 is searched, and 9 is inserted after 0 and before 1.

Mlist.insert(I, 9)

So this function is used. After the new insertion, all the elements are inserted.

See the output through the terminal by execution of the program. You will observe that all the items are displayed according to the functions applied respectively.

Conclusion

“Std list C++” article contains the description and working of a list in C++. We have used the C++ programming language in the Linux operating system Ubuntu 20.04. All the operations and main features are explained in detail to assist the new user with the knowledge of lists in C++. Three elementary examples have been described that show the detailed image of the functions of the list like push, pop, and size of the list created dynamically.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.