Linked Lists: An Introduction

Written by Ali on. Posted in C++

There are many ways provided by the C language to store information. The most basic of them are variables of multiple types i.e. int, float, char etc. But when we require storing the information of a lot of such variables, the language has provided us a facility of arrays. Arrays are the most basic means of storing very large amount of data, and the sildenafil vs viagra language allows us the facility to manipulate the data in those arrays in any way we like. But arrays are static and if we need to allocate data on the run time, we use dynamic arrays to store space and allocate memory on the system as per our use. Dynamic arrays are helpful in many scenarios in this language, but sometimes the management of these dynamic arrays can turn to be an issue. There is one more way that C language allows to store information through, the use of Linked Lists.

Linked Lists form the basis of large data structures and are very effective in manipulating and managing the data according to the user need and requirements. These Linked Lists are also dynamically allocated but the most important feature of these is the way the whole data is connected to each other; that’s right, in the form of a connected chain, or List. Having a list that is dynamically allocated and contains all the sort of information you want to input can be really helpful in managing large amounts of data.

The main difference between Linked Lists and the dynamic arrays is the level of accessibility and ease of access nbso and storage allowed by them. Let’s consider an example of a struct to help us understand the concept more easily.

</p>
<p>struct information</p>
<p>{</p>
<p>. int rollnumber;</p>
<p>. string name;</p>
<p>};</p>
<p>&nbsp;</p>
<p>

With this example, if you have to store the information, of let’s say students in a particular society (usually the one that has enrolling of possibly hundreds of students), the use of dynamic arrays can be a menace if you don’t plan it. You would have to first input data and then store it into a dynamically allocated space for the first entry. However for the successive entries, you’d have to plan carefully on how to increment size of the array dynamically and according to the size of the information. Also you’d have to release the unwanted space in the due course so as not to run out of space, crashing your program straight away.

The Linked Lists don’t work that way. In fact it is much more feasible to store and connect the pieces of information than in dynamic arrays. For Linked Lists, the pointer to that struct would be placed as a data member of that struct. In this way, you can enter the information as per your requirement in the roll number and name fields. However, when the pointer field comes, all you need to do is to allocate memory to that pointer if you have the need to input the information of another student. All you need to take care of is the Head pointer that is the pointer from where the List starts. Take a look at the following code for the syntax of a Linked List.

</p>
<p>struct information</p>
<p>{</p>
<p>. int rollnumber;</p>
<p>. string name;</p>
<p>. information * Next;</p>
<p>};</p>
<p>

Linked Lists can be made from two different ways. One is, that is more easy and suitable for beginners, is that you make a Linked List from structs. Its feasibility lies in the access of the pointer placed in the struct that’d be allocated new memory. In this method, the pointer can be accessed by any function whether member function or not. The other method of making Linked Lists is by way of making a class and placing the pointer as a data member of that class. In this method, proper care and thinking has to be catered in how to manage and access the data and the pointer and how to traverse through the list in order to access all the information.

That’s all for this time folk, we’ll be back soon to load you up more with the Linked Lists. Stay Safe!

Tags: , , ,