![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Steps
Comments Now we can discuss details of the linked list implementation and the difference in using classes and structures. We used a C++ structure to create a single element: struct llink { int elem; llink* nextelem; }; The linked list element consists of two parts of different data types. Therefore, it was very convenient to combine them using the struct keyword. Remember that we cant use arrays for creating a data aggregate of pieces of different data types. The first structure member handles the data (sometimes called satellite data). In our example, the data has an integer value but linked lists can maintain any type of data, for instance, pointer to image structures. The second structure member provides a pointer to the next list element. Note that we use a kind of recursion because the pointer points to an element of the llink type that is defined by the structure. This is an important feature that makes C++ structures really powerful. So far we have dealt with data and the data describes a linked list element. Do we need to extend it to a class? The answer is no; there is no need to add operations. Elements dont need operations on themselves. There is no such thing as incrementing a list element or the addition of two elements. Even display functions should display the whole list rather than a separate element. Because we want to use a common approach, we will leave the structure without moving it to a class. This is mostly a question of programming style. For historical reasons, we deal with structures if we deal with pure data and if there is no need of operations on this data. Now we can consider the linked list itself. No doubt, it should be a class for several reasons. First, we can encapsulate all data members. Fortunately we dont need many of those. The important thing is that we have to define operations with this linked list. There are list elements involved in the operations; however, the elements are hidden from external objects. Creating a linked list involves quite a bit of logic. In our design we decided that an empty list was just a NULL. It means that we reserve the space for the list on-the-fly while adding the elements. Therefore (if we were writing a real program), we have to destroy the memory items when we dont need the elements. These operations need a constructor and a destructor that are class member functions. The last class elements we have to specify are the functions AddElement and DisplayList. The functions provide a necessary interface for the encapsulated data. We definitely need more functions and their creation is left as a good assignment for the reader.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|