堆栈java实现及应用

 

1.给出对应于stack的数据类型接口

package dataStructures;

public interface Stack
{
   public boolean empty();  //是否空栈
   public Object peek();     //返回栈顶元素
   public void push(Object theObject);//
   public Object pop();  //
}

2.实现子类

 

1)/** a stack class derived from ArrayLinearList */

package dataStructures;

import java.util.*;

public class DerivedArrayStack extends ArrayLinearList
                               implements Stack
{
   // constructors
   /** create a stack with the given initial capacity */
   public DerivedArrayStack(int initialCapacity)
      {super(initialCapacity);}

 

   /** create a stack with initial capacity 10 */
   public DerivedArrayStack()
      {this(10);}

 

   // methods
   /** @return true iff stack is empty */
   public boolean empty()
       {return isEmpty();}


   /** @return top element of stack
     * @throws EmptyStackException when the stack is empty */
   public Object peek()
   {
      if (empty())
         throw new EmptyStackException();
      return get(size() - 1);
   }

 

   /** add theElement to the top of the stack */
   public void push(Object theElement)
      {add(size(), theElement);}

 

   /** remove top element of stack and return it
     * @throws EmptyStackException when the stack is empty */
   public Object pop()
   {
      if (empty())
         throw new EmptyStackException();
      return remove(size() - 1);
   }
  
   /** test program */
   public static void main(String [] args)
   { 
      int x;
      DerivedArrayStack s = new DerivedArrayStack(3);
      // add a few elements
      s.push(new Integer(1));
      s.push(new Integer(2));
      s.push(new Integer(3));
      s.push(new Integer(4));


      // delete all elements
      while (!s.empty())
      {
         System.out.println("Top element is " + s.peek());
         System.out.println("Removed the element " + s.pop());
      }
   } 
}

2)类arrayStack

 

/** a stack class that uses a one-dimensional array */

package dataStructures;

import java.util.EmptyStackException;
import utilities.*;

public class ArrayStack implements Stack
{
   // data members
   int top;          // current top of stack
   Object [] stack;  // element array

   // constructors
   /** create a stack with the given initial capacity
     * @throws IllegalArgumentException when initialCapacity < 1 */
   public ArrayStack(int initialCapacity)
   {
      if (initialCapacity < 1)
         throw new IllegalArgumentException
               ("initialCapacity must be >= 1");
      stack = new Object [initialCapacity];
      top = -1;
   }

   /** create a stack with initial capacity 10 */
   public ArrayStack()
      {this(10);}

   // methods
   /** @return true iff stack is empty */
   public boolean empty()
      {return top == -1;}


   /** @return top element of stack
     * @throws EmptyStackException when the stack is empty */
   public Object peek()
   {
      if (empty())
         throw new EmptyStackException();
      return stack[top];
   }

 

   /** add theElement to the top of the stack */
   public void push(Object theElement)
   {
      // increase array size if necessary
      if (top == stack.length - 1)
         stack = ChangeArrayLength.changeLength1D(stack, 2 * stack.length);
        
      // put theElement at the top of the stack
      stack[++top] = theElement;
   }

 

   /** remove top element of stack and return it
     * @throws EmptyStackException when the stack is empty */
   public Object pop()
   {
      if (empty())
         throw new EmptyStackException();
      Object topElement = stack[top];
      stack[top--] = null;   // enable garbage collection
      return topElement;
   }
  
   /** test program */
   public static void main(String [] args)
   { 
      int x;
      ArrayStack s = new ArrayStack(3);
      // add a few elements
      s.push(new Integer(1));
      s.push(new Integer(2));
      s.push(new Integer(3));
      s.push(new Integer(4));


      // delete all elements
      while (!s.empty())
      {
         System.out.println("Top element is " + s.peek());
         System.out.println("Removed the element " + s.pop());
      }
   } 
}

 

3.DerivedLinkedStack

 

/** a stack class derived from Chain */

package dataStructures;

import java.util.*;

