Linked Lists using classes

Written by Ali on. Posted in C++

Hello everyone and we’re back again with yet another post on Linked Lists series. This time, we introduce to you a much wider concept of linked lists that is more feasible and easy to implement; implementation of Linked Lists using classes.

Via classes, the Linked Lists get a whole new dimension to their implementation. The creation and access becomes way lot easier and the work can be all protected as the data members can be declared private. The similar concept can also help in implementation of a rather larger and safer group of data than that can be implemented via structs. A separate class of Head pointer can be made that can allow the formation of a completely new Linked Lists anytime during the program.

As we have seen earlier, the getters and setters can be made of the class to further assist in our work with Linked Lists. The work gets broken down with the help of member functions, each doing a little job of its own, assisting through our work collectively. Take a look at a possible implementation below.

</p>
<p>class List</p>
<p>. {</p>
<p>. private:</p>
<p>. int num;</p>
<p>. List * Next;</p>
<p>. public:</p>
<p>. int getnum();</p>
<p>. void setnum( int );</p>
<p>. void setptr( List * null );</p>
<p>. List*&amp; nextptr();</p>
<p>. void print( List * );</p>
<p>. void addnode( int , List *&amp; );</p>
<p>. bool deletenode( List *&amp; , int );</p>
<p>. bool updatenode( List *&amp; , int );</p>
<p>. };</p>
<p>

Over here, we have set a single casino online data member for our convenience of understanding the concept. The getter and setter functions of the num allow us to set the value of num accordingly. The getter and setter of Next provide easy way of accessing the Next pointer as it is variably used in Linked Lists.

Here, far as convenience is concerned, we can simply make an object of the class list and via the usage of data member the addition and deletion of node in the List can be no big deal at all, and that all be at a much safer scale.

This is just to give you just a twitch of how the work can be with simplified using the encapsulation technique of the C language. Stay tuned for our next post.

Tags: , ,

Trackback from your site.

Comments (1)

  • Sarah

    |

    I am forever inebdetd to you for this information.

    Reply

Leave a comment