C++

Print linked list C++

A linked list is a type of data structure just like simple linear lists; it is also a list but contains a series of nodes that are connected through addresses of each other. This article will demonstrate the creation, working, and implementation of the linked list to display its data.

Why linked list is used?

The functionality or working of a linked list is mainly the drawback of arrays. The major drawback of the array is that its size is fixed; we always need to define the size of an array. But in the linked list, we create a dynamic one, in which the upper limit is not necessarily defined.

Another important feature that linked lists have over arrays is that we can insert data at any stage and at any point, either at the end, at the start, or in the middle. Similarly, data at any point can be deleted easily.

Representation of Linked list

A linked list is made of several nodes. Each node has two portions in it. One part is used to have some data in it, and it is referred to as the ‘head’ part. Whereas the second part, known as ‘next’, is a pointer type variable used to carry the address of the next node to build a link between that node. In C++, the linked list creation is declared inside the public portion of the class body.

Implementation of Linked list

We have implemented a linked list in Ubuntu operating system by using text editor and Linux terminal.

This is a sample example in which the context procedure of a linked list is explained. We used a class to create a data variable and a pointer-type variable to store the address. These both are mentioned in the public portion to easily access them by any other function inside the main program. In the main program, we have created 3 nodes. The address part of these nodes is declared as NULL. The first node is also known as a head node. All these three nodes are assigned by the data. The data part is written along with the hyphen and an angular bracket.

Head -> data =1;

After data addition, the first node is linked with a second node. As we know, the next part of the linked list node contains the address of the next node, so the ‘next’ part of the first ‘head’ node having the next part is assigned the address of the second node. In this way, a connection between two nodes is created. A similar phenomenon is applied for the second and the third node for assigning data and connecting nodes. The last node, as in this scenario, the third one, having the ‘next’ part, is assigned as ‘NULL’, as there is no further node to be linked with.

Hopefully, the concept of creating a linked list will be now familiar to you. Now we will proceed forward to a simple C++ program for linked list creation and displaying the results.

Example 1

Print data in Linked list

Like the simple program described above, we have created a class to create a data variable and the pointer type next. The data variable has a data type of integer to store the integer values. Each node address part is declared as none in the main program because there is no creation yet. Each node’s data part is filled with data. And all of these nodes are linked by creating a link, by having the next node address in the address part accept the last node, which should be declared as NULL.

Now coming towards the print portion, here will display the data inside the linked list. The loop will not be terminated until the first node is not null because if the first node is null, it means there are no further nodes inside the list. Display the message along with the data of the first node. This will be done by assigning the value/address present in the next part of one is to the current value of one. And then, the loop continues following the same method for each node.

One = one->next;

After writing the code, now we will save this file with the extension ā€œ.cā€ as it is a C++ program. Go to the Linux terminal and compile the code to execute it. For the compilation, we need a compiler. In the case of C++, we use a G++ compiler. It will compile the source code that we have saved in the file and will store the results in an output file.’ file. c’ is the name of the file.

$ g++ -o file file.c

$./file

On execution, you can see that all the values inside the lists are explained.

Example 2

This example works on the same methodology, but the data is inserted in the main program and will be displayed in a separate function. First, in the class, both the data variables are declared.

Then inside the main program, all these nodes are assigned as Null because of empty addresses. Then just like the previous examples, each node is assigned a specific data in it. Then each node is connected by creating a specific link. Each data inside the node is present in the data part, so the loop will only display the data context. We will not print the address of the nodes. In the end, the print list() function call is made. This function call will send a parameter of the first node in the linked list. As we want to display the contents starting from the first node.

A print list() function is used here to display the data content present in each node. This function will take an argument having a pointer-type variable. This variable will point towards a given node that is mostly the first one.

A while loop is used here by using a logic that the loop will keep looping until the node is null; in other words, the loop will move to the last node. Because only the last node’s next part is empty. In this way, the data from each node will be displayed. And the main part of the print function is that the ā€œnā€ variable that assigns the first node will be transferred to the second node, and then so on. Now compile the code and then execute it.

Drawbacks of linked list

We cannot do random access or search for arrays in linked lists. We can access elements from the start of the node. And binary search is unable to be implemented in this situation.

For pointers to store addresses, we need extra space in the memory and the list’s data.

Conclusion

‘Print linked list C++’ is the article that is implemented in C++ programming language using Ubuntu operating system as an implementation tool. As the name shows, we have discussed the printing method for displaying the data inside the linked list. Moreover, first, the basics of linked list, its creation, and then implementation are explained with elementary examples. We have also described its advantages and drawbacks to help the user understand the linked list’s functionality.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.