Page 119 - DCAP407_DATA_STRUCTURE
P. 119

Data Structure



                              exit
                          6.   Assign q=q->link
                          7.   To delete the last element, check if(q->link->data) is equal to data. If true, then execute the
                              statements in step 8.
                          8.   temp=q->link
                              free the deleted temp node
                              q->link=NULL
                              exit
                          9.   Display entered element not found

                          10.  Exit.

                                            The following program deletes a node following the given node:

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

                                            void createlist(int);
                                            void delete(int);
                                            void Display();

                                            struct node
                                            {
                                            int value;
                                            struct node *link;
                                            }*START;

                                            void createlist(int data)
                                            {
                                            struct node *q,*temp;
                                            temp= (struct node*)malloc(sizeof(struct node));
                                            temp->value=data;
                                            temp->link=NULL;
                                            if(START==NULL)
                                            START=temp;
                                            else
                                            q=START;
                                            while(q->link!=NULL)
                                            q=q->link;
                                            q->link=temp;
                                            }

                                            void delete(int data)
                                            {
                                            struct node *temp,*q;
                                            if (START->value == data)
                                            {
                                            temp=START;
                                            START=START->link; //to delete first element
                                            free(temp);
                                            return;




                          112                     LOVELY PROFESSIONAL UNIVERSITY
   114   115   116   117   118   119   120   121   122   123   124