zoukankan      html  css  js  c++  java
  • C++编程练习(3)----“实现简单的栈的顺序存储结构“

    栈(stack)是限定仅在表尾进行插入和删除操作的线性表。

    允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom)。

    栈又称为后进先出(Last In First Out)的线性表,简称为LIFO结构。


    用数组方式实现简单的栈的代码如下:

    /* SqStack.h*/
    #include<iostream>
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    #define MAXSIZE 20
    typedef int SElemType;
    typedef int Status;
    
    class SqStack{
    public:
    	SqStack():data(),top(-1) {};
    	SElemType data[MAXSIZE];
    	int top;	/*用于栈顶指针*/
    	Status Push(SElemType e);	/*进栈操作*/
    	Status Pop(SElemType *e);	/*出栈操作*/
    	Status ShowStack() const;	/*从栈顶至栈底输出元素*/
    };
    Status SqStack::Push(SElemType e)
    {
    	if(top==MAXSIZE-1)	/*栈满*/
    		return ERROR;
    	top++;
    	data[top]=e;
    	return OK;
    }
    Status SqStack::Pop(SElemType *e)
    {
    	if(top==-1)
    		return ERROR;
    	*e=data[top];
    	top--;
    	return OK;	
    }
    Status SqStack::ShowStack() const
    {
    	int j;
    	std::cout<<"从栈顶至栈底输出元素为:";
    	for(j=top;j>=0;j--)
    		std::cout<<data[j]<<"  ";
    	std::cout<<std::endl;
    	return OK;
    }



  • 相关阅读:
    052-14
    052-13
    css垂直居中
    js中的null 和undefined
    给数组添加属性
    js中避免函数名和变量名跟别人冲突
    js变量问题
    Life
    BFC和haslayout
    json文件
  • 原文地址:https://www.cnblogs.com/fengty90/p/3768860.html
Copyright © 2011-2022 走看看