> For the complete documentation index, see [llms.txt](https://krjaeh0.gitbook.io/j-log/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://krjaeh0.gitbook.io/j-log/programming/reference-and-snippets/c-snippets/mystack.c.md).

# MyStack.c

{% code title="MyStack.c" %}

```c
//
//  MyStack.c
//  C_HeaderTest
//  Created by admin on 3/16/24.
//

#include "MyStack.h"

int initStack(stack *_pStack_)
{
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: initStack()\n");
        return -1;
    }

    _pStack_->nCurrentCount = 0;
    _pStack_->pTop = NULL;
    return 0;
}

stack *createStack(void)
{
    stack *pResult = (stack*)malloc(sizeof(stack));
    if (pResult == NULL)
    {
        printf("memory allocation error: createStack()\n");
        return NULL;
    }
    if (initStack(pResult) != 0)
    {
        printf("Memory initialization failed: createStack()\n");
        return NULL;
    }
    return pResult;
}

int initNode(node *_pNode_, const char _cData_)
{
    if (_pNode_ == NULL)
    {
        printf("Unallocated memory access error: initNode()\n");
        return -1;
    }
    _pNode_->cData = _cData_;
    _pNode_->pNext = NULL;
    return 0;
}

int push(stack *_pStack_, const char _cData_)
{
    node *pNewNode = NULL;
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: push()\n");
        return -1;
    }
    pNewNode = (node*)malloc(sizeof(node));
    if (pNewNode == NULL)
    {
        printf("memory allocation error: push()\n");
        return -2;
    }
    if (initNode(pNewNode, _cData_) != 0)
    {
        printf("Memory initialization failed: createStack()\n");
        return -3;
    }
    pNewNode->pNext = _pStack_->pTop;
    _pStack_->pTop = pNewNode;
    _pStack_->nCurrentCount++;
    return 0;
}

node *pop(stack *_pStack_)
{
    node *pPopNode = NULL;
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: pop()\n");
        return NULL;
    }
    if ((_pStack_->pTop == NULL) && (_pStack_->nCurrentCount == 0))
    {
        printf("Stack underflow error: pop()\n");
        return NULL;
    }
    pPopNode = _pStack_->pTop;
    _pStack_->pTop = pPopNode->pNext;
    pPopNode->pNext = NULL;
    _pStack_->nCurrentCount--;
    return pPopNode;
}

node *peek(const stack *_pStack_)
{
    node *pPeekNode = NULL;
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: peek()\n");
        return NULL;
    }
    if ((_pStack_->pTop == NULL) && (_pStack_->nCurrentCount == 0))
    {
        printf("No data in stack: peek()\n");
        return NULL;
    }
    pPeekNode = (node*)malloc(sizeof(node));
    if (pPeekNode == NULL)
    {
        printf("memory allocation error: peek()\n");
        return NULL;
    }
    if (initNode(pPeekNode, _pStack_->pTop->cData) != 0)
    {
        printf("Memory initialization failed: peek()\n");
        return NULL;
    }
    return pPeekNode;
}

int displayStack(const stack *_pStack_)
{
    node *pNode = NULL;
    int nCount = 0;
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: displayStack()\n");
        return -1;
    }

    printf("current stack: %d number\n", _pStack_->nCurrentCount);
    pNode = _pStack_->pTop;
    for ( ; pNode != NULL; pNode = pNode->pNext)
    {
        printf("[%d]-[%c]\n", _pStack_->nCurrentCount - nCount, pNode->cData);
        nCount++;
    }

    return 0;
}

int deleteStack(stack *_pStack_)
{
    node *delNode = NULL;
    if (_pStack_ == NULL)
    {
        printf("Unallocated memory access error: deleteStack()\n");
        return -1;
    }
    while (_pStack_->pTop != NULL)
    {
        delNode = _pStack_->pTop;
        _pStack_->pTop = delNode->pNext;
        free(delNode);
    }
    free(_pStack_);
    return 0;
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://krjaeh0.gitbook.io/j-log/programming/reference-and-snippets/c-snippets/mystack.c.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
