stack array to achieve one : advantages: Into the stack and out of the stack faster, the disadvantage: limited length (sometimes this is not a disadvantage)
public class Stack {private int top =-1;
Private object[] OBJS;
public Stack (int capacity) throws exception{if (capacity < 0) throw new Exception ("Illegal Capacity:" +capacity);
OBJS = new Object[capacity]; public void push (Object obj) throws exception{if (top = objs.length-1) throw new Exception ("Stack is full!")
;
Objs[++top] = obj;
Public Object POPs () throws exception{if (top = 1) throw new Exception ("Stack is empty!");
return objs[top--];
public void Dispaly () {System.out.print ("bottom-> top: |"); for (int i = 0; I <= top; i++) {System.out.print (objs[i]+) |
");
} System.out.print ("\ n");
public static void Main (string[] args) throws exception{stack s = new stack (2);
S.push (1);
S.push (2);
S.dispaly ();
System.out.println (S.pop ());
S.dispaly ();
S.push (99);
S.dispaly ();
S.push (99); }
}
Bottom-> Top: | 1 | 2 |
2
Bottom-> top: | 1 |
Bottom-> Top: | 1 | |
Exception in thread ' main ' java.lang.Exception:Stack is full!
At Stack.push (stack.java:17) at
Stack.main (stack.java:44)
The time complexity of data entry stack and stack is constant O (1)
stack Array Implementation two : advantages: No length limitations, disadvantages: Slow into the stack
Import Java.util.Arrays;
public class Unboundedstack {private int top =-1;
Private object[] OBJS;
Public Unboundedstack () throws exception{, this (10); public unboundedstack (int capacity) throws exception{if (capacity < 0) throw new Exception ("Illegal capacity:
"+capacity);
OBJS = new Object[capacity];
public void push (Object obj) {if (top = objs.length-1) {this.enlarge ();
} Objs[++top] = obj;
Public Object POPs () throws exception{if (top = 1) throw new Exception ("Stack is empty!");
return objs[top--];
private void enlarge () {int num = OBJS.LENGTH/3;
if (num = = 0) num = 1;
OBJS = arrays.copyof (OBJS, objs.length + num);
public void Dispaly () {System.out.print ("bottom-> top: |"); for (int i = 0; I <= top; i++) {System.out.print (objs[i]+) |
");
} System.out.print ("\ n");
public static void Main (string[] args) throws exception{unboundedstack US = new Unboundedstack (2);
Us.push (1); Us.pUsh (2);
Us.dispaly ();
System.out.println (Us.pop ());
Us.dispaly ();
Us.push (99);
Us.dispaly ();
Us.push (99);
Us.dispaly (); }
}
Bottom-> Top: | 1 | 2 |
2
Bottom-> top: | 1 |
Bottom-> Top: | 1 | |
Bottom-> Top: | 1 | 99 | 99 |
Because the stack is implemented by an array, the length of the array is fixed, when the stack space is insufficient, the original array data must be copied to a longer array, considering the stack may need to be replicated, the average need to replicate N/2 data items, so the time complexity of the stack is O (N), The time complexity of the stack is still O (1)
Stack single chain table implementation : No length limit, and the stack and the speed of the stack is very fast
public class LinkedList {Private class data{private Object obj;
Private Data next = null;
Data (Object obj) {this.obj = obj;
} private Data a = null;
public void Insertfirst (Object obj) {Data data = new data (obj);
Data.next = A;
i = data;
The public Object Deletefirst () throws exception{if (A/= null) throw new Exception ("empty!");
Data temp = i;
i = First.next;
return temp.obj;
public void display () {if (A/= null) System.out.println ("Empty");
System.out.print ("Top-> bottom: |");
Data cur = i;
while (cur!= null) {System.out.print (cur.obj.toString () + "|");
cur = cur.next;
} System.out.print ("\ n"); }
}
public class Linkedliststack {
private LinkedList ll = new LinkedList ();
public void push (Object obj) {
ll.insertfirst (obj);
}
Public Object POPs () throws exception{return
Ll.deletefirst ();
}
public void display () {
ll.display ();
}
public static void Main (string[] args) throws exception{
linkedliststack lls = new Linkedliststack ();
Lls.push (1);
Lls.push (2);
Lls.push (3);
Lls.display ();
System.out.println (Lls.pop ());
Lls.display ();
}
Top-> Bottom: | 3 | 2 | 1 |
3
The time complexity of data entry stack and stack is constant O (1)