sec stacks /linked stack implementation class lstack implements stack private link top/pointer to first element private int size/number of elements //constructors public lstack(top nullsize public lstack(int sizetop nullsize /reinitialize stack public void clear(top nullsize public void push( it/put "iton stack top new link(ittop)size++public pop(/remove "itfrom stack assert top !null "stack is empty" it top element()top top next()size--return itpublic topvalue(/return top value assert top !null "stack is empty"return top element()public int length(return size/return length figure linked stack class implementation linked stacks the linked stack implementation is simplified version of the linked list implementation the freelist of section is an example of linked stack elements are inserted and removed only from the head of the list the header node is not used because no special-case code is required for lists of zero or one elements figure shows the complete class implementation for the linked stack the only data member is topa pointer to the first (toplink node of the stack method push first modifies the next field of the newly created link node to point to the top of the stack and then sets top to point to the new link node method pop is also quite simple the variable temp stores the value of the top nodewhile ltemp keeps link to the top node as it is removed from the stack the stack is updated by setting top to point to the next element in the stack the old top node