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;


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

How To Run A C Program in Command Prompt

CC++C++17Learn C++

How To Compile And Run A C Program

CC++Introduction to C++Learn C++

What Kind Of Programming Language Is C?

CC++Introduction to C++Learn C++

What Does C Mean In Programming?