public class DerivedLinkedStack extends Chain
                                implements Stack
{
   // constructors
   /** included only for compatibility with other stack classes */
   public DerivedLinkedStack(int initialCapacity)
      {super();}

   public DerivedLinkedStack()
      {this(0);}

  

// methods
   /** @return true iff stack is empty */
   public boolean empty()
      {return isEmpty();}


   /** @return top element of stack
     * @throws EmptyStackException when the stack is empty */
   public Object peek()
   {
      if (empty())
         throw new EmptyStackException();
      return get(0);
   }

  

 /** add theElement to the top of the stack */
   public void push(Object theElement)
      {add(0, theElement);}

  

/** remove top element of stack and return it
     * @throws EmptyStackException when the stack is empty */
   public Object pop()
   {
      if (empty())
         throw new EmptyStackException();

      // remove and return top element
      return remove(0);
   }
  
   /** test program */
   public static void main(String [] args)
   { 
      int x;
      DerivedLinkedStack s = new DerivedLinkedStack(3);
      // add a few elements
      s.push(new Integer(1));
      s.push(new Integer(2));
      s.push(new Integer(3));
      s.push(new Integer(4));


      // delete all elements
      while (!s.empty())
      {
         System.out.println("Top element is " + s.peek());
         System.out.println("Removed the element " + s.pop());
      }
   } 
}

4. DerivedVectorStack

/** a stack class derived from Vector */

package dataStructures;

import java.util.*;

public class DerivedVectorStack extends Vector
                                implements Stack
{
   // constructors
   /** create a stack with the given initial capacity */
   public DerivedVectorStack(int initialCapacity)
      {super(initialCapacity);}

   /** create a stack with initial capacity 10 */
   public DerivedVectorStack()
   {// use default capacity of 10
      this(10);
   }

   // methods
   /** @return true iff stack is empty */
   public boolean empty()
      {return isEmpty();}


   /** @return top element of stack
     * @throws EmptyStackException when the stack is empty */
   public Object peek()
   {
      if (empty())
         throw new EmptyStackException();
      return get(size() - 1);
   }

   /** add theElement to the top of the stack */
   public void push(Object theElement)
      {add(size(), theElement);}

   /** remove top element of stack and return it
     * @throws EmptyStackException when the stack is empty */
   public Object pop()
   {
      if (empty())
         throw new EmptyStackException();
      return remove(size() - 1);
   }
  
   /** test program */
   public static void main(String [] args)
   { 
      int x;
      DerivedVectorStack s = new DerivedVectorStack(3);
      // add a few elements
      s.push(new Integer(1));
      s.push(new Integer(2));
      s.push(new Integer(3));
      s.push(new Integer(4));


      // delete all elements
      while (!s.empty())
      {
         System.out.println("Top element is " + s.peek());
         System.out.println("Removed the element " + s.pop());
      }
   } 
}

5)DerivedArrayStackWithCatch

/** a stack class derived from ArrayLinearList */
/** this implementation catches exceptions thrown by
  * ArrayLinearList */

package dataStructures;

import java.util.*;

public class DerivedArrayStackWithCatch extends ArrayLinearList
                                        implements Stack
{
   // constructors
   /** create a stack with the given initial capacity */
   public DerivedArrayStackWithCatch(int initialCapacity)
      {super(initialCapacity);}

   /** create a stack with initial capacity 10 */
   public DerivedArrayStackWithCatch()
      {this(10);}

   // methods
   /** @return true iff stack is empty */
   public boolean empty()
       {return isEmpty();}


   /** @return top element of stack
     * @throws EmptyStackException when the stack is empty */
   public Object peek()
   {
      try {return get(size() - 1);}
      catch (IndexOutOfBoundsException e)
      {
         throw new EmptyStackException();
      }
   }

   /** add theElement to the top of the stack */
   public void push(Object theElement)
      {add(size(), theElement);}

   /** remove top element of stack and return it
     * @throws EmptyStackException when the stack is empty */
   public Object pop()
   {
      try
         {return remove(size() - 1);}
      catch (IndexOutOfBoundsException e)
      {
         throw new EmptyStackException();
      }
   }
  
   /** test program */
   public static void main(String [] args)
   { 
      int x;
      DerivedArrayStackWithCatch s = new DerivedArrayStackWithCatch(3);
      // add a few elements
      s.push(new Integer(1));
      s.push(new Integer(2));
      s.push(new Integer(3));
      s.push(new Integer(4));


      // delete all elements
      while (!s.empty())
      {
         System.out.println("Top element is " + s.peek());
         System.out.println("Removed the element " + s.pop());
      }
   } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值