#include
using namespace std;
class node{
public:
int data;
node*next;
node(int d)
{
data=d;
next=NULL;
}
};
int length(nodehead)
{
int count{0};
while(head!=NULL)
{
count++;
head=head->next;
}
return count;
}
void insertatend(node&head,int d)
{
node*tail=head;
if(head==NULL){
head=new node(d);
return;
}
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(d);
return;
}
void insertatmiddle(node*&head,int d,int p)
{
if(p==0 or head==NULL){
insertathead(head,d);
}
else if(p>length(head)){
insertattail(head,d);
}
else
{
node*temp=head;
int i{0};
while(i<p){
temp=temp->next;
i++;
}
node*n=new node(d);
n->next=temp->next;
temp->next=n;
}
}
void insertathead(node*&head,int d)
{
if(head==NULL)
{
head=new node(d);
return;
}
node*n=new node(d);
n->next=head;
head=n;
}
void print(node*head)
{
while(head!=NULL)
{
cout<<head->data<<"->";
head=head->next;
}
cout<<endl;
}
int main()
{
node*head=NULL;
cout<<(“hello world\n”)<<endl;
insertathead(head,30);
insertathead(head,20);
insertathead(head,10);
insertathead(head,0);
print(head);
insertatmiddle(head,15,2);
insertatend(head,40);
print(head);
return 0;
}