Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON3: Solve def pop(self) for the Stack. class Node(object): \"\"\" A n

ID: 3736228 • Letter: U

Question

 USING PYTHON3: Solve  def pop(self) for the Stack.   class Node(object):     """     A node is a simple container with two pieces of information     data: the contained information     next: a reference to another node     We can create node-chains of any size.     """     def __init__(self, data, next=None):         """         Create a new node for the given data.         Pre-conditions:             data:  Any data value to be stored in the node             next:  Another node (or None, by default)         """         self._data = data         self._next = next  class Stack(object):      def __init__(self):         """     Purpose         creates an empty queue         """         self.__size = 0        # how many elements in the queue         self.__first = None    # the node chain starts here         self.__last = None     # the node chain ends here       def is_empty(self):         """     Purpose         checks if the given queue has no data in it     Pre-conditions:         queue is a queue created by create()     Return:         True if the queue has no data, or false otherwise         """         return self.__size == 0       def size(self):         """     Purpose         returns the number of data values in the given queue     Pre-conditions:         queue: a queue created by create()     Return:         The number of data values in the queue         """         return self.__size       def push(self, value):         """         Purpose             adds the given data value to the given queue         Pre-conditions:             queue: a queue created by create()             value: data to be added         Post-condition:             the value is added to the queue         Return:             (none)         """         n = Node.Node(value)          if self.is_empty():             self.__first = n             self.__last = n         else:             n = self.__first             n._next = self.__first          self.__size += 1        def pop(self):         """         Purpose             removes and returns a data value from the given queue         Post-condition:             the first value is removed from the queue         Return:             the first value in the queue, or None         """          TO BE COMPLETE             return None  

Explanation / Answer

class Node(object):

    """

    A node is a simple container with two pieces of information

    data: the contained information

    next: a reference to another node

    We can create node-chains of any size.

    """

    def __init__(self, data, next=None):

        """

        Create a new node for the given data.

        Pre-conditions:

            data: Any data value to be stored in the node

            next: Another node (or None, by default)

        """

        self._data = data

        self._next = next

class Stack(object):

    def __init__(self):

        """

    Purpose

        creates an empty queue

        """

        self.__size = 0        # how many elements in the queue

        self.__first = None    # the node chain starts here

        self.__last = None     # the node chain ends here

    def is_empty(self):

        """

    Purpose

        checks if the given queue has no data in it

    Pre-conditions:

        queue is a queue created by create()

   Return:

        True if the queue has no data, or false otherwise

        """

        return self.__size == 0

    def size(self):

        """

    Purpose

        returns the number of data values in the given queue

    Pre-conditions:

        queue: a queue created by create()

    Return:

        The number of data values in the queue

        """

        return self.__size

    def push(self, value):

        """

        Purpose

            adds the given data value to the given queue

        Pre-conditions:

            queue: a queue created by create()

            value: data to be added

        Post-condition:

            the value is added to the queue

        Return:

            (none)

        """

        n = Node(value)

        if self.is_empty():

            self.__first = n

            self.__last = n

        else:

            #n = self.__first

            n._next = self.__first

            self.__first = n

        self.__size += 1

    def pop(self):

        """

        Purpose

            removes and returns a data value from the given queue

        Post-condition:

            the first value is removed from the queue

        Return:

            the first value in the queue, or None

        """

        # if stack is empty

        if self.is_empty():

            return None

        # if stack is not empty

        else:

          

            # store the value of first node in temp

            temp = self.__first._data

            # make second node as the new first

            self.__first = self.__first._next

            self.__size -= 1

            return temp

           

ob = Stack()

ob.push(1)

ob.push(2)

ob.push(3)

ob.push(4)

while ob.is_empty() == False:

    print(ob.pop())