数据结构----栈

来源:互联网 发布:三菱plc编程案例 编辑:程序博客网 时间:2024/05/29 21:29

 

 

class StackX {

    privateintmaxSize;// size of stack array

    privatedouble[] stackArray;

    privateinttop; // top of stack

    //-------------------------------------------------------------

 

    public StackX(int s)// constructor

    {

        maxSize = s;// set array size

        stackArray =newdouble[maxSize];// create array

        top = -1;// no items yet

    }

 

    //-------------------------------------------------------------

 

    publicvoid push(double j) // put item on top of stack

    {

        stackArray[++top] = j;// increment top, insert item

    }

 

    //-------------------------------------------------------------

 

    publicdouble pop()// take item from top of stack

    {

        returnstackArray[top--];// access item, decrement top

    }

 

    //-------------------------------------------------------------

 

    publicdouble peek()// peek at top of stack

    {

        returnstackArray[top];

    }

 

    //-------------------------------------------------------------

 

    publicboolean isEmpty()// true if stack is empty

    {

        // - 94 -

        return (top == -1);

    }

 

    //-------------------------------------------------------------

    publicboolean isFull()// true if stack is full

    {

        return (top ==maxSize - 1);

    }

    //-------------------------------------------------------------

} // end class StackX

 

栈中,

maxSize:描述栈的大小

stackArray描述栈的类型数组

top:描述栈的指针

push(double j):在栈里加数据的方法

pop():在栈里去数据的方法

peek():取出当前栈中指针指向的数据

isEmpty():判断栈中是不是还有数据

isFull():判读栈是不是满了

 

 

 

 

 

原创粉丝点击