Page 110 - DCAP407_DATA_STRUCTURE
P. 110

Unit 6: Linked List Operations



                               4.  The switch statement is used to implement the three operations. The details
                                   of the actions performed in each switch case is as follows:
                                   (a)  Case 1  and case 2 call the function  display()  to display  the elements
                                       present in a list or to display a message “linked list is empty” in case
                                       there are no elements.
                                   (b)  Case 3 prompts the input from the user to insert a new node at the
                                       beginning of the list  by calling insert_beg() function and also calls the
                                       display() function to display the list.
                                   (c)  Case 4 results in the exit of the program.
                                   (d)  The  default  prints  a message “your choice is wrong” in case the user
                                       enters input greater than 4.
                               5.  The program terminates when the user enters 4.

               6.3.2   Inserting a Node after a Given Node
               Let us consider START as the initial position in a list, data as the input entered by a user, pos as any
               position in the list where the data will be inserted, and q and temp as the temporary pointers to hold
               the node address.
               Algorithm to insert a new node after a given node is as follows:
               1.   Input the data

               2.   Specify the pos where the data is to be inserted
               3.   Initialize q to START.
               4.   If  q  is equal  to  NULL, then  print “there are less elements present in the list than  the entered
                    position number”
               5.   for (i=0;i<pos-1;i++). q=q->link;

                    temp=(struct node*)malloc(sizeof (struct node));
                    temp->link=q->link;
                    temp->value=data;
                    q->link=temp;
               6.   Repeat step 5 until i  value is not less than pos-1
               7.   Exit.


                               The following program inserts a node after a given node:

                               #include<stdio.h>
                               #include<conio.h>
                               #include<malloc.h>
                               #include<process.h>

                               void createlist(int);
                               void add_beginning(int);
                               void add_after(int, int);
                               void Display();
                               struct node
                               {
                               int value;
                               struct node *link;
                               }*START;



                                        LOVELY PROFESSIONAL UNIVERSITY                          103
   105   106   107   108   109   110   111   112   113   114   115