Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, data, next = None):
- self.data = data
- self.next = next
- def __getitem__(self, index):
- head = self
- while index > 0:
- if head.next is not None:
- head = head.next
- index -= 1
- else:
- raise IndexError
- return head.data
- def set(self, new_item, index):
- head = self
- while index > 0:
- if head.next != None:
- head = head.next
- index -= 1
- else:
- raise IndexError
- head.data = new_item
- return head.data
- def insert(self, new_item, index):
- head = self
- while index > 0:
- if head.next != None:
- head = head.next
- index -= 1
- else:
- return None
- head.next = Node(new_item, head.next)
- if __name__ == '__main__':
- n = Node(2, Node(8, Node(10)))
- n.insert(7, 2)
- while n != None:
- print(n.data)
- n = n.next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement