bartoli wrote: |
---|
For the Kth element, you would simply look K times at the elements next element. K is a number |
voteboo wrote: |
---|
The former code of the code I posted above did that bartoli |
If you're referring to the code you posted here, then no it doesn't. I see functions for deleting the smallest, or the largest, or deleting some entry in the list matching an input, but I don't see any that delete the Kth element.
I don't know if by Kth element he's(author of the book) referring to the "largest element" or "smallest element"??? |
He's referring to neither. He's referring to
the Kth element from the front of the list.
Let's say the list has 5 elements in it.
node1 -> node2 -> node3 -> node4 -> node5 -> (null)
Here,
K can be any number from 1 to 5. e.g. Deleting the Kth element (where
K==1), would be
deleting node1 from the list (see the list above). e.g. Deleting the Kth element (where
K==3) would be
deleting node3 from the list.
I need some code to show how to |
On this board, we don't give out full solutions to problems, only explanations to help get through any roadblocks or misunderstandings. If we were to show you the code for this, it would be giving you the full solution. But, hopefully you should be able to figure it out from here. To reiterate, the "Kth element" means the Kth element in the list from the front. What you need to do is start at the head of the list. If K==1, you've found the element to delete. Otherwise, check the link. If it's not null, set the current node to the next. Stop when you've done K-1 moves or if the next node is null. If you've reached a null entry before reaching the Kth node, then the list
does not have a Kth element. Otherwise, after K-1 moves you've reached the Kth node, then all you have to do is delete the node.