implementing stack with singly linked list in this sectionwe demonstrate use of singly linked list by providing complete python implementation of the stack adt (see section in designing such an implementationwe need to decide whether to model the top of the stack at the head or at the tail of the list there is clearly best choice herewe can efficiently insert and delete elements in constant time only at the head since all stack operations affect the topwe orient the top of the stack at the head of our list to represent individual nodes of the listwe develop lightweight node class this class will never be directly exposed to the user of our stack classso we will formally define it as nonpublicnested class of our eventual linkedstack class (see section for discussion of nested classesthe definition of the node class is shown in code fragment class node"""lightweightnonpublic class for storing singly linked node ""slots _element _next streamline memory usage def init (selfelementnext)self element element self next next initialize node' fields reference to user' element reference to next node code fragment lightweight node class for singly linked list node has only two instance variableselement and next we intentionally define slots to streamline the memory usage (see page of section for discussion)because there may potentially be many node instances in single list the constructor of the node class is designed for our convenienceallowing us to specify initial values for both fields of newly created node complete implementation of our linkedstack class is given in code fragments and each stack instance maintains two variables the head member is reference to the node at the head of the list (or noneif the stack is emptywe keep track of the current number of elements with the size instance variablefor otherwise we would be forced to traverse the entire list to count the number of elements when reporting the size of the stack the implementation of push essentially mirrors the pseudo-code for insertion at the head of singly linked list as outlined in code fragment when we push new element onto the stackwe accomplish the necessary changes to the linked structure by invoking the constructor of the node class as followsself head self node(eself headcreate and link new node note that the next field of the new node is set to the existing top nodeand then self head is reassigned to the new node