//#include<stdio.h>
//#include<stdlib.h>
#include<iostream>
void Insert(int data, int n);
void Print();
typedef struct Node{
int data;
Node * next;
}Node;
Node * head;
int main()
{
head = NULL;
Insert(2, 1);
Insert(3, 2);
Insert(4, 2);
Insert(5, 3);
Print();
return 0;
}
void Insert(int data, int n)
{
Node *newnode = new Node();
newnode->data = data;
newnode->next = NULL;
if(n == 1)
{
newnode->next = head;
head = newnode;
return;
}
//find n-1
Node *oldnode = head;
for(int i =0; i<n-2; i++)
{
oldnode = oldnode->next;//find n-1
}
newnode->next = oldnode->next;
oldnode->next = newnode;
}
void Print()
{
Node *temp = head;
printf("List is:");
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}