I am just wanting a simple explanation of the linking process when pushing data onto a stack. I know how to build on using the code from my book, but I am not really sure I understand how the process works when you move the stack head link from one to the next.
For stacks like:
typedef struct node
{
void dataptr;
struct node* link;
}STRUCT_NODE;
typedef struct
{
int count;
STACK_NODE* top;
}STACK;
How do you change the link to point to the new data pushed on the stack. Also I do not know
Stacks can be implemented in various ways, but given the way you phrase your question I'm assuming your stack is just a linked list, something like
"when you move the stack head link from one to the next" the picture just changes to:
Of course A is not reachable any more in this graph, so you'd better have another pointer to it somewhere (be it only to dispose of it), but this is the gist how how the stack is popped (by making
head = head->next
if each node in the stack is astruct node
with anext
field that's astruct node*
, and of coursehead
is astruct node*
as well).That's for popping something off the stack (and you should free the memory used by
A
in that case). In detailed steps, it would be:1/ Saving the old head.
2/ Adjusting the head.
3/ Returning the old head (pointed at by
old
).If instead you're talking about pushing something onto the stack, that's an operation that involves:
1/ Creating a new element.
2/ Pointing it towards the current head
3/ Adjusting the head to point to it.
Given your data structures representing a node on the stack, and the actual stack itself:
you would initialise a stack as follows:
This basically gives you an empty stack. I'm going to use
top
to decide if the stack is empty - you could use eithercount
ortop
(as0
orNULL
respectively) to do that job but it's a good idea to choose one and stick with it in case you ever write some buggy code in future where the cached count and actual count get out of step :-)To push a node onto the stack, it's a simple operation of:
The following code shows how you can do it:
This will push onto either an empty stack or a populated one. The edge case of an empty stack will see
newNode.link
set toNULL
, thenmyStack.top
set tonewNode
, which is the correct behaviour.To pop a node off the stack:
which , translated to code, is:
This returns either the address of the popped node, or
NULL
if the stack was empty.The whole set of operations could be encapsulated as follows. Hopefully the comments will make it self-explanatory in conjunction with the comments above but feel free to raise any concerns and I'll address them with an edit.
The reason I've insisted that a stack must be empty before deleting is because the code doesn't know for certain what the payload is. It could be a simple pointer to a block of memory (shallow) in which case you could just use:
but, if it's a pointer to memory holding other pointers to memory, it's more problematic to automatically free the memory it references (it would probably be best done as a callback to the caller of the API since it would be the only beast that knew for sure how to properly free the memory). Far simpler to make that a pre-requisite on the caller up front.
Say you have some STACK called my_stack, and a STACK_NODE called my_node. To add my_node to my_stack, do this: