An explanation of how stacks work in C

I just want a simple explanation of the binding process when pushing data onto the stack. I know how to use the code from my book, but I'm not quite sure that I understand how this process works when you move the stack header link from one to another.

For stacks like:

typedef struct node
{
    void dataptr;
    struct node* link;
}STRUCT_NODE;

typedef struct
{
    int count;
    STACK_NODE* top;
}STACK;

How to change the link to indicate new data pushed onto the stack. Also i don't know

+3
source share
3 answers

Stacks can be implemented in various ways, but given how you formulate your question, I assume that your stack is just a linked list, something like

head
A → B → C → D → 0

", , :

    head
A → B → C → D → 0

, A , - ( , ), , ( head = head->next, node struct node next, a struct node*, , , head struct node*).

, - ( , A). :

1/ .

      head
old → A → B → C → D → 0

2/ .

          head
old → A → B → C → D → 0

3/ ( old).

, - , , :

1/ .

    head
Z   A → B → C → D → 0

2/

    head
Z → A → B → C → D → 0

3/ , .

head
Z → A → B → C → D → 0
+7

, node , :

typedef struct node {
    void        *dataptr;
    struct node *link;
} NODE;

typedef struct {
    int  count;
    NODE *top;
} STACK;

:

STACK myStack;
myStack.count = 0;
myStack.top = NULL;

. top, , - count, top ( 0 NULL ), , , - , : -)

node , :

  • node (1).
  • node .
  • node (3).
  • (4).

, :

/* 1 */ NODE *newNode = malloc (sizeof (NODE)); // should check for failure.
        newNode->dataptr = NULL;
/* 2 */ newNode->link = myStack.top;
/* 3 */ myStack.top = newNode;
/* 4 */ myStack.count++;

. , newNode.link NULL, myStack.top newNode, .

node :

  • (1).
  • NULL, ( ).
  • (3).

, , :

/* 1 */ NODE *popNode = myStack.top;
/* 2 */ if (myStack.top != NULL) {
            myStack.top = myStack.top->link;
            myStack.count--;
        }
/* 3 */ return popNode;

node, NULL, .

. , , , .


// Error codes (probably should be enums).

#define STACK_ERR_OKAY       0
#define STACK_ERR_NOTEMPTY   1
#define STACK_ERR_NOPAYLOAD  2
#define STACK_ERR_NOMEMORY   3

// Structures.

typedef struct sNode {
    void         *data;   // Payload pointer.
    struct sNode *next;   // Link to next node.
} tNode;

typedef struct {
    int          count;   // Count for fast sizing.
    NODE         *top;    // First node.
} tStack;

// Make a new stack returning its pointer or NULL on failure.

tStack *stackNew (void) {
    tStack stack = malloc (sizeof (tStack));
    if (stack != NULL) {
        stack->count = 0;
        stack->top = NULL;
    }
    return stack;
}

// Delete a current stack, must be empty first.

int stackDel (tStack *stack) {
    if (stack->top != NULL)
        return STACK_ERR_NOT_EMPTY;
    free (stack);
    return STACK_ERR_OK;
}

// Push a pointer payload (no NULLs allowed) onto the stack.

int stackPush (tStack *stack, void *payload) {
    if (payload == NULL)
        return STACK_ERR_NOPAYLOAD;
    tNode *node = malloc (sizeof (tNode));
    if (node == NULL)
        return STACK_ERR_NOMEMORY;
    node->data = payload;
    node->next = stack->top;
    stack->top = node;
    stack->count++;
    return STACK_ERR_OK;
}

// Pop a pointer payload from the stack. Returns NULL if stack was empty.

int stackPop (tStack *stack) {
    tNode *node = stack->top;
    if (node == NULL) {
        return NULL;
    stack->top = node->next;
    stack->count--;
    return node->data;
}

// Get stack size.

int stackSize (tStack *stack) {
    return stack->count;
}

, , , , . (), :

void stackDel (tStack *stack) {
    tNode *node = stackPop (stack);
    while (node != NULL) {
        free (node);
        node = stackPop (stack);
    }
    free (stack);
}

, , , (, API, , ). .

+1

Say you have a STACK called my_stack and a STACK_NODE called my_node. To add my_node to my_stack, do the following:

my_node.link = my_stack.top;
my_stack.top = &my_node;
my_stack.count = my_stack.count + 1;
0
source

Source: https://habr.com/ru/post/1732473/


All Articles