Question: Write a function insert ( ) to insert a node to a linked list. A linked list contains a series of nodes and each node
Write a function insert to insert a node to a linked list. A linked list contains a series of nodes and each node holds a value and the values. Each node contains a pointer used to link to the next node. The values in the linked list are in a sorted order. The function insert will insert a node into the linked list in sorted fashion. A linked list starts with a pointer to the first node. If the pointer points to NULL, then it is an empty list. Complete the following program by implementing the insertfriend function. The output of the program is
class Node
public:
Node;
Nodeint val;
int getValue;
Node getLink;
void setLinkNode &nd;
friend void insertNode &head, Node nd;
friend ostream& operator ostream &ost, Node head;
private:
int value;
Node link;
;
Node::Node : value linkNULL
Node::Nodeint val : valueval linkNULL
Node Node::getLink
return thislink;
int Node::getValue
return thisvalue;
void Node::setLinkNode &nd
thislink &nd;
void insertNode &head, Node nd
Implement insert function
ostream & operator ostream &ost, Node head
Node cur head;
while cur NULL
ost curvalue;
cur curlink;
return ost;
int main
int data;
Node nd;
Node head NULL;
for int i ; i ; i
nd new Nodedatai;
inserthead nd;
cout head endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
