C++C++11C++14C++17Code SnippetIntroduction to C++Language FeatureLearn C++

Learn To Use Powerful Modern Linked Lists In C++

Learn To Use Powerful Modern Linked Lists In C++ main image

In this post, you’ll get answers to these questions:

  • Can I use linked lists with unique_ptr in a modern way?
  • How can I use linked list with the class, struct combinations?
  • How can I use smart pointers with linked lists?

By learning how to use Powerful Modern Linked Lists in c++, it will help you to build C++ applications with the use of C++ Software.

What does the smart pointer unique_ptr mean?

std::unique_ptr is a smart pointer (Since C++11) that manages and owns another object with a pointer. When the unique_ptr goes out of scope the runtime disposes of that object automatically.

The object at that pointer address is disposed of by its associated “deleter” . This happens If the unique_ptr object is destroyed or if the unique_ptr object is assigned another pointer by the operator ‘=’ or by the reset() method. Thus the user do not need to free or delete the smart pointer. The default deleter uses the delete operator which destroys the object and deallocates the memory. A unique_ptr may have no object that means it is empty.

std::unique_ptr can be used with the dynamically-allocated array of objects and with the single objects.

What is the syntax for unique_ptr?

Here is a unique_ptr example used with a struct,

How to use modern linked lists with std::unique_ptr?

First let’s create a Type class (TLinkedList) for our modern linked list,

under this public: definition we can define our st_node struct.

and let’s keep adding more lines. We can define the head of this st_node by using std::unique_ptr as below,

and in this class we can define a add_value() method to add our values as give below,

finally we can add destructor ~TLinkedList() method to delete all linked list from the memory

Remember that destructor name should be same as your linked list class. As a result, our class will be as below,

How do I declare an instance of TLinkedList class?

Now we should declare TLinkedList class in our VCL form,

Thus we have a modern linked list that destroy it self safely. We can create a linked list (linked_list) by using our linked list type (TLinkedList) in any procedure. For example,

This TLinkedList class can be used in the most of C++ compilers (C++11, C++14, C++17). Here is the full C++ Builder VCL file,

How do I declare the TLinkedList Class in my C++ app?

Don’t forget to add class declaration in header file of the form in public as below,

This is a very modern and unique example that combines the class and the struct together with a linked list created by std::unique_ptr.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Deleted Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?