How To Create A Linked List In A C++ Program

The C++ programming language is one of the top 5 programming languages around the world. The superbly rich C++ programming language includes many elements of the C language and goes further by adding object-oriented programming features like classes, objects, and methods. Linked Lists are used to sort data groups (i.e. users, parts) in C and C++ applications. In this post we will learn C++ with a modern linked list example. We will learn to use modern linked lists using std::unique_ptr.

As C and C++ programmers we have many great selections of C++ IDEs. Tools like the free C++ Builder CE and Dev-C++. You can even use the BCC32 command line compiler. Although C++ Builder’s primary purpose is to be the best available C++ IDE which helps developers be at their most efficient and effective possible selves it is also quite capable of editing and compiling most C code thanks to C and C++ having a shared lineage.

How To Create A Linked List In A C++ Program. The C and C++ logos.

How to create a linked list in a C++ program with std::unique_ptr ?

First let’s create a Type class, let’s name it as TLinkedList for our modern linked list,

How to create a class for a linked list in a C++ program?

If you want you can use your own class names, remember that this is a type class, so using T letter at the beginning of the identifier is common practice.

How to declare a node of a linked list?

Now, in this class we will have members of linked list, generally these are called nodes. This can be integer data, x,y,z coordinates of object in a 3D environment, users, material parts or any data can be a node. First, we need to declare our node structure.

Under this public: definition of TLİnkedList example above, we can declare our st_node struct. In this example our node has two parameters; val value and a pointer to the next node.

std::unique_ptr is a smart pointer (Since C++11). It manages and owns another object with a pointer. When the unique_ptr goes out of scope the runtime disposes of that object automatically. C++ is object oriented so we will create a class to use linked list.

How to declare a head node of a linked list?

All linked lists have a primary element, called the head. This is the first address of the first member of the linked list. If the linked list is empty, it is a null parameter. We can define the head of this st_node by using std::unique_ptr as below,

How to create a add method to a linked list in a C++ program?

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

How to free a linked list in a C++ program when the class is destructed?

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

Remember that the destructor name should be same as your linked list class.

Is there a simple example of using a linked list in a C++ program?

As a result of codes above, our TLinkedList class will be as below,

How to declare a linked list in a C++ program?

Now we should declare the TLinkedList class in our VCL form,

If we are using C++ Builder, this line should be under the TForm1 class as a public member as given below,

How to create an object for a linked list in a C++ program from its class?

Now we can create many linked list objects with our class type. For example,

How to add new members to a linked list in a C++ program?

Thus we have a modern linked list that will destroy itself 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). Below is the full C++ Builder VCL code for using a linked list in a C++ program.

Is there a full example about how to create a linked list in a C++ program?

Here is the full C++ Builder VCL example about to create a simple linked list under the TLinkedList class.

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

How to create classic linked list in a C++ program (or in classic C)?

If you don’t want to use classes, here is another classic linked list method that can be used in both C++ and C applications. Note that the example above is a modern approach, much safer and easier in use.

Some developers may find linked lists complex and unsafe. Another similar way of handling this kind of data in C++ is using vectors. If you are good with using vectors you don’t need to use linked lists. You can use methods of vectors to add, to remove or to list all members of a vector. Please search vectors in All of this can be used in C++ applications with the latest RAD Studio, C++ Builder.

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.

Download RAD Studio 11


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
CC++C++17Introduction to C++Learn C++

How To Run A C++ Program In The Linux Terminal

C++C++17Game DevelopmentLearn C++

How To Program A Game In C++

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

What Is C++ Programming Used For?

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

How To Run A C++ Program