implement the function deleteAfterKey in unordered singly circular linked list
QUESTION
An unordered singly circular linked list
contains integer keys and has no sentinel nodes. The structure of the node, and
the skeleton of class LinkedList is given below:
struct Node
{
int key;
Node * next;
};
|
class LinkedList
{
Node * head;
public:
...
bool deleteAfterKey(int m);
};
|
You have to implement the function deleteAfterKey which finds the node containing
key equal to m (passed as parameter),
and deletes the node as well as the next m-1
nodes from the list (we consider the list to end before the head node). So if m=5, it will find the node containing 5,
then delete that node as well as the 4 nodes that come after it. If there are
less than m-1 nodes after the node
with key=m, all these will be
deleted.
For example if the list is:
2-> 9-> 4->3->1->2->5 then deleteAfterKey(3) would result in 2->
9-> 4->5 and
if the list is: 2->9-> 4->6->1->2->5 then deleteAfterKey(6) will result in:
2-> 9-> 4
If there is no node in the list with key=m,
the function simply returns false. If you use any helper function then
implement it also. Make sure you handle
all cases.
SOLUTION
Comments
Post a Comment