Site icon Learn 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:

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?

[crayon-673fe8eeaca47646431450/]
[crayon-673fe8eeaca50349883273/]

Here is a unique_ptr example used with a struct,

[crayon-673fe8eeaca51221498810/]

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

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

[crayon-673fe8eeaca52902729205/]

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

[crayon-673fe8eeaca53396383843/]

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

[crayon-673fe8eeaca54249111667/]

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

[crayon-673fe8eeaca55318697184/]

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

[crayon-673fe8eeaca56818381913/]

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

[crayon-673fe8eeaca57733616567/]

How do I declare an instance of TLinkedList class?

Now we should declare TLinkedList class in our VCL form,

[crayon-673fe8eeaca58636635392/]

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,

[crayon-673fe8eeaca5b182473881/]

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,

[crayon-673fe8eeaca5c807847963/]

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,

[crayon-673fe8eeaca5d968157089/]

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.

Exit mobile version