Stack definition
Stack: a special serial form of data structure that is unique in that it allows only the link serial or array
One end of the INSERT, delete operation. Its implementation can be realized through one-dimensional array and link serial.
Tips: Simply put, the stack is actually a linear table with limited operations. is a LIFO data structure that can use a number of
The form of a group or a linked list is implemented.
The operation of the stack performance form:
Stack Implementation
Know the definition of the stack, let's look at the implementation of the stack. The first thing we need to understand is what the stack does.
Define its actions through an interface.
Package com.kiritor;
/**
* Stack operation definition
* @author kiritor*/public
interface Stack<t> {
/* null
/Boolean isempty (); c6/> * * Empty stack
/void clear ();
/* Stack
/T pop ();
/* into the stack * *
boolean push (T data);
* * Stack length
/int length ();
/* View the elements of the top of the stack, but do not remove it
/T peek ();
/* Returns the position of the object in the stack *
/int search (t);
}
array implementation of stacks
Array implementations of stacks, using arrays at the bottom
Package com.kiritor; Array implementation of/** * stack * @author kiritor*/public class arraystack<t> implements stack<t>{private t[] T
= (t[]) new object[16];//stack space defaults to private int size = 0;
@Override public Boolean IsEmpty () {return size==0; @Override public void Clear () {for (int i = 0;i<t.length;i++) t[i]=null;//its reference to NULL to facilitate GC to reclaim size = 0;//stack
Size 0}/* Stack operation/* @Override public T pop () {if (size==0) return null;
else {T tmp = t[size-1];
T[SIZE-1]=NULL;//facilitates GC recovery of size--;
return TMP;
} @Override public Boolean push (T data) {if (size>=t.length) {//Stack space is full, the expansion of resize () is required;
} T[size++]=data;
return true;
public void Resize () {t[] tmp = (t[]) new object[t.length*2];
for (int i = 0;i<t.length;i++) {Tmp[i]=t[i];
t[i]=null;//facilitates GC processing} t = tmp;
TMP = null;//facilitates GC processing, increases program efficiency} @Override public int length () {return size;
/* View top of stack element, but do not remove/@Override public T Peek () { if (size==0) return null;
else {return t[size-1]; }/* The following table starts at 1 */@Override public int search (T-t) {for (int i= 0;i<size;i++) if (T.equals (this.t[i)) Retu
RN i+1;
return 0;
@Override public String toString () {StringBuilder sb = new StringBuilder ();
Sb.append ("arraystack:\n[\n");
for (int i = size-1 I >=0; i--) {sb.append ("" +t[i].tostring ());
if (i!= size + 1) {sb.append ("\ n");
} sb.append ("]");
return sb.tostring ();
}
}Test code:
public static void Main (string[] args) {
arraystack<string> arraystack = new arraystack<> ();
Arraystack.push ("C language");
Arraystack.push ("C + +");
Arraystack.push ("JAVA");
Arraystack.push ("Data Structure");
Arraystack.pop ();
Arraystack.peek ();
Arraystack.pop ();
System.out.println (Arraystack.tostring ());
}
Output results:
the realization of the chain list of the stack
Package com.kiritor; /** * Stack List implementation * @author kiritor*/public class linkstack<t> implements stack<t>{/* to encapsulate the data into a node */class No
De {private Node pre;
Private T data;
}/* Top of stack pointer/private Node;
private int size;//stack size public linkstack () {this.top = null;
this.size = 0;
@Override public Boolean IsEmpty () {return size==0;
@Override public void Clear () {top = null;
size = 0;
@Override public T POPs () {if (top!= null) {t = Top.data;
Change top of stack pointer = Top.pre;
size--;
return t;
return null;
@Override public boolean push (T data) {node node = new Node ();
Node.data = data;
Node.pre = top;
Change top of stack pointer = node;
size++;
return true;
@Override public int Length () {return size;
@Override Public T Peek () {return top.data;
}/* The following table starts at 1 * * @Override public int search (T t) {int count=0;
For (Node node= top;node.pre!=null;node = node.pre) {count++;
if (T.equals (node.data)) return size-count;
return 0;
@Override public String toString () {StringBuilder sb = new StringBuilder ();
Sb.append ("Linkstack:" +length () + "\n[\n");
int count=0;
for (node node = top;node!=null;node=node.pre) {count++;
Sb.append ("" +node.data.tostring ());
if (count!= size + 1) {sb.append ("\ n");
} sb.append ("]");
System.out.println (count);
return sb.tostring ();
}
}Test code:
public static void Main (string[] args) {
linkstack<string> arraystack = new linkstack<> ();
Arraystack.push ("C language");
Arraystack.push ("C + +");
Arraystack.push ("JAVA");
Arraystack.push ("Data Structure");
System.out.println (Arraystack.tostring ());
}
The results are not posted.
Because of the particularity of the stack operation, the efficiency of the stack implemented in the array mode is higher than that of the linked list.
The reason is that the array is accessed faster. Second, because the operation on the top of the stack does not involve too much element movement
But using chained implementations to wrap data into node, take data from it, and maintain stack-top pointers and the precursor pointers.