数据结构(C语言)-- 栈

基于C语言实现的栈

程序代码:
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define True 1
#define False 0

#define STACK_INT_SIZE 10  /*存储空间初始分配量*/
#define STACKINCREMENT 5  /*存储空间分配增量*/
typedef  int ElemType; /*定义元素的类型*/
typedef  int Status; /*定义元素的类型*/
typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;     /*当前已分配的存储空间*/
}SqStack;

Status InitStack(SqStack &S);   /*构造空栈*/
Status push(SqStack &S,ElemType e); /*入栈*/
Status Pop(SqStack &S,ElemType &e);  /*出栈*/
Status GetTop (SqStack S, ElemType &e); /*取顺序栈栈顶元素*/
Status StackEmpty(SqStack  S); /*判断顺序栈是否为空*/
int StackLength( SqStack S );/*求顺序栈的长度*/
Status CreateStack(SqStack &S);     /*创建栈*/
void PrintStack(SqStack &S);   /*出栈并输出栈中元素*/
Status DestroyStack( SqStack &S );   /*销毁栈*/

Status InitStack(SqStack &S){      /*构造空栈*/
    S.base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType));
    if(!S.base) return ERROR;
    S.top=S.base;
    S.stacksize=STACK_INT_SIZE;
    return OK;
}/*InitStack*/

Status Push(SqStack &S,ElemType e)     /*入栈*/
{
   if(S.top-S.base>=S.stacksize)
   {
	   S.base =(ElemType *)realloc(S.base,
		   (S.stacksize+STACKINCREMENT)*sizeof(ElemType));
	   if(!S.base)return ERROR;
	   S.top=S.base+S.stacksize ;
	   S.stacksize += STACKINCREMENT;
   }
	   *S.top++ =e;
   return OK;
}/*Push*/

Status Pop(SqStack &S,ElemType &e)       /*出栈*/
{
   if(S.top == S.base )return ERROR;
   e=*--S.top;
   return OK;
}/*Pop*/

Status GetTop (SqStack S, ElemType &e) /*取顺序栈栈顶元素*/
{
   if(S.top ==S.base )return ERROR;
   e=*(S.top-1);
   return OK;
}/*GetTop*/

Status StackEmpty(SqStack  S) /*判断顺序栈是否为空*/
{
   if(S.top ==S.base )return ERROR;
   else return OK;
}/*StackEmpty*/

int StackLength( SqStack S )/*求顺序栈的长度*/
{
	int i;
    if(S.top ==S.base )return ERROR;
	i=S.top-S.base;
	return OK;

}/*StackLength*/

Status CreateStack(SqStack &S)
{
    int e;
    if(InitStack(S))
        printf("Init Success!\n");
    else{
        printf("Init Fail!\n");
        return ERROR;
    }
    printf("input data:(Terminated by inputing a character)\n");
    while(scanf("%d",&e))
        Push(S,e);
    return OK;
}/*CreateStack*/

void PrintStack(SqStack &S){
    ElemType e;
    while(Pop(S,e))
        printf("%3d",e);
    printf("\n");
}/*Pop_and_Print*/

Status DestroyStack(SqStack &S)   /*销毁栈*/
{
	  if( S.base )
	  {
		  free(S.base);
		  S.stacksize = 0;
		  S.base = S.top = NULL;
	  }
      return OK;
}

int main(){
    SqStack ss;
	ElemType e;
    printf("\ncreate Stack\n");
    CreateStack(ss);
	Push(ss,e);
	GetTop(ss,e);
    printf("\nData on the top of the Stack is %d\n",e);
    printf("\n");
	if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");
    printf("\nThere are %d datas in the Stack \n",StackLength(ss));
    printf("\n");

   printf("\nPop&Print\n");
    PrintStack(ss);

if(StackEmpty(ss)) 
		printf("\nthe Stack is empty.\n");
	else
		printf("\nthe Stack is not empty.\n");
    printf("\n");

DestroyStack(ss);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值