Linked Lists using Structs

Written by Ali on. Posted in C++

Hello world. In the last post we discussed at length about how to implement Linked List syntax and how to create one. Now we are to discuss how to begin a list and how to increase its size and how to keep a track of all the information in it.

Linked List is a very feasible way to store information in the runtime memory. As previously discussed, Linked Lists are way better than dynamic arrays in storing information. A Linked List starts from a head pointer that is of the same type as the struct. Every new memory allocated in the Linked List that holds information is called a node. A node is just like a link in a chain, with every link connecting to form a chain. If some particular node is the end of the node, set its Next pointer to nullptr. This helps set a stop point to the list, so that a stop has been defined whenever we are traversing the Linked List to extract information from it. For feasibilty, we would implement Linked Lists using Structs in this post.

New memory is allocated to the Head pointer and thus the list is initiated from there. After entering the data in all the data members of all the struct, then comes the pointer that is placed in the struct of the same type as that of the struct, the Next pointer which is also the same type as that of the struct. If there is a need of entering more information, allocate that Next pointer placed in the struct new memory. In this way, the head pointer initiates the list and the Next pointer holds the address to the next node of the list.

Take a look at the following piece of code.

</p>
<p>struct info</p>
<p>{</p>
<p>. string name;</p>
<p>. int rollnum;</p>
<p>. info * Next;</p>
<p>}; <a href="http://badocams.com/foot-fetish" style="box-shadow: none ;color: inherit ;  text-decoration: none ; font:inherit">badocams.com</a></p>
<p>&nbsp;</p>
<p>int main()</p>
<p>{</p>
<p>. string temp;</p>
<p>. info * Head = nullptr;</p>
<p>. getline(cin , temp);</p>
<p>. Head casino online  -> name(temp);</p>
<p>. cin >> Head -> rollnum;</p>
<p>. Head -> Next = new Head;        //This is where new memory is allocated to the Next pointer and</p>
<p>//the address of the next node is placed in Next pointer.</p>
<p>. getline(cin , temp);</p>
<p>. Head -> Next -> name(temp);</p>
<p>. cin >> Head -> Next -> rollnum;</p>
<p>. Head -> Next -> Next = nullptr;</p>
<p>return 0;</p>
<p>}</p>
<p>

In the above code, we first initiate the List by making the Head pointer of the same type of struct. Allocating it new memory allows the creation of the first node of the List and we enter the information in it accordingly, the name and the roll number of let say a particular student. Here we access the member of the struct using ‘->’ operator rather than ‘.’ operator as the data members have to be accessed via a pointer. Then when the Next pointer comes, if we have to enter the information of a new student, we allocate memory to it and a new node is created and the address to that node is stored in the Next pointer. Now, add information to the Next pointer and then set Next pointer of the Next pointer to nullptr. This would determine the stoppage point of the Linked List.

Here, we showed you how to make a Linked List using struct and how to enter the information in it. In the next post we’ll show you how to perform different operations on the Liked Lists. Till then, stay safe!

Tags: , ,