Beispiel #1
0
 def append(self, value):
     if self.head is None:
         self.head = Node(value)
         self.tail = self.head
     else:
         self.tail.next = Node(value)
         self.tail = self.tail.next
    def push(self, value):
        new_top = Node(value)

        if self.top is None:
            self.top = new_top
        else:
            new_top.next = self.top
            self.top = new_top
Beispiel #3
0
    def insert_after(self, node, value):
        n = self.head

        while n.next is not None:
            if n == node:
                new_node = Node(value)
                new_node.next = n.next
                n.next = new_node
                return

            n = n.next
Beispiel #4
0
class LinkedStack:
    def __init__(self):
        self.last_ = None

    def push(self, item):
        self.last_ = Node(item, self.last_)
        return True

    def pop(self):
        item = self.last_.get_item()
        self.last_ = self.last_.get_next()
        return item
    def __init__(self, nodes: Optional[List] = None) -> None:
        """
        This is called when a new LinkedList object is instantiated

        :param nodes: (list) The start list (default None)
        """
        self.head = None
        if nodes is not None:
            node = Node(data=nodes.pop(0))
            self.head = node
            for elem in nodes:
                node.next = Node(data=elem)
                node = node.next
Beispiel #6
0
	def insert(self, index, item):
		if index < 0:
			index = 0
		elif index > self.list_size:
			index = self.list_size

		if index == 0:
			node = Node(item, self.head)
			self.head = node
		else:
			node = self._get_node(index - 1)
			node.link = Node(item, node.link)
		self.list_size += 1
Beispiel #7
0
    def insert(self, pos, value):
        node = self.head
        index = 0

        while node:
            if index == pos - 1:
                new_node = Node(value)
                new_node.next = node.next
                node.next = new_node
                return True

            node = node.next
            index += 1
        return False
Beispiel #8
0
    def insert(self, index, item):
        if self.is_full():
            return False
        if index < 0:
            index = 0
        elif index > self.count:
            index = self.count

        if index == 0:
            self.head = Node(item, self.head)
        else:
            node = self._get_node(index - 1)
            node.link = Node(item, node.link)
        self.count += 1
        return True
Beispiel #9
0
 def append(self, item):
     node = Node(item)
     if self.is_empty():
         self.front = node
     else:
         self.rear.link = node
     self.rear = node
     self.queue_size += 1
    def add_first(self, node: Node) -> None:
        """
        Add a new node as the first position in the linked list

        :param node: (Node) The new node instance to add the linked list
        :return: void
        """
        node.next = self.head
        self.head = node
Beispiel #11
0
    def push(self, value):
        new_node = Node(value)

        if self.head is None:
            self.head = new_node
            self.tail = self.head
        else:
            self.tail.next = new_node
            self.tail = new_node
Beispiel #12
0
    def enqueue(self, item):
        node = Node(item)

        if self.is_empty():
            self.last = node
            self.first = self.last
        else:
            self.last.set_next(item)
            self.last = item
        return True
Beispiel #13
0
    def merge_node_set_into_node(self, original: Node, attached: Set[Node]):
        # border attribute should be true if any are borders
        original.is_border = any([i.is_border for i in attached])

        # all nodes in attached should be removed from grid
        for i in attached:
            if i.position != original.position:
                i.deleted = True

        # position should cover all attached
        original.position = set.union(*[i.position for i in attached])

        # final outflow is the outflow for all nodes except outflows to itself
        all_possible_outflows = []
        for i in attached:
            for j in i.outflow:
                if not overlaps(j.position, original.position):
                    if not any([overlaps(j.position, k.position) for k in all_possible_outflows]):
                        all_possible_outflows.append(j)

        original.outflow = all_possible_outflows
Beispiel #14
0
	def add_sorted(self, item):
		if self.head is None:
			self.head = Node(item)
			self.count += 1
			return
		current = self.head
		previous = None
		while current is not None:
			if current.item < item:
				previous = current
				current = current.link
			else:
				break

		if previous is not None:
			node = Node(item, previous.link)
			previous.link = node
			self.count += 1
		else:
			self.head = Node(item, self.head)
			self.count += 1
    def add_after(self, target_node_data: Any, new_node: Node) -> None:
        """
        Add a new node after the given target value from the linked list

        :param target_node_data: The target data value
        :param new_node: (Node) The new node instance to add after the given target value from the linked list
        :return: void
        :raises:
            Exception: List is empty
            Exception: Node with data '%s' not found
        """
        if not self.head:
            raise Exception("List is empty")

        for node in self:
            if node.data == target_node_data:
                new_node.next = node.next
                node.next = new_node
                return

        raise Exception("Node with data '%s' not found" % target_node_data)
Beispiel #16
0
 def push(self, item):
     self.top = Node(item, self.top)
     self.stack_size += 1
Beispiel #17
0
 def _add(self, value):
     self.length += 1
     self.head = Node(value, self.head)
Beispiel #18
0
 def set_border(self, i: int, j: int, node: Node):
     node.is_border = i == 0 or j == 0 or i == len(
         self.node_grid) - 1 or j == len(self.node_grid[0]) - 1
Beispiel #19
0
 def _addFirst(self, value):
     self.length = 1
     node = Node(value)
     self.head = node
     self.tail = node
Beispiel #20
0
 def _add(self, value):
     self.length += 1
     node = Node(value)
     if self.tail:
         self.tail.pointer = node
     self.tail = node
Beispiel #21
0
 def to_node(self, row: int, col: int, altitude: float):
     node = Node()
     node.altitude = altitude
     node.position = {(row, col)}
     node.deleted = False
     return node
Beispiel #22
0
 def push(self, item):
     self.last_ = Node(item, self.last_)
     return True
Beispiel #23
0
	def double_list(self):
		current = self.head
		while current is not None:
			node = Node(current.item, current.link)
			current.link = node
			current = current.link.link