def insertNode(self, afterNode, newData):
        newNode = Node(newData)

        if afterNode is None:
            return

        nextNode = afterNode.next
        afterNode.next = newNode
        newNode.next = nextNode
        self.size += 1
    def insertHead(self, newData):
        newNode = Node(newData)

        if self.head is None:
            self.head = newNode
            self.size += 1
            return

        newNode.next = self.head
        self.head = newNode
def testCaseForIntersected():
    l1 = LinkedList()

    currentNode = l1.head
    for i in range(5):
        n = Node(i)
        if currentNode is None:
            l1.head = n
            currentNode = l1.head
        else:
            currentNode.next = n
            currentNode = currentNode.next


    l1.printList()

    l2 = LinkedList()

    currentNode = l1.head
    counter = 0
    while counter < 3:
        currentNode = currentNode.next
        counter +=1

    l2.head = currentNode
    l2.printList()

    # call the functions
    result = isIntersectBtw(l1,l2)
    print("Intersect Result: " + str(result))
Exemple #4
0
    def enqueue(self, newData):
        newNode = Node(newData)

        if self.first is None and self.last is None:
            # first element
            self.first = newNode
            self.last = newNode
        else:
            self.last.next = newNode
            self.last = newNode

        self.size += 1
        print("New data {" + str(newData) + "} was pushed into queue")
    def insertTail(self, newData):
        newNode = Node(newData)

        if self.head is None:
            self.head = newNode
            self.size += 1
            return

        lastNode = self.head

        while lastNode.next:
            lastNode = lastNode.next

        lastNode.next = newNode
        self.size += 1
    def get_initial_graph(self):
        # creating a dictionary based on city names
        # each entry of this dict holds a node that's created with this name [i've use it kinda like hashMap in java]
        graph_details_dictionary = {}
        # creating graph nodes with their names and heuristic values
        for x in self.initialStateAdjacencyList:
            current_node = Node(name=x, heuristic=self.initialStateHeuristics[x])
            graph_details_dictionary.update({x: current_node})

        # fill children field of each node and assign pointers to it's children with it's corresponding cost
        # children field is a dictionary consist of children nodes and their cost
        # in this form -> a.children = {"city a" : 200 , "city b" : 400}
        for x in graph_details_dictionary:
            for y in self.initialStateAdjacencyList[x]:
                graph_details_dictionary[x].children.update(
                    {graph_details_dictionary[y]: self.initialStateAdjacencyList[x][y]})  # update children dictionary
                # like the form specified above :{"child city x" :200}

        return graph_details_dictionary
    def reverse(self, newData=None):

        reversedStack = Stack()
        if not newData:
            for i in range(self.size):
                reversedStack.pushNode(self.pop())
        else:
            isAddedNewNode = False
            for i in range(self.size):
                # big to small
                newNode = Node(newData)

                if newData < self.peek().data and not isAddedNewNode:
                    #insert first
                    reversedStack.pushNode(newNode)
                    isAddedNewNode = True

                reversedStack.pushNode(self.pop())
            if not isAddedNewNode:
                reversedStack.pushNode(newNode)

        return reversedStack
Exemple #8
0
def testCaseForLoopDetection():
    #circular linkedlist

    l1 = LinkedList()

    currentNode = l1.head
    for i in range(5):
        n = Node(i)
        if currentNode is None:
            l1.head = n
            currentNode = l1.head
        else:
            currentNode.next = n
            currentNode = currentNode.next

    currentNode.next = l1.head.next.next
    # l1.printList()

    # call the functions
    result = getBeginningNodeForLoop(l1)
    if result:
        print("Started Node for loop: " + str(result.data))
Exemple #9
0
 def push(self, newData):
     newNode = Node(newData)
     self.pushNode(newNode)