堆棧java實現及應用

來源:互聯網
上載者:User

 

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());
      }
   } 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.