Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Модуль связного списка"""
- class LinkedListItem:
- """Узел связного списка"""
- def __init__(self, data=None):
- self.content = data
- self.next_item = None
- self.previous_item = None
- @property
- def next_item(self):
- """Следующий элемент"""
- return self._next
- @next_item.setter
- def next_item(self, value):
- self._next = value
- if value is not None and value.previous_item != self:
- value.previous_item = self
- @property
- def previous_item(self):
- """Предыдущий элемент"""
- return self._previous
- @previous_item.setter
- def previous_item(self, value):
- self._previous = value
- if value is not None and value.next_item != self:
- value.next_item = self
- def __repr__(self):
- return str(self.content)
- class LinkedList:
- """Связный список"""
- def __init__(self, *args):
- self.first_item = None
- if len(args) > 0:
- self.append(*args)
- @property
- def last(self):
- """Последний элемент"""
- if self.first_item is None:
- return None
- return self.first_item.previous_item.content
- def append_left(self, item):
- """Добавление слева"""
- list_item = LinkedListItem(item)
- if self.first_item is None:
- self.first_item = list_item
- list_item.next_item = list_item
- else:
- self.first_item.previous_item = list_item
- self.first_item = list_item
- return self
- def append_right(self, item):
- """Добавление справа"""
- list_item = LinkedListItem(item)
- if self.first_item is None:
- self.first_item = list_item
- list_item.next_item = list_item
- else:
- self.first_item.previous_item.next_item = list_item
- self.first_item.previous_item = list_item
- return self
- def append(self, *args):
- """Добавление справа"""
- for item in args:
- self.append_right(item)
- return self
- def remove(self, item):
- """Удаление первого вхождения"""
- if self.first_item is None:
- raise ValueError("List is empty")
- flag = True
- for current_item in self:
- if current_item.content == item:
- flag = False
- if current_item.next_item == current_item:
- self.first_item = None
- else:
- if current_item == self.first_item:
- self.first_item = current_item.next_item
- current_item.previous_item.next_item = current_item.next_item
- current_item.next_item.previous_item = current_item.previous_item
- break
- if flag:
- raise ValueError("Item not found")
- return self
- def insert(self, previous, item):
- """Вставка справа от первого вхождения"""
- if self.first_item is None:
- self.append(item)
- else:
- for current_item in self:
- if current_item.content == previous:
- list_item = LinkedListItem(item)
- list_item.next_item = current_item.next_item
- list_item.previous_item = current_item
- current_item.next_item = list_item
- break
- return self
- def __len__(self):
- count = 0
- current_item = self.first_item
- while current_item is not None:
- count += 1
- current_item = current_item.next_item
- if current_item == self.first_item:
- break
- return count
- def __iter__(self):
- current_item = self.first_item
- while current_item is not None and self.first_item is not None:
- yield current_item
- current_item = current_item.next_item
- if current_item == self.first_item:
- break
- def __getitem__(self, index: int):
- if index < 0:
- index = len(self) + index
- if index < 0 or index >= len(self):
- raise IndexError("Index out of range")
- current_index = 0
- current_item = self.first_item
- while current_index != index:
- current_item = current_item.next_item
- current_index += 1
- if current_item == self.first_item:
- break
- return current_item.content
- def __contains__(self, item):
- for current_item in self:
- if current_item.content == item:
- return True
- return False
- def __repr__(self):
- return str(self)
- def __str__(self):
- string_list = []
- for item in self:
- string_list.append(str(item))
- return '[' + ', '.join(string_list) + ']'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement