Page 89 - DCAP407_DATA_STRUCTURE
P. 89

Data Structure



                          5.3.1   Singly-Linked List
                          As the name suggests, a singly-linked list consists of only one pointer that points to another node. It is
                          also known as a linear list because the last node in a singly-linked list is assigned a NULL value and
                          hence does not point to any other node. The first node in the list is known as a HEAD or first node.
                          Figure 5.3 depicts a singly-linked list. The HEAD Node is a dummy node pointing to Node1. Node1
                          holds the address of Node2, and Node2 holds the address of Node3. Node3 points to NULL to indicate
                          that there are no additional nodes present in the list.
                                                  Figure 5.3: Representation of a Singly-linked List



                                              Node1                   Node2                 Node3


                            HEAD          200                    400                    600       NULL
                            Node                   Address                Address of
                                                   of Node2               Node3


                                              The program shows the implementation of a singly-linked list consisting of
                                              four nodes. The program displays the value present in each node.

                                              The program flow is depicted below:


                                              1.   Creating node1 with value 200

                                                     New_list
                                                                      200

                                              2.   Creating node2 with value 400

                                                     New_list

                                                                      200       400

                                              3.   Creating node3 with value 600

                                                     New_list

                                                                      200       400      600

                                              4.   Creating node4 with value 800


                                                     New_list

                                                                      200       400      600      800









                          82                      LOVELY PROFESSIONAL UNIVERSITY
   84   85   86   87   88   89   90   91   92   93   94