以下是用C语言实现的一个栈的结构:
定义一个节点结构
- typedef struct Node{
- int data;
- struct Node*p;
- }NODE,*PNODE;
定义栈的结构体
- typedef struct Stack{
- PNODE top;
- PNODE bottom;
- }STACK,*PSTACK;
对栈的初始化函数
- void initStack(PSTACK pStack)
- {
- pStack->top = (PSTACK)malloc(sizeof(NODE));
- if (pStack->top == NULL)
- {
- printf("maoolc error\n");
- exit(-1);
- }
- else
- {
- pStack->bottom = pStack->top;
- pStack->top->p = NULL;
- {
- }
入栈函数
- void pushStack(PSTACK pStack,int val)
- {
- PNODE pNew = (PNODE)malloc(sizeof(NODE));
- if (pNew == NULL)
- {
- printf("maoolc error\n");
- exit(-1);
- }
- pNew->data =val;
- pNew->next = pStack->top;
- pStack->top = pNew;
- return;
- }