栈的实现,算是最基本的数据结构了。具体实现如下:
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 P

本文介绍了栈这一基本数据结构的实现,通过提供的SQSTACK.H文件内容展示具体的实现细节,并展示了如何使用栈进行十进制到二进制的转换操作。
最低0.47元/天 解锁文章
2101

被折叠的 条评论
为什么被折叠?



