Java実装スタックStack_スタック内部で配列ストレージ構造を使用する
4059 ワード
Java実装スタックStack_スタック内部で配列ストレージ構造を使用する
抽象データ型スタックの定義:
スタック(stack)は、表の末尾に挿入または削除操作を限定する線形テーブルであるため、スタックにとって表の末尾にはスタックトップと呼ばれ、対応して、表頭端をスタックベースと呼ぶ特殊な意味がある.要素を含まない空のテーブルを空スタックと呼びます.
スタック--後進先出(last in first out)
スタック・シーケンス・スタック・スタックのシーケンス・ストレージ構造は、スタック・ベースからスタック・トップまでのデータ要素を、アドレス連続メモリ・ユニットのセットを用いて順次格納するものである.
具体的にはjava.util.Stack;内部で実装されたデータ構造スタックは、Javaにおける高度なデータ構造の実装プロセスを体得するためにのみ使用されます...
コードを貼る
===END===
抽象データ型スタックの定義:
スタック(stack)は、表の末尾に挿入または削除操作を限定する線形テーブルであるため、スタックにとって表の末尾にはスタックトップと呼ばれ、対応して、表頭端をスタックベースと呼ぶ特殊な意味がある.要素を含まない空のテーブルを空スタックと呼びます.
スタック--後進先出(last in first out)
スタック・シーケンス・スタック・スタックのシーケンス・ストレージ構造は、スタック・ベースからスタック・トップまでのデータ要素を、アドレス連続メモリ・ユニットのセットを用いて順次格納するものである.
具体的にはjava.util.Stack;内部で実装されたデータ構造スタックは、Javaにおける高度なデータ構造の実装プロセスを体得するためにのみ使用されます...
コードを貼る
package hash;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-9-14
* Time: 7:14
* To change this template use File | Settings | File Templates.
*/
public class CustomStack<E> {
protected E[] elementData; //
protected int elementCount; //
protected int capacityIncrement; //
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public CustomStack(int initialCapacity, int capacityIncrement) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
this.elementData = (E[]) new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
*
*
* , 。
* , P , , 。
*/
public synchronized E push(E item) {
// ,
int minCapacity = elementCount + 1;
if (minCapacity - elementData.length > 0) { //
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
if (newCapacity - MAX_ARRAY_SIZE > 0) {
newCapacity = (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}
E[] copy = (E[]) new Object[newCapacity]; //
System.arraycopy(elementData, 0, copy, 0, elementData.length); //
elementData = copy;
}
elementData[elementCount++] = item;
return item;
}
/**
* ,
*/
public synchronized E pop() throws Exception {
E obj;
int index = elementCount - 1; //
if (index < 0) {
throw new Exception(" ");
}
obj = elementData[index];
elementData[index] = null; // gc
elementCount--;
return obj;
}
/**
* ,
*/
public synchronized E peek() throws Exception {
E obj;
int index = elementCount - 1; //
if (index < 0) {
throw new Exception(" ");
}
obj = elementData[index];
return obj;
}
/**
*
*/
public synchronized boolean empty() {
return elementCount == 0;
}
/**
*
*
* ,
*
* @param o
* @return
*/
public synchronized int search(Object o) {
int index = elementCount - 1;
if (o == null) {
for (int i = index; i >= 0; i--) {
if (elementData[index] == null) {
return elementCount - i;
}
}
} else {
for (int i = index; i >= 0; i--) {
if (elementData[index].equals(o)) {
return elementCount - i;
}
}
}
return -1;
}
public static void main(String args[]) throws Exception {
CustomStack<String> stringCustomStack = new CustomStack<String>(12, 10);
for (int i = 0; i < 200; i++) {
stringCustomStack.push("lyx" + i);
}
System.out.println(stringCustomStack.peek());
System.out.println(stringCustomStack.pop());
System.out.println(stringCustomStack.elementCount);
}
}
===END===