Quick actions

cmd+k|ctrl+k

Navigation

Languages

inserting at a particular pos(linked list)

Snippet info

Language

Cpp

Visibility

public

Author

gsubhikaran

Created

2019-05-24T12:43:11Z

Updated

2019-05-24T12:43:11Z

#include <iostream>
using namespace std;
struct node{
    int x;
    node *next;
};
node* insert(node *head,int value)
  {
      node *temp=new node;
     if(temp!=NULL)
     {
      temp->x=value;
      temp->next=head;
      head=temp;
      return head;
     }
  }
  void display(node **temp)
    {
        node *ptr= *temp;
        while(ptr!=NULL)
         {
             cout<<ptr->x<<endl;
             ptr=ptr->next;
         }
    }
 void insert(node **temp,int y,int z)
   {  int j=0;
       node *ptr= *temp;
       node *ptr1=new node;
      
       while(ptr!=NULL)
       {
           j++;
           if(j==y-1)
             { node *o=new node;
               o->x=z;
               ptr1=ptr->next;
               ptr->next=o;
               o->next=ptr1;
               break;     
             }
             ptr=ptr->next;
       }
       
   }
int main() {
    node *head=NULL;
    int n;
    int m;
    cin>>n;
    int i;
    for(i=0;i<n;i++)
     {
          cin>>m;
          head =insert(head,m);
     }
     int position;
     cin>>position;
     int n1;
     cin>>n1;
     insert(&head,position,n1);
     display(&head);
     
    
    return 0;
    
}
INFO