using static internal classes as a single-linked list
1 Packagedb;2 3 /**4 * Only the head node implementation stack.5 * 6 * @authorFightzhao7 * 8 */9 Public classStack<e> {Ten /* One * There are the following methods to stack push (E x) out of stack pop () stack top element top () A */ - Private Static classNode<e> { - PublicNode<e>Next; the PublicE data; - - PublicNode (E data, node<e>next) { - This. data =data; + This. Next =Next; - } + } A at PrivateNode<e>head; - Private intthesize; - - PublicStack () { -Head =NULL; - } in - Public intsize () { to returnthesize; + } - the Public voidpush (E data) { *Node<e> Pnode =NewNode<e>(data, head); $Head =Pnode;Panax Notoginsengthesize++; - } the + Public voidpop () { AHead =Head.next; thethesize--; + } - $ PublicE Top () { $ returnHead.data; - } - the Public voidprintall () { -Node<e> Pnode =head;Wuyi while(Pnode! =NULL) { the System.out.println (pnode.data); -Pnode =Pnode.next; Wu } - About } $}
Using a linked list with only the head node to implement the stack