栈的实现,算是最基本的数据结构了。具体实现如下:
SQSTACK.H的内容:
#ifndef __SQSTACK_H__
#define __SQSTACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define INITSTACKSIZE 100
#define ADDSTACKSIZE 10
typedef char ElemType; //不同程序需要修改
typedef struct SQSTACK{
ElemType *top;
ElemType *base;
int size,length;
}SqStack,*SSqStack;
int InitStack(SSqStack*);
void DestroyStack(SSqStack);
void DisplyStack(SSqStack);
int PushStack(SSqStack,ElemType);
int PopStack(SSqStack);
int EmptyStack(SSqStack);
int LengthStack(SSqStack);
ElemType TopStack(SSqStack);
int print(ElemType);
#endif
SQSTACK.C的内容:
#include "SqStack.h"
int InitStack(SSqStack* sstack)
{
(*sstack)=(SSqStack)malloc(sizeof(SqStack));
(*sstack)->base=(ElemType*)malloc(INITSTACKSIZE*sizeof(ElemType));
(*sstack)->top=(*sstack)->base;
(*sstack)->length=0;
(*sstack)->size=INITSTACKSIZE;
return (*sstack)->base!=NULL;
}
void DestroyStack(SSqStack sstack)
{
free(sstack->base);
free(sstack);
}
void DisplyStack(SSqStack sstack)
{
int i=~sstack->length+1;
while(++i)
{
print(*(sstack->base-i));
printf("->");
}
print(*(sstack->base-i));
}
int PushStack(SSqStack sstack,ElemType elem)
{
ElemType* _temp_sstack;
if(sstack->length>=sstac