I just started learning C a few weeks ago, and I think I know what you're asking about dynamically allocating memory.
Because everything in Java is done on the fly, you don't need to declare all of your variables before you use them.
For instance, in C you would do something like this:
CODE
#include <stdio.h>
int main()
{
int x;
int y = 10;
int z = x + y;
for(x = 0; x < y; x++)
printf("x + y = %d", z);
}
In java, you can just declare all of these as you go:
CODE
public static void main(String[] args)
{
int y = 10;
for(int x = 0; x < y; x++)
{
int z = x + y;
System.out.println("x + y = " + z);
}
}
So as a roundabout way of answering your question, Java dynamically allocates memory on its own.
So, whenever you need to add a node in your chain for the stack, all you need to do is create a new node with your given constructor, then declare
CODE
someOtherNode.next = newNode;
and this will progressively make your stack larger.
Likewise, when you remove a node from the chain, all you need to do is make sure that nothing is pointing to it. Because you're using a stack, I assume that you'd only be worried about removing the last node in the chain, so a good way to do this would be something like:
CODE
secondToLastNode.next = null;
Which would make secondToLastNode the last node in the chain. After this is done, the newly discarded node will be trashed by Java's automatic garbage collection (so you don't have to call free() like you would need to in C).
I hope that this was what you were looking for. Feel free to post any more questions and I'll do my best to help.
This post has been edited by keems21: 19 Mar, 2007 - 08:04 PM