Usually the first data structure that is taught is the linked list. This is because it is simple and easy to implement. In order to create a linked list it is necessary to understand pointers. (The Pointers lesson is located in the archive) A linked list can be as large or small as the programmer wants it to be; of course memory size has to be taken into consideration. A linked list is individual nodes chained together by pointers. Each node can contain any number of fields of information depending on the structure. The following is an example of defining the structure of a linked list.

Adding a node to a list is easiest to do at the end of the list. Creating the new node can be done by using the following example.

This creates a new node and sets the values of each field in the node equal to a value that would be input from the terminal or from file. Traversing the list and deleting a node from the linked list is more difficult and will be discussed in later posts.
No comments:
Post a Comment