I am having trouble walking through a linkedlist starting at the first node and moving to the last node while keeping track of thecurrent node and the previous node. Then once I find the last node I want to set it to null but I only want to delete the last node when a certain amount of elements have bee pushed onto the dropstack which is decided by the user. This is what I have so far.
I can not use an iterator to move through the linkedlist and I am trying to walk through the linked list in the push method so that when the count of the number of elements in the linkedlist stack is for example at 3 and the size of the linkedlist stack is 3 that the last node is deleted and the new element is pushed onto the top of the linkedlist stack. I have been stuck on this for a couple days now so any help would be awesome. Thanks.
Welcome to the Ranch, both of you.
TC: draw a diagram of your List on paper. Put arrows on each of the nodes showing how they connect to one another. Then see how you can iterate the List. If it is a stack, then it is usually a singly‑linked list, which you can only iterate one direction. So finding the bottom node means iterating the whole stack. It is finding it that is hard, not deleting it.
Get that working, without any count or anything. You now have a deleteLastNode() method.
Now add a deleteLastNodeIfSizeOver(int size) method, which can simply use an if statement to decide whether to call that other method. Give those methods either boolean return type, or return the E you have just deleted. Beware: if the size is small, those methods might return null.
By the way: it is not normal behaviour for a stack to delete its bottom element. You usually only access a stack from its top.
Why have you created that Exception class when there is one already in existence?
You are right there. If the stack is A->B->C->D-> you need to get down to D to find out that it is the last node and null the next pointer in C. However, at that point you have lost any reference to C since there is no "prev" reference in D. So you need to save a previous node. You have a previous node, you set it, and you null the "next" pointer in previous. The problem is that you save to previous after the loop. But after the loop all you have is D to save.
If the value passed to the DropStack constructor is the maximum number of elements, than your logic should change as you are not treating it like it is the maximum amount of values to store.
There is another issue related to count in DropStack.