inserting at a particular pos(linked list)

Run Settings
LanguageC++
Language Version
Run Command
#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; }
Editor Settings
Theme
Key bindings
Full width
Lines