stack Java implementation and application

Source: Internet
Author: User
Tags garbage collection

1. Give the data type interface corresponding to the stack

Package datastructures;

public interface Stack
{
public boolean empty (); is empty stack
Public Object Peek (); Returns the top element of the stack
public void push (Object theobject);//
Public Object pop (); //
}

2. Implementing subclasses

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's stack and return it
     * @throws emptystackexception WH En the stack is empty */
   public Object pop ()
   {
      if (empt Y ())
         throw new Emptystackexception ();
      return Remove (Size ()-1);
  }
  
  /** Test program */
   public static void Main (String [] args)
 &N Bsp { 
      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) class 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 in the top of the stack
Stack[++top] = theelement;
}

/** Remove top element's 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 to compatibility with the 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 to get (0);
}

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

/** Remove top element's 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)
&NBSP;&N Bsp { 
      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's stack and return it
     * @throws emptystackexception WH En the stack is empty */
   public Object pop ()
   {
      if (empt Y ())
         throw new Emptystackexception ();
      return Remove (Size ()-1);
  }
  
  /** Test program */
   public static void Main (String [] args)
&NBSP;&N Bsp { 
      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's 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 ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.