Page 527 - Computer science 868 Class 12
P. 527

Disadvantages of linked list
                 1.  As linked list has two parts: the data part and the link part, it requires more memory to store the elements.
                 2.  In order to access an element in a linked list, we have to traverse its predecessors. In an array, any particular location
                   can be accessed directly.
                 3.  Traversing in a linked list in the reverse direction is even more difficult because it requires more memory for the
                   pointer.

                 13.3.1 Basic Operations on a Single Linked List
                 The basic operations that can be performed on a single linked list are as follows:
                 1.  Creation of a new linked list
                 2.  Inserting a node in an existing linked list at the
                   •  Beginning
                   •  End

                   •  Any specified position
                 3.  Deletion of a node from an existing linked list from the
                   •  Beginning
                   •  End
                   •  Any specified position
                 4.  Traversing or displaying the nodes of the linked list from the beginning to the end
                 5.  Searching a specific node from the linked list
                 6.  Joining or concatenating two linked lists
                 7.  Splitting a linked list into two parts

                 To perform the above operations on a linked list, we must first learn how to define a node in a linked list.
                 Let us define a linked list called Node which stores an integer number data and an address of the next node as
                 follows:

                    class Node
                    {   int  data ;   // stores the value of the current node
                        Node next;    // stores the address of the next node
                    }
                   1.  Write an algorithm/Java code to create a linked list containing ‘n’ nodes using method:
                      void createList(int n)
                  Ans.  Algorithm

                      Step 1:   Start.
                      Step 2:   Create a new pointer start, ptr of type Node

                      Step 3:   Accept n from the user
                      Step 4:   Accept any number in num

                      Step 5:   Assign ptr.data=num and ptr1.next=null
                      Step 6:   Assign start=ptr

                      Step 7:   Initialise counter i to 1
                      Step 8:   Repeat steps while i < n





                                                                                                                       525
                                                                                                       Data Structures  525
   522   523   524   525   526   527   528   529   530   531   532