栈的抽象数据类型定义
(2011-03-27 15:10:33)
标签:
栈数据元素宋体抽象数据类型杂谈 |
分类: 数据结构 |
ADT Stack{
数据对象:D={ai|ai∈ElemSet, i=1,2, …,n, n≥0}
数据关系:R1={<ai-1,ai>|ai-1,ai∈D, i=1,2, …,n }
基本操作:
}ADT Stack
#include<stdio.h>
#include<malloc.h>
#define STACK_INIT_SIZE 1
#define STACKINCREMENT 10
typedef char ElemType;
struct SqStack
{
ElemType *elem;
ElemType *top;
int stacksize;
};
int InitStack(struct SqStack *S)
{
S->elem=(ElemType *)malloc(STACK_INIT_SIZE *
sizeof(ElemType));
if(!S->elem)
{
printf("初始化栈失败");
return 0;
}
else
{
S->top=S->elem;
S->stacksize= STACK_INIT_SIZE;
printf("初始化栈成功\n");
return 1;
}
}
int Push(struct SqStack *S,ElemType x)
{
if(S->top-S->elem>=S->stacksize)
{
S->elem=(ElemType
*)realloc(S->elem,(S->stacksize+STACKINCREMENT)
* sizeof(ElemType));
if(!S->elem)
{
printf("开辟空间失败\n");
return 0;
}
else
{
S->top=S->elem+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
}
*S->top=x;
S->top++;
return x;
}
int Pop(struct SqStack *S,ElemType x)
{
if(S->top==S->elem)
{
printf("栈空\n");
return 0;
#include<malloc.h>
#define STACK_INIT_SIZE 1
#define STACKINCREMENT 10
typedef char ElemType;
struct SqStack
{
};
int InitStack(struct SqStack *S)
{
}
int Push(struct SqStack *S,ElemType x)
{
}
int Pop(struct SqStack *S,ElemType x)
{