包含顺序栈、链表栈的实现
package Structure.Stack;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public interface StackInterface<E extends Comparable<E>> {
public void Push(E value);
public void Pop();
public E Top();
public void MakeEmpty();
}
package Structure.Stack;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public class SeqStack<E extends Comparable<E>> implements StackInterface<E> {
private
Object data_[];
private
int top_;
private
int maxSize_;
public SeqStack(int stackSize)
{
maxSize_ = stackSize;
top_ = -1;
data_ = new Object[maxSize_];
}
private boolean IsEmpty(){return top_ == -1;}
private void DoubleSize()
{
Object temp[] = new Object[2*maxSize_];
for(int i = 0 ; i <= top_ ; ++i)
{
temp[i] = data_[i];
}
data_ = temp;
maxSize_ *= 2;
}
@Override
public void Push(E value) {
// TODO Auto-generated method stub
if( top_ == maxSize_ -1)
{
DoubleSize();
}
++top_;
data_[top_] = value;
}
@Override
public void Pop() {
// TODO Auto-generated method stub
if(!IsEmpty())
--top_;
}
@Override
public E Top() {
// TODO Auto-generated method stub
if(!IsEmpty())
return (E)data_[top_];
else return null;
}
@Override
public void MakeEmpty() {
// TODO Auto-generated method stub
top_ = -1;
}
}
package Structure.Stack;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
class Node<E extends Comparable<E> >
{
Node<E> next_;
E data_;
public Node(E data,Node<E> next){data_ = data ; next_ = next;}
}
public class ListStack<E extends Comparable<E> > implements StackInterface<E> {
private Node<E> top_;
public ListStack(){top_ = null;}
@Override
public void Push(E value) {
// TODO Auto-generated method stub
Node<E> n = new Node<E>(value,null);
if(top_ == null)
{
top_ = n;
}
else
{
n.next_ = top_;
top_ = n;
}
}
@Override
public void Pop() {
// TODO Auto-generated method stub
if(top_ != null)
{
top_ = top_.next_;
}
}
@Override
public E Top() {
// TODO Auto-generated method stub
return top_.data_;
}
@Override
public void MakeEmpty() {
// TODO Auto-generated method stub
top_ = null;
}
}
package Structure.Stack;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public class StackTest {
public static void main(String[] args)
{
StackInterface<Integer> s = new SeqStack<Integer>(5);
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
s.Push(7);
s.Push(8);
Integer t = s.Top();
System.out.println(t);
s.Pop();
s.Pop();
s.Pop();
t = s.Top();
System.out.println(t);
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
s.Push(7);
s.Push(8);
t = s.Top();
System.out.println(t);
System.out.println();
System.out.println();
StackInterface<Integer> s2 = new ListStack<Integer>();
s2.Push(1);
s2.Push(10);
s2.Push(15);
System.out.println(s2.Top());
s2.Pop();
System.out.println(s2.Top());
s2.Pop();
System.out.println(s2.Top());
}
}