URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Class Discussion
  HTML https://srm.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: Data Structure
       *****************************************************
       #Post#: 26--------------------------------------------------
       Insert Node in Specific Position (Data Structure)
   DIR By: prannyll
       Date: May 13, 2019, 8:16 am
       ---------------------------------------------------------
       //This is the simplest method to insert a new node in any given
       position
       //Program tested in Turbo C++ and Dev C++
       #include<stdio.h>
       #include<conio.h>
       #include<stdlib.h>
       int ch, i, data, count = 0;
       typedef struct node{
       int data;
       struct node *next;
       }t;
       t* head;
       t* end;
       t* temp;
       void main()
       {
       clrscr();
       printf(" ADDRESS \t DATA \t NEXT");
       for(i = 0; i < 5 ; i++)
       {
       t* ptr = (t*)malloc(sizeof(t*));
       t* ptr2 = (t*)malloc(sizeof(t*));
       if(i==0)
       head = ptr;
       ptr->data = i+1;
       ptr->next = ptr2;
       if(i==4)
       {
       ptr->next = NULL;
       end = ptr;
       }
       free(ptr2);
       count++;
       printf("\n (%d) %u \t %d \t %u",i+1, ptr, ptr->data,
       ptr->next);
       }
       printf("\n Where do you want to add new node : ");
       scanf("%d", &ch);
       printf(" Enter Data");
       scanf("%d", &data);
       temp = head;
       if(ch == 1)
       {
       t* ptr = (t*)malloc(sizeof(t*));
       ptr->next = head;
       ptr->data = data;
       head = ptr;
       count++;
       }
       else if(ch >= 5)
       {
       t* ptr = (t*)malloc(sizeof(t*));
       end->next = ptr;
       ptr->next = NULL;
       ptr->data = data;
       count++;
       }
       else
       {
       t* ptr = (t*)malloc(sizeof(t*));
       for(i = 0; i < ch-2; i++)
       {
       temp = temp->next;
       }
       ptr->next = temp->next;
       ptr->data = data;
       temp->next = ptr;
       count++;
       }
       temp = head;
       printf(" ADDRESS \t DATA \t NEXT");
       for(i = 0; i < count; i++)
       {
       printf("\n (%d) %u \t %d \t %u",i+1, temp, temp->data,
       temp->next);
       temp = temp->next;
       }
       
       getch();
       }
       
       //Good Luck
       *****************************************************
       Page 1 of 1