Page 122 - DCAP407_DATA_STRUCTURE
P. 122

Unit 6: Linked List Operations



                                 3.   Display
                                 4.   Exit
                                 Enter your choice: 3
                                 List is: 10 20 30

                                 In this example:
                                 1.   A structure named node with a data field named value and link field
                                      named *link are created.
                                 2.   Three functions are created namely, createlist(), delete(), and Display().
                                      The user has to select the appropriate function to be performed.
                                 3.   The function createlist() allocates the memory for list node and stores
                                      the elements entered by the user.
                                 4.   The delete()  function compares the data entered by the user with the
                                      START element if both are  equal then the first element is deleted.
                                      Otherwise, the elements in the other nodes are verified with the entered
                                      data and the deletion of the element takes place accordingly.

                                 5.   The Display() function displays the list after the deletion operation.
                                 6.   The program terminates when the user enters 4.



                           Write a program to delete an element at the beginning of a circular linked list.






                           Deleting a Node in a Circular Linked List
                           Assume p as first node and q as last node, link as the pointer field pointing to the next
                           node and value as the data stored in each node.
                           To delete an element at the beginning of a circular linked list, follow the statements listed
                           below:
                           p=q->link
                           q->link=p->link
                           printf(“the deleted element is %d\n”, p->value)
                           freenode(p);
                           Suppose, only one node exists in the list then assign NULL  value to  q  to indicate the
                           empty list.
                           The same is depicted in the following statements:
                           If(q->link==q) {
                           printf(“the deleted element is %d\n”, q->value)
                           freenode(q)

                           q=NULL
                           return q
                           }





                                        LOVELY PROFESSIONAL UNIVERSITY                          115
   117   118   119   120   121   122   123   124   125   126   127