Page 111 - DCAP407_DATA_STRUCTURE
P. 111

Data Structure



                                          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 add_beginning(int data)
                                          {
                                          struct node *temp;
                                          temp=(struct node*)malloc(sizeof(struct node));
                                          temp->value=data;
                                          temp->link=START;
                                          START=temp;
                                          }
                                          void add_after(int data,int pos)
                                          {
                                          struct node *temp,*q;
                                          int i;
                                          q=START;
                                          for(i=0;i<pos-1;i++)
                                          {
                                          q=q->link;
                                          if(q==NULL)
                                          {
                                          printf ("\n\n There are less than %d elements",pos);
                                          getch();
                                          return;
                                          }
                                          }
                                          temp=(struct node*)malloc(sizeof (struct node));
                                          temp->link=q->link;
                                          temp->value=data;
                                          q->link=temp;
                                          }
                                          void Display()
                                          {
                                          struct node *q;
                                          if(START == NULL)
                                          {
                                          printf ("\n\nList is empty");
                                          return;
                                          }
                                          q=START;
                                          printf("\n\nList is : ");
                                          while(q!=NULL)
                                          {
                                          printf ("%d ", q->value);
                                          q=q->link;
                                          }




                          104                     LOVELY PROFESSIONAL UNIVERSITY
   106   107   108   109   110   111   112   113   114   115   116