Step-by-step write algorithm (the linear stack)

Source: Internet
Author: User

Text: Step-by-step write algorithm (linear stack)


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


We talked about the queue ahead, and today we go on to another data structure: the stack. The stack is almost the lifeblood of programming, there is no function call without a stack, and of course there is no software design. So what are the special properties of the stack? In fact, the properties of the stack are mainly shown in the following two aspects:

(1) Stack data is first in and out

(2) The length of the stack depends on the height of the stack top

So what should be the design of a stack as a continuous memory type? You can try it yourself first:

(1) Design stack node

typedef struct _stack_node{    int* pdata;int length;int top;} Stack_node;
(2) Creating 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;}
(3) releasing 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;}
(4) Stack Press-in 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;}
(5) Stack popup 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;}
(6) Count how much data is contained in the current stack

int Count_stack_number (const stack_node* pstacknode) {    return pstacknode->top;}

Recommendation: The stack is the basis of function calls, is the basis of recursive calls, is a source of many problems, it is recommended that friends usually have time to practice a good.


"Preview: The following blog introduces the contents of the linked list"


Step-by-step write algorithm (the linear stack)

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.