Stack implementation of data structures (JAVA) (I)
// Stack top element public class StackElement
{Private StackElement
NextElement; private T data; public StackElement (T data) {this. data = data;} public StackElement
GetNextElement () {return nextElement;} public void setNextElement (StackElement
NextElement) {this. nextElement = nextElement;} public T getData () {return data;} public void setData (T data) {this. data = data ;}}
Tool Stack:
Public class SeqStack
{Private StackElement
Obj; // private int size used to save data; // number of elements public SeqStack () {this. size = 0;} // Add an element public boolean push (T data) {if (data! = Null) {StackElement
TempElement = new StackElement (data); tempElement. setNextElement (this. obj); this. obj = tempElement; tempElement = null; // reclaim this. size ++; return true;} return false;} // a public boolean pop () {if (this. size> 0) {this. obj = this. obj. getNextElement (); this. size --; return true;} return false;} // clear the public void clear () {this. size = 0; this. obj = null;} // get the stack top element public T get () {return obj = null? Null: obj. getData () ;}// stack size public int size () {return this. size;} public static void main (String [] arg) {SeqStack
SelStack = new SeqStack (); for (int I = 0; I <10; I ++) selStack. push (I); System. out. println (The size of the stack: + selStack. size (); int size = selStack. size (); for (int I = 0; I
Running result: