1. What is a stack?
Stack is a linear table that qualifies inserts and deletes only at the end of a table.
2. Characteristics of the stack:
1.) The stack is also known as the LIFO linear table, and the stack element has a linear relationship, that is, the predecessor-successor relationship.
2. The special of the stack is that its stack bottom is fixed, allowing only inserts and deletes at the top of the stack.
3. Stack's sequential storage structure (Java array implementation):
Stack array implementation, the bottom uses the array: public class Arraystack<t> {private object[] arr;//array first element as stack bottom as it changes small private int
Length
Data initialization public Arraystack () {arr = new object[16];
length = 0;
}//element into stack public boolean push (T data) {if (length >= arr.length) {//Determine if array expansion is required
Resize ();
} arr[length++] = data;
return true;
}//Element out stack @SuppressWarnings ("unchecked") public T pop () {if (length = 0) {
return null;
Return (T) arr[--length];
///Set the data in the array to NULL to facilitate GC recycling public void Clear () {for (int i = 0; i < length; i++) {
Arr[length] = null;
length = 0;
}//Array expansion capacity private void resize () {object[] temp = new Object[arr.length * 3/2 + 1]; for (int i = 0; i < length;
i++) {Temp[i] = Arr[i];
Arr[i] = null;
} arr = temp;
//Get the number of elements in the stack public int length () {return length;
//Determine if the array is null public boolean IsEmpty () {return length = 0;
///print stack element public String toString () {StringBuilder sb = new StringBuilder ();
for (int i = 0; i < length; i++) {Sb.append (arr[i].tostring ());
if (i!= length-1) {sb.append (",");
} return sb.tostring (); public static void Main (string[] args) {arraystack<integer> stack = new arraystack<
Integer> ();
Long start = System.currenttimemillis (); for (int i = 0; i < 1000000 i++) {Stack.push (i);//into stack} long temp = System.cur
Renttimemillis (); SYSTEM.OUT.PRINTLN ("Push Time:" + (Temp-start)); while (Stack.pop ()!= null) {;//Out Stack} System.out.println ("Pop Time:" + (System.curr
Enttimemillis ()-temp); }
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Run time:
push time:257ms
pop time:5ms
4. Stack chain-type storage structure (Java list implementation):
node element public
class Node<t> {
private node<t> prev;//Remember the previous node of the current node
private T Data//Field
Pub LIC node () {
} public
node (T data, node<t> prev) {
this.data = data;
This.prev = prev;
}
Public node<t> GetPrev () {return
prev;
}
public void Setprev (node<t> prev) {
this.prev = prev;
}
Public T GetData () {return
data;
}
public void SetData (T data) {
This.data = Data
}
}
Stack of linked list implementation, the bottom of the use of single linked list: public class Linkedstack<t> {private node<t> top;//stack tip pointer (chain footer) Private I NT size;
Stack length//Data Initialization public linkedstack () {top = null;
size = 0; }//Node into stack public boolean push (T data) {node<t> NewNode = new Node<t> (data,to p); The top of the new node is set to top = NewNode;
Setting the new node to the top of the stack pointer size++;
return true;
}//Node out stack public T POPs () {if (top!= null) {node<t> tempnode = top; top = Top.getprev ();
Setting the precursor node of the top pointer of the stack as the top pointer size--;
return Tempnode.getdata ();
return null;
//Determine whether the stack is empty public boolean isempty () {return size = = 0;
//Get the length of the stack public int length () {return size; ///print stack element public String toString () {
node<t> node = top;
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < size; i++) {Sb.append (Node.getdata (). toString ());
if (i!= size-1) {sb.append (",");
node = Node.getprev ();
Return Sb.reverse (). toString (); public static void Main (string[] args) {linkedstack<integer> stack = new LINKEDSTACK&L
T;integer> ();
Long start = System.currenttimemillis (); for (int i = 0; i < 1000000 i++) {Stack.push (i);//into stack} long temp = System.cur
Renttimemillis ();
SYSTEM.OUT.PRINTLN ("Push Time:" + (Temp-start)); while (Stack.pop ()!= null) {;
Out Stack} System.out.println ("Pop Time:" + (System.currenttimemillis ()-temp)); }
}
Run time:
push time:986ms
pop time:15ms
5. Two ways to achieve comparison:
By comparing the stack and the stack time, we can see that the array implementation has obvious advantages in performance, because the stack of array implementation does not need to move a large number of elements when adding and deleting elements, but it needs to be copied when enlarging the capacity of the array. However, the stack of the list implementation needs to wrap the data into node, when the stack needs to take out the data from node, while maintaining the stack top pointer and the precursor pointer.
6. The stack in the JDK API:
1.) Java.util.Stack: A common sequential stack, the underlying base is based on an array implementation. This class is thread-safe.
2.) Java.util.LinkedList: A two-way linked list, thread is not safe, can be used as a stack. In a multithreaded environment, you can use the tools method of the collections class to transform it into a thread-safe class.
3. All two classes provide a push (), pop (), Peek (), and so on.
Author: csdn Blog zdp072