Page 532 - Computer science 868 Class 12
P. 532

Java Code
                    void deleteMid(Node start ,int pos)
                    {
                    int c=0;
                    Node ptr= new Node(start);   // initialise pointers temp and ptr to start
                    Node temp= new Node(start);
                    while(c<pos-1)                    //  move pointer to the given position
                    {
                    temp=ptr;                         // temp pointing to the previous node
                    c++;
                    ptr=ptr.next;                     // ptr is pointing to the current node
                    }
                    temp.next=ptr.next;               // address of previous node changed to next node
                    ptr.next=null;                    // breaking the link of the current node
                    }
                7.   Write an algorithm/Java code to delete a node from the end of an existing linked list using method: void
                    deleteEnd(Node start) where start refers to the start pointer.

                                   header 1001




                                   29     1098 1156             18     1156 NULL              35       NULL
                                        1001                         1098                          1156
               Ans.  Algorithm
                    Step 1:  Start.

                    Step 2:  Create a new Nodes ptr and temp and initialise them to start
                    Step 3:  Repeat Step 4 to Step 5 while ptr.next != null

                    Step 4:  Assign temp = ptr
                    Step 5:  Assign ptr = ptr.next

                    Step 6:  Set temp.next= null

                    Step 7:  Stop.
                    Java Code

                    void deleteEnd(Node start )
                    {
                    Node ptr= new Node(start);  // initialise pointers temp and ptr to start
                    Node temp= new Node(start);
                    while(ptr.next !=null)            // move pointer to last node
                    {
                    temp=ptr;                         // temp pointing to the previous node
                    ptr=ptr.next;                     // ptr is pointing to the current node
                    }
                    temp.next=null;                   // address of previous node changed null and link broken
                    }


                530530  Touchpad Computer Science-XII
   527   528   529   530   531   532   533   534   535   536   537