Computer Concept & Programming In C - Unit IV - 17

   printf("\nInsert after the i-th node");
   printf("\nEnter the position of the node counting from 0. \n");
   scanf("%d",&position);
   printf("\nEnter the number to be inserted \n");
   scanf("%d", &value);
   insert_after_i_node(&head, position, value);
   traverse_list(&head);
   break;

    case 2: value = NULL;
   printf("\nInsert at last");
   printf("\nEnter the number to be inserted at the last");
   scanf("%d", &value);
   insert_at_last(&head, value);
   traverse_list(&head);
   break;

    case 3: value = NULL;
   value1 = NULL;
   printf("\nEnter the value after which to insert a node.\n");
   scanf("%d", &value);
   printf("\nEnter the number to be inserted.\n");
   scanf("%d", &value1);
   insert_after(&head, value, value1 );
   traverse_list(&head);
   break;

    case 4: return_value = NULL;
   position = NULL;
   printf("\nDelete after i-th node");
   printf("\nEnter the position of the node counting from 0.\n");
   scanf("%d", &position);
   return_value = delete_after_i_node(&head, position);
   if(return_value != NULL)
     printf("\nThe value of node deleted is %d\n",return_value);
   traverse_list(&head);
   break;

    case 5: value = NULL;
   printf("\nEnter the value which you want to delete from list.\n");
   scanf("%d", &value);
   delete_node(&head, value);
   traverse_list(&head);
   break;

    case 6: return_value = NULL;
   printf("\nDelete at start");
   return_value = delete_at_start(&head);
   if(return_value != NULL)
     printf("\The value deleted from head is %d\n", return_value);
   traverse_list(&head);
    break;

    case 7: return_value = NULL;
   printf("\nDelete the last node.");
   return_value = delete_at_last(&head);
   if(return_value != NULL)
     printf("\nThe value deleted at last is %d\n", return_value);
   traverse_list(&head);
   break;

    case 8:  position = NULL;
    value = NULL;
    printf("\nTo a search node by its value.");
    printf("\nEnter the number you want to search.\n");
    scanf("%d",&value);
    position = search_node(&head, value);
    if(position != NULL)
      {
printf("\n Number is present at %d location.", position);
printf("\n Index starts from 0.");
      }
     else
      {
if(head -> info != value)
 printf("\n Number is not present in the list\n");
      }
    break;

    case 9: traverse_list(&head);
    break;

    default: flag = 1;
    break;
}/* end switch case*/
}/* end main_menu  */

void insert_at_begining(struct node **h, int num )
{
  struct node *new_node = NULL;
  new_node = create_node();
  new_node -> info = num;
  new_node -> next = NULL;
  if((*h) == NULL)
    (*h) = new_node;
  else
  {
    new_node -> next = (*h);
    (*h) = new_node;
  }
}

void insert_after_i_node(struct node **h, int pos, int num )
{
  int i;
  struct node *temp = NULL, *temp1 = NULL, *new_node = NULL;
  if((*h) != NULL)
  {
    temp = (*h);
    temp1 = temp -> next;
    for(i = 0; i < pos; i++)
       {
if(temp -> next == NULL)
  break;
temp = temp -> next;
temp1 = temp -> next;
       }
     if(i == pos)
      {
new_node = NULL;
new_node = create_node();
new_node -> info =  num;
temp -> next = new_node;
if(temp -> next == NULL)
new_node -> next = NULL ;
else
new_node -> next = temp1;
       }
      else
       printf("\n Invalid position in the list");
   }
 else
 printf("\nList is empty.Use Insert at beginning option");
}