One-step write algorithm (linear stack)

Source: Internet
Author: User
Tags stack pop

 

[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.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.