#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
void nodeathead(node* &head, int val)
{
node* p=new node(val);
p->next=head;
head=p;
}
void nodeatlast(node* &head,int val)
{
node* l=new node(val);
if(head==NULL)
{
head=l;
return;
}
node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=l;
}
void nodeafter(node* &head,int key,int val)
{
node* l=new node(val);
node* temp=head;
while(temp->data!=key)
{
temp=temp->next;
if(temp==NULL)
{
return;
}
}
l->next=temp->next;
temp->next=l;
}
void print(node* &head)
{
node* l=head;
while(l!=NULL)
{
cout<<l->data<<" ";
l=l->next;
}
}
int main()
{
node* head = new node(45);
nodeathead(head,9);
nodeathead(head,8);
nodeathead(head,6);
nodeathead(head,3);
nodeatlast(head,98);
nodeatlast(head,977);
nodeafter(head,6,7);
nodeafter(head,977,988);
print(head);
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter