C++C++11C++14C++17Learn C++

Learn To Code Simple Linked List In Modern C++ On Windows

What Is An Eligible Copy Constructor In C++ Software

Linked List, composed of structural elements and it is a linear data structure, and each element is stored at different memory locations. Linked lists are good to add, inserts, subtract, delete each element from this list. Linked lists were popular much in the 80s to 2000s, nowadays mostly vectors with structures are used instead of linked lists, because of their simplified operations. They are a little bit hard to code for beginners, but also they are still really good if you are strong on all linked list operations.

linked list element (generally a struct) is presented with a pointer while it’s each element are located in memory with calloc() or malloc() commands in their element size. These elements have at least one pointer of its another structure element ( generally called as *next) that points to another pointer (memory address).

In C++ Builder, you can use both linked list methods and vectors with CLANG C++17 standard compiler or with its New Bcc Compiler.

In this post we will present you a very simple linked list in Modern C++;

Let’s start with defining an element (st_user) structure of our linked list in a structure. This will be used to store user names, ages, and address of the next linked list.

Generally a linked lists is starts with a head pointer that shows the address of first member, and a current pointer (generally *p) is used to operate on a member, some linked lists may use a tail pointer to reach last element easily. Let’s define *head and *p;

Now we can allocate the first member in this user structure (st_user) and we can point it’s address with a head pointer as below;

As you see we allocate first member in memory with (struct st_user*) type, and this memory address is stored in *p. pointer. Now we can add a new member

Here head->next = p; means previous element’s *next address is address of new element. So we linked first element to second element. In this simple example we can write all elements of our linked list as below;

At the end we should free all linked list from the memory,

Full code of this C++ Builder Console VCL Application will be like this;

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

Dr. Yilmaz Yoru has 33+ 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++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?