A 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.
A 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.
1 2 3 4 5 6 7 8 |
struct st_user { String name; // if you want constant size you can use wchar_t name[32]; int age; struct st_user *next; }; |
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;
1 2 3 |
struct st_user *head, *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;
1 2 3 4 |
p = (struct st_user*) malloc(sizeof(st_user)); p->name = L"Jim"; p->age = 23; p->next = 0; head = p; |
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
1 2 3 4 |
p = (struct st_user*) malloc(sizeof(st_user)); p->name = L"Ali"; p->age = 27; p->next = 0; head->next = p; |
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;
1 2 3 4 |
p = head; while(p!=NULL) { std::wcout << p->name << ":" << p->age << " , "; p=p->next; }; |
At the end we should free all linked list from the memory,
1 2 3 4 5 |
struct st_user *tmp; p = head; while(p!=NULL) { tmp=p; p=p->next; free(tmp); }; |
Full code of this C++ Builder Console VCL Application will be like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <vcl.h> #include <windows.h> #include <string.h> #include <tchar.h> #include <stdio.h> struct st_user { String name; // if you want constant size you can use wchar_t name[32]; int age; struct st_user *next; }; int _tmain(int argc, _TCHAR* argv[]) { struct st_user *head, *p, *tmp; // Add some linked list members p = (struct st_user*) malloc(sizeof(st_user)); p->name=L"Jim"; p->age=23; p->next=0; head=p; p = (struct st_user*)malloc(sizeof(st_user)); p->name=L"Ali"; p->age=27; p->next=0; head->next = p; // Print All Linked List p = head; while(p!=NULL) { std::wcout << p->name << ":" << p->age << " , "; p=p->next; }; while(p!=NULL) { tmp=p; p=p->next; free(tmp); }; //free all linked list getchar(); return 0; } |