Java ~ Stack Array Representation

Source: Internet
Author: User

/**
* @ Author yuanzhenhui
* @ Serialdata 2006-9-10
* @ Version 1.0
*
*/

Stack Interface

Public interface stackinterface {
Public Boolean isempty ();
 
Public Boolean isfull ();
 
Public void push (Object OO) throws overflowexception;
 
Public object POP () throws underflowexception;
 
Public object get ();
}

// Custom exception
Class overflowexception extends exception {
Public overflowexception (){
System. Out. println ("this is an exception ");
}
}
Class underflowexception extends exception {
Public underflowexception (){

}
}
Public class mystack implements stackinterface {
Private object table [];
// Top is the top element of the stack.
Private int Top =-1;
// Construct the stack of N storage units
Public mystack (int n ){
Table = new object [N];
}
 
Public Boolean isempty (){
Return Top =-1;
}
 
Public Boolean isfull (){
Return top> = table. Length-1;
}
 
Public void push (Object OO) throws overflowexception {
If (! This. isfull ()){
Top ++;
Table [Top] = Oo;
} Else {
Throw new overflowexception ();
}
}
 
Public object POP () throws underflowexception {
Object temp = NULL;
If (! This. isempty ()){
Temp = table [Top];
Table [Top] = NULL;
Top --;
} Else {
Throw new underflowexception ();
}
Return temp;
}
 
Public object get (){
If (! This. isempty ()){
Return table [Top];
} Else
Return NULL;
}
}
 

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.