Exemplo n.º 1
0
 def enqueue(self, newValue):
     """
     Enter a new value into the queue and this method place this value on its position according the defination
     of the function after(v,u) provided by user/taskmaster.
     :param newValue: the value to be entered into the queue
     :return: None
     """
     newNode = LinkedNode( newValue )
     currentNode=self.front
     priviousNode=self.front
     if  self.front == None:
         self.front = newNode
     else:
         while currentNode!=None and self.after(newNode.value,currentNode.value) :
                     priviousNode=currentNode
                     currentNode=currentNode.link
         if currentNode!=None and self.after(currentNode.value,newNode.value) :
                 if priviousNode!=currentNode:
                    #normal insertion
                    priviousNode.link=newNode
                    newNode.link=currentNode
                 else:
                     #change of head
                     self.front = newNode
                     newNode.link=currentNode
         else:
                 #below logical part take care of nodes to be add in the end and addition of equal priority nodes
                 while  currentNode!=None and (not self.after(currentNode.value,newNode.value)):
                         priviousNode=currentNode
                         currentNode=currentNode.link
                 priviousNode.link=newNode
                 if priviousNode!=currentNode:
                     #elementing case of only single node
                     newNode.link=currentNode
 def prepend(self, new_value):
     """ Add value to the beginning of the list.
         List is modified.
         :param new_value: the value to add
         :return: None
     """
     self.__front = LinkedNode(new_value, self.__front)
Exemplo n.º 3
0
 def enqueue( self, newValue ):
     newNode = LinkedNode( newValue )
     if self.front == None:
         self.front = newNode
     else:
         self.back.link = newNode
     self.back = newNode
Exemplo n.º 4
0
 def append(self, new_value):
     """ Add value to the end of the list.
         List is modified.
         :param new_value: the value to add
         :return: None
     """
     node = self.__front
     newNode = LinkedNode(new_value)
     if node == None:
         self.__front = newNode
     else:
         while node.link != None:
             node = node.link
         node.link = newNode
    def append(self, new_value):
        # we will need to append the node to the end
        # It can have two possibilities - head is null or node already exists
        # if head is null -  create the new node,
        # - newnode next is pointed to None
        # - new node prev also is pointed to None

        newNode = LinkedNode(new_value)
        node = self.__head

        if node == None:
            newNode.next
            self.__head = newNode
        else:
            while node.next != None:
                node = node.next
            node.next = newNode
 def insert(self, cursor, new_value):
     """ Place a new value in the list, just before the position
         of the cursor. If the cursor is 'off', this method works
         just like the append method.
         List is modified.
         The cursor still refers the location it did in the list
             before this method was called.
         :param cursor: the list position
         :param new_value: the value to be placed before the cursor position
         :return: None
     """
     if cursor == self.__front:
         self.prepend(new_value)
     else:
         node = self.__front
         while node.next != cursor:
             node = node.next
         node.next = LinkedNode(new_value, cursor)
Exemplo n.º 7
0
 def push(self, newValue):
     self.top = LinkedNode(newValue, self.top)