stack.h

来自「数据结构实验程序 线性表 栈和队列 二叉树 图 查找 内部排序」· C头文件 代码 · 共 60 行

H
60
字号
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>



#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef struct BiTNode{
	char data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;


struct SqStack{
	BiTree *base;
    BiTree *top;
	int stacksize;
};

void InitStack(struct SqStack *S)
{S->base=(BiTree*)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!S->base)exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}

void Push(struct SqStack* S,BiTree e)
{if(S->top-S->base>=S->stacksize)
{S->base=(BiTree*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(BiTree));
if(!S->base)exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;}
*S->top++=e;
}

BiTree Pop(struct SqStack *S,BiTree e)
{
if(S->top-S->base>0)
e=*--S->top;
return e;
}



BiTree Top(struct SqStack *S,BiTree e)
{
if(S->top-S->base>0)
e=*(S->top-1);
return e;
}



int StackEmpty(struct SqStack *S)
{if(S->top-S->base==0)return 1;
else return 0;
}

⌨️ 快捷键说明

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