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

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 pointer with linked lists? Let’s try to answer these questions.

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.

close

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsLanguage FeatureLearn C++

How To Add Shadow Effects To Your C++ Apps

C++ComponentsLanguage FeatureLearn C++

How To Make Controls Have A Glow Effect In C++?

C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

C++C++17Introduction to C++Language FeatureLearn C++

This Is How To Use Parallel Programming in C++ Builder

en_USEnglish