Stack Java version of data structure

Source: Internet
Author: User

Import Java.lang.reflect.Array;

/*

The specific principle in the C + + version has been said very clearly, here no longer repeat,

Just a point: Java generics have a boundary effect, and once the departure scope is immediately replaced with the object type, reflection is required in the new generic array.

*/

Interface basestack<t>{
Boolean push (T x);
Boolean pop ();
void Clear ();
Ttop ();
}
public class Jbstack<t> implements basestack<t>{
T[] Mstack;
int mmaxsize,mindex;
static public void Main (String atgs[]) {
Jbstack<integer> test=new jbstack<integer> (integer.class,10);
Test.push (1);
System.out.println ("" +test.top ());
}
Public Jbstack (class<t> type,int Size) {
TODO auto-generated Constructor stub
Mmaxsize=size;
Mindex=-1;
mstack= (t[]) array.newinstance (type,mmaxsize);//Note the way in which the new generic array is in Java generics have boundary substitution properties
}
@Override
Public boolean push (T x) {
TODO auto-generated Method Stub
if (Isfull ()) {
Over flow
return false;
}
mindex++;
Mstack[mindex] = x;
return true;
}
@Override
public boolean pop () {
TODO auto-generated Method Stub
if (Isempyty ()) {
Empty
return false;
}
mindex--;
return true;
}

@Override
public void Clear () {
TODO auto-generated Method Stub
Mindex=-1;
}

@Override
Public T Top () {
TODO auto-generated Method Stub
Return (T) Mstack[mindex];
}
Boolean Isempyty () {
if (mindex==-1) {
return true;
}
else{
return false;
}
}
Boolean isfull () {
if (mindex<mmaxsize) {
return false;
}
else{
return true;
}
}
}

Stack Java version of data structure

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.