In: Computer Science
Show the code how to add a node to the front of a list.
Draw the picture also that goes with the code.
NOW
Show the code how to add a node to the end of a list.
Draw the picture also that goes with the code.
EXTRA CREDIT
Show the code how to add a node to a specific spot in a list.
Draw the picture also that goes with the code.
Linked list definition
struct Node
{
char data;
Node *next;
};
Adding the new node in front of the list
void insert(Node *Head)
{
Node *y =new Node();
cout<<" Enter the character";
cin>>y->data;
y->next=NULL;
y->next=Head;
Head=y;
}
Adding the new node at the end of the list
void insert(Node *Head)
{
Node *y =new Node();
cout<<" Enter the character";
cin>>y->data;
y->next=NULL;
Node *a= Head;
while(a->next!= Null)
a=a->next;
a->next=y;
}
Adding the new node after a specific Node
void insertAfter(Node* prev_node)
{
if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL";
return;
}
Node *y =new Node();
cout<<" Enter the character";
cin>>y->data;
y->next=NULL;
y->next = prev_node->next;
prev_node->next = y;
}