arraystack-arraystack.html

来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 55 行

HTML
55
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre> <font color=#ff0080>/**  * Implementation of the stack ADT using a fixed-length array.  An  * exception is thrown if a push operation is attempted when the size  * of the stack is equal to the length of the array.  This class  * includes the main methods of the built-in class java.util.Stack.  */</font><font color=#8000a0>public</font> <font color=#8000a0><font color=#ff8000>class</font> </font>ArrayStack&lt;E&gt; <font color=#8000a0><font color=#ff8000>implements</font> </font>Stack&lt;E&gt; {  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>int</font> capacity; 	<font color=#ff0080>// The actual capacity of the stack array</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>static</font> <font color=#8000a0><font color=#8000a0>final</font> </font><font color=#8000a0>int</font> CAPACITY = 1000;	<font color=#ff0080>// default array capacity</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font>E S[];		<font color=#ff0080>// Generic array used to implement the stack </font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>int</font> top = -1;	<font color=#ff0080>// index for the top of the stack</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>ArrayStack</font>() {    <font color=#ff8000>this</font><font color=#0000ff></font>(CAPACITY); <font color=#ff0080>// default capacity </font>  }  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>ArrayStack</font>(<font color=#8000a0>int</font> cap) {    capacity = cap;    S =<font color=#0000ff> </font>(E[]) <font color=#8000a0><font color=#ff8000>new</font> </font>Object[capacity]; <font color=#ff0080>// compiler may give warning, but this is ok</font>  }  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>int</font> <font color=#0000ff>size</font>() {     <font color=#ff8000>return</font><font color=#0000ff> </font>(top + 1);  }  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>boolean</font> <font color=#0000ff>isEmpty</font>() {    <font color=#ff8000>return</font><font color=#0000ff> </font>(top &lt; 0);  }  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>push</font>(E element) <font color=#8000a0><font color=#ff8000>throws</font> </font>FullStackException {    <font color=#ff8000>if</font><font color=#0000ff> </font>(<font color=#0000ff>size</font>() == capacity)      <font color=#8000a0><font color=#ff8000>throw</font> </font><font color=#ff8000>new</font> <font color=#0000ff>FullStackException</font>(<font color=#008000>"Stack is full."</font>);    S[++top] = element;  }  <font color=#8000a0><font color=#8000a0>public</font> </font>E <font color=#0000ff>top</font>() <font color=#8000a0><font color=#ff8000>throws</font> </font>EmptyStackException {    <font color=#ff8000>if</font><font color=#0000ff> </font>(<font color=#0000ff>isEmpty</font>())      <font color=#8000a0><font color=#ff8000>throw</font> </font><font color=#ff8000>new</font> <font color=#0000ff>EmptyStackException</font>(<font color=#008000>"Stack is empty."</font>);    <font color=#8000a0><font color=#ff8000>return</font> </font>S[top];    }  <font color=#8000a0><font color=#8000a0>public</font> </font>E <font color=#0000ff>pop</font>() <font color=#8000a0><font color=#ff8000>throws</font> </font>EmptyStackException {    <font color=#8000a0>E </font>element;    <font color=#ff8000>if</font><font color=#0000ff> </font>(<font color=#0000ff>isEmpty</font>())      <font color=#8000a0><font color=#ff8000>throw</font> </font><font color=#ff8000>new</font> <font color=#0000ff>EmptyStackException</font>(<font color=#008000>"Stack is empty."</font>);    element = S[top];    S[top--] = null; <font color=#ff0080>// dereference S[top] for garbage collection.</font>    <font color=#8000a0><font color=#ff8000>return</font> </font>element;  }</dl></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?