Java implementation stack.

Source: Internet
Author: User

Define an interface Mystack interface:

Package Stack;

Public interface Mystack<t> {
Boolean isEmpty ();
int length ();
Boolean push (T date);
T pop ();
}

Array implementations:

Package Stack;

public class Arraystack<t> implements mystack<t>{
Private object[] Objs = new OBJECT[16];
Size is equivalent to a cursor
private int size = 0;
public Boolean isEmpty () {

return size==0;
}

@Override
public int Length () {
TODO auto-generated Method Stub
return size;
}

@Override
Public boolean push (T date) {
if (size>=objs.length) {
Resize ();
}
Objs[size]=date;
size++;
return true;
}

@Override
Public T pop () {
if (size==0) {
return null;
}

Return (T) objs[--size];
}
public void Resize () {
object[] temp = new object[objs.length*3/2+1];
for (int i=0;i<size;i++) {
Temp[i]=objs[i];
Objs[i] = null;
}
Objs=temp;
}
Public String toString () {
StringBuffer sb= new StringBuffer ();
Sb.append ("arraystack:[");
for (int i =0;i<size;i++) {
Sb.append (Objs[i]);
if (i!=size-1) {
Sb.append (",");
}
}
Sb.append ("]");
return sb.tostring ();
}
}

Linked list implementations:

Package Stack;

public class Linkstack<t> implements mystack<t> {
Node Top=null;
int size=0;

@Override
public Boolean isEmpty () {
TODO auto-generated Method Stub
return top==null;
}

@Override
public int Length () {

return size;
}

@Override
Public boolean push (T date) {
Node node = new node ();
Node.date = date;
Node.node = top;
Top=node;
size++;
return true;
}

@Override
Public T pop () {
if (size!=0) {
node node = top;
Top=node.node;
size--;
return node.date;
}
return null;
}
Class node{
private node node;
Private T date;
}
}

Test:

/**
*
*/
Package Stack;

Import Org.junit.Test;


/**
* @author zjj19960106
*
*/
public class Testspeed {
Class person{
Public person (String name, int age) {
This.name=name;
This.age=age;
}
private String name;
private int age;
}
@Test
public void Testspeed () {
mystack<person> stack = new arraystack<person> ();
int num = 10000000;
Long start = System.currenttimemillis ();
for (int i = 0; i < num; i++) {
Stack.push (New person ("Xing", 25));
}
Long temp = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Push Time:" + (Temp-start));
while (stack.pop () = null);
System.out.println ("Pop Time:" + (System.currenttimemillis ()-temp));
}

}

Result: The time of the list is greater than the array, the stack operation is to the top of the stack, so the use of arrays is also O (1), the array of elements moving position, and the use of the stack of each data is also encapsulated into a node, so time than the number of leader.

Java implementation stack.

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.