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

相關文章
相關標籤/搜索