• 【数据结构】顺序栈的基本操作操作---C/C++


    本博客所有文章均已迁入到<a target=_blank target="_blank" href="http://www.coderbean.com">http://www.coderbean.com</a>
    //头文件内容
    #define STACK_INIT_SIZE 10
    #define STACKINCREMENT 2
    struct SqStack
    {
    	SElemType *base;
    	SElemType *top;
    	int stacksize;
    };


    //顺序栈的操作
    //初始化,清空,压栈,出栈,遍历,销毁
     Status InitStack(SqStack &S)
    {
    	if(!(A.base=(SElemType *)malloc(STACK_INIT_SIZE)*sizeof(SElemType))))
    	{
    		exit(OVERFLOW);//储存分配失败;
    	}
    	S.top = S.base;//栈顶和栈底指向同一位置
    	S.stacksize = STACK_INIT_SIZE;
    	return OK;
    }
    
    Status DestroyStack(SqStack &S)
    {
        free(S.base);
        S.base = NULL;
        S.top = NULL;
        S.stacksize = 0;
        return OK;
    }
    
    Status ClearStack(SqStack &S)
    {
    	S.top = S.base;
    	return OK;
    }
    
    Status StackEmpty(SqStack &S)
    {
    	if(S.top == S.base)
    		return TRUE;
    	else
    		return FALSE;
    }
    
    Status StackLength(SqStack &S)
    {
    	return (S.top - S.base);
    }
    
    Status GetTop(SqStack S, ElemType &e)
    {
    	if(S.top > S.base)//注意判断当栈不为空是才能提取栈顶元素
    	{
    		e = *S.top--;
    		return OK;
    	}
    	else
    		return ERROR;
    }
    Status Push(SqStack &SElemType,ElemType e)
    {
    	if(S.top - S.base>=S.stacksize)
    	{
    		S.base = (SElemType*)realloc(S.stacksize + S.base,STACKINCREMENT*sizeof(SElemType));
    		//记得要加上原来的容量realloc的用法
    		if(!S.base)
    		{
    			exit(OVERFLOW);
    		}
    		S.top = S.base + S.stacksize;
    		S.stacksize += STACKINCREMENT;
    		//注意以上两条语句的顺序
    	}
    	*++S.top = e;
    	return OK;
    }
    
    Status Pop(SqStack &S, SElemType *e)
    {
    	if(S.top == S.base)
    		return ERROR;//首先要判断栈非空
    	e = *S.top--;
    	return OK;
    }
    Status StackTraverse(SqStack S, Status(*visit)(SElemType))
    {
    	while(S.top != S.base)
    	{
    		visit(*S.base++);
    	}
    	printf('
    ');
    	return OK;
    }
    


  • 相关阅读:
    linux中awk命令提取连续列
    re.sub()用法的详细介绍
    pygame教程
    基于 HTML5 和 CSS3 开发的优秀应用程序
    KISSYUI的表单前端js验证和JQuery扩展插件Validate
    提供源码 后台界面WebUi框架分享
    推荐10款非常优秀的HTML5开发工具
    产品化思维之用户认证和用户授权
    JAVA虚机查找class 的规则
    产品化思维之基于资源属性的权限控制方案(ABAC)
  • 原文地址:https://www.cnblogs.com/coderbean/p/4489055.html
Copyright © 2020-2023  润新知