1 /*implementing stacks with linked lists--chain stacks2 * */3 Public classMylinkedstack {4 Publicmylinkedlist linklist;5 intitems;6 7 PublicMylinkedstack () {8Items = 0;9Linklist =Newmylinkedlist ();Ten } One A Public BooleanIsEmpty () { - returnlinklist.isempty (); - } the - Public voidPushintkey) { - Linklist.insertfirst (key); -items++; + } - + PublicLink Pop () { ALink temp =Linklist.deletefirst (); atitems--; - returntemp; - } - - Public voidDiaplaystack () { -System.out.println ("Stack--top--to--bottom"); in linklist.displaylinkedlist (); - } to + Public intsize () { - returnitems; the } * $ PublicLink GetTop () {Panax Notoginseng returnLinklist.deletefirst (); - } the + Public Static voidMain (string[] args) { AMylinkedstack stack =NewMylinkedstack (); the for(inti = 0; I < 7; i++){ + Stack.push (i); - } $ $ Stack.diaplaystack (); - Stack.pop (); - Stack.diaplaystack (); the System.out.println (Stack.size ()); - }Wuyi the}
The list has been defined in advance and can be used directly.
Stack (storage structure linked list)--java implementation