stack.h

来自「这是一个语法分析器 这是一个语法分析器」· C头文件 代码 · 共 52 行

H
52
字号
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

int new_stack(pstack *head, int size)
{
    (*head) = (pstack)malloc(sizeof(stack));
    if ((*head) == NULL)
        return -1;
    (*head)->buf = (int *)malloc(sizeof(int)*size);
    if ((*head)->buf == NULL)
        return -1;
    (*head)->buf_size = size;
    (*head)->ptr = 0;
    return 0;
}

int push(pstack head, int element)
{
    if (head->ptr == head->buf_size - 1)
        return -1;
    head->ptr++;
    head->buf[head->ptr] = element;
    return 0;
}

int top(pstack head)
{
    return head->buf[head->ptr];
}

int empty(pstack head)
{
    if (head->ptr == 0)
        return 1;
    return 0;
}

int pop(pstack head)
{
    if (empty(head))
        return -1;
    head->ptr--;
    return 0;
}

void delete_stack(pstack *head)
{
    free((*head)->buf);
    free((*head));
}
#endif // STACK_H_INCLUDED

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?