1 PackageiYou.neugle.list;2 3 Public classMyseqstack<t> {4 PrivateStack stack =NewStack ();5 6 classStack {7 Public intMaxSize = 10;8 Publict[] data = (t[])NewObject[maxsize];9 Public inttop =-1;//stack Top pointerTen } One A //Initialize Stack - Public voidSeqstackinit () { - This. Stack.top = 1; theSYSTEM.OUT.PRINTLN ("Stack initialization succeeded!")); - } - - //into the stack + Public voidSeqstackpush (T data) { - if( This. Stack.top = = This. stack.maxsize-1) { +System.out.println ("The contents of the stack are full!")); A return; at } - This. stack.data[++ This. stack.top] =data; - } - - //out of the stack - PublicT Seqstackpop () { in if( This. Stack.top = =-1) { -System.out.println ("The contents of the stack are empty!")); to } +T data = This. Stack.data[stack.top]; - This. stack.data[ This. stack.top] =NULL; the This. stack.top--; * returndata; $ }Panax Notoginseng - //get top of stack element the PublicT Seqstackpeek () { + if( This. Stack.top = =-1) { ASystem.out.println ("The contents of the stack are empty!")); the return NULL; + } - return This. stack.data[ This. Stack.top]; $ } $}
Java data structure and algorithm------stack