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

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

female freelancer typing on laptop while sitting in bedroom

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.

Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn about C++ Encapsulation

C++C++11C++14C++17Introduction to C++Learn C++

Discover Class Methods in C++

C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

en_USEnglish