[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
We talked about the queue. Today we will discuss another data structure: Stack. A stack is almost the lifeblood of program design. Without a stack, there is no function call, and of course there is no software design. What are the special attributes of the stack? In fact, the attributes of the stack are mainly manifested in the following two aspects:
(1) The stack data is first in and then out
(2) The stack length depends on the height of the stack top.
So how should we design a stack as a continuous memory type? You can give it a try first:
(1) design stack nodes
Typedef struct _ STACK_NODE
{
Int * pData;
Int length;
Int top;
} STACK_NODE;
Typedef struct _ STACK_NODE
{
Int * pData;
Int length;
Int top;
} STACK_NODE; (2) create a stack
STACK_NODE * alloca_stack (int number)
{
STACK_NODE * pStackNode = NULL;
If (0 = number)
Return NULL;
PStackNode = (STACK_NODE *) malloc (sizeof (STACK_NODE ));
Assert (NULL! = PStackNode );
Memset (pStackNode, 0, sizeof (STACK_NODE ));
PStackNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pStackNode-> pData ){
Free (pStackNode );
Return NULL;
}
Memset (pStackNode-> pData, 0, sizeof (int) * number );
PStackNode-> length = number;
PStackNode-> top = 0;
Return pStackNode;
}
STACK_NODE * alloca_stack (int number)
{
STACK_NODE * pStackNode = NULL;
If (0 = number)
Return NULL;
PStackNode = (STACK_NODE *) malloc (sizeof (STACK_NODE ));
Assert (NULL! = PStackNode );
Memset (pStackNode, 0, sizeof (STACK_NODE ));
PStackNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pStackNode-> pData ){
Free (pStackNode );
Return NULL;
}
Memset (pStackNode-> pData, 0, sizeof (int) * number );
PStackNode-> length = number;
PStackNode-> top = 0;
Return pStackNode;
} (3) release the stack
STATUS free_stack (const STACK_NODE * pStackNode)
{
If (NULL = pStackNode)
Return FALSE;
Assert (NULL! = PStackNode-> pData );
Free (pStackNode-> pData );
Free (void *) pStackNode );
Return TRUE;
}
STATUS free_stack (const STACK_NODE * pStackNode)
{
If (NULL = pStackNode)
Return FALSE;
Assert (NULL! = PStackNode-> pData );
Free (pStackNode-> pData );
Free (void *) pStackNode );
Return TRUE;
} (4) stack pushing data
STATUS stack_push (STACK_NODE * pStackNode, int value)
{
If (NULL = pStackNode)
Return FALSE;
If (pStackNode-> length = pStackNode-> top)
Return FALSE;
PStackNode-> pData [pStackNode-> top ++] = value;
Return TRUE;
}
STATUS stack_push (STACK_NODE * pStackNode, int value)
{
If (NULL = pStackNode)
Return FALSE;
If (pStackNode-> length = pStackNode-> top)
Return FALSE;
PStackNode-> pData [pStackNode-> top ++] = value;
Return TRUE;
} (5) stack pop-up data
STATUS stack_pop (STACK_NODE * pStackNode, int * value)
{
If (NULL = pStackNode | NULL = value)
Return FALSE;
If (0 = pStackNode-> top)
Return FALSE;
* Value = pStackNode-> pData [-- pStackNode-> top];
Return TRUE;
}
STATUS stack_pop (STACK_NODE * pStackNode, int * value)
{
If (NULL = pStackNode | NULL = value)
Return FALSE;
If (0 = pStackNode-> top)
Return FALSE;
* Value = pStackNode-> pData [-- pStackNode-> top];
Return TRUE;
} (6) count the data contained in the current stack
Int count_stack_number (const STACK_NODE * pStackNode)
{
Return pStackNode-> top;
}
Int count_stack_number (const STACK_NODE * pStackNode)
{
Return pStackNode-> top;
}
Suggestion: Stack is the basis for function calling, the basis for recursive calling, and the root cause of many problems. We suggest you have time to practice well.