review 栈 --java实现

栈 stack是限制仅在表的一端进行插入和删除操做的线性表。其特色为先进后出FILO; spa

Demo代码以下: 指针

class StackX {
private int maxSize;
private long[] stackArray;
private int top;


public StackX(int s) {
// Initial stack
maxSize = s;
stackArray = new long[maxSize];
// top
top = -1;
}


/**
* push elements to stack
*/
public void push(long j) { element

// top指针向上移动一位
stackArray[++top] = j;
}


public long peek() {
    return stackArray[top];
}


public long pop() {
    return stackArray[top--];
}


public boolean isEmpty() {
    return top == -1;
}


public boolean isFull() {
    return top == maxSize - 1;
}
}

public class StackApp {
public static void main(String[] args) {
    StackX theStack = new StackX(10);
    // 向栈中压入元素
    theStack.push(10);
    theStack.push(20);
    theStack.push(30);
    theStack.push(40);
    theStack.push(50);


System.out.println("获取栈中元素--弹出:");
    while (!theStack.isEmpty()) {
        long value = theStack.pop();
        System.out.print(value + "  ");
    }
 }
} it

相关文章
相关标签/搜索