#include
#include
using namespace std;
class node
{
public:
int data;
node *next;
node(int d)
{
next=NULL;
data=d;
}
};
void insertathead(node *&head, int data)
{
node *n=new node(data);
n->next=head;
head=n;
}
void insertatmiddle(node &head, int data)
{
nodetemp=head;
int c=0;
while(temp!=NULL)
{
temp=temp->next;
c++;
}
for(int i=0;i<(c/2);i++)
{
temp=temp->next;
}
node *n=new node(data);
n->next=temp->next;
temp->next=n;
}
void print(node *head)
{
node *temp=head;
while(temp!=NULL)
{
cout<data<<"–>";
temp=temp->next;
}
}
int main()
{
node *head=NULL;
insertathead(head, 3);
insertathead(head, 4);
insertathead(head, 5);
insertatmiddle(head, 6);
print(head);
return 0;
}
I wrote this code for insertion at middle but the output coming is 5–>4–>3–>
Code for insertion at middle
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.