Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, init_data):
- self.data = init_data
- self.next = None
- def get_data(self):
- return self.data
- def get_next(self):
- return self.next
- def set_data(self, new_data):
- self.data = new_data
- def set_next(self, new_next):
- self.next = new_next
- def __str__(self):
- return str(self.data)
- prev = None
- cur = head
- while cur.next is not None:
- prev = cur
- cur = cur.next
- if prev: #in case the head is the tail
- prev.next = None
- class List:
- def __init__(self):
- self._nodes = None
- def push(self, node):
- node.set_next(self._nodes)
- self._nodes = node
- return self
- def pop(self):
- if self._nodes is None:
- return None
- temp = self._nodes
- self._nodes = temp.get_next()
- return temp
- def __len__(self):
- l = 0
- n = self._nodes
- while n is not None:
- n = n.get_next()
- l += 1
- return l
- def remove(self, node):
- n = self._nodes
- p = None
- while n is not None:
- if n is node:
- p.set_next(n.get_next())
- n.set_next(None)
- return True
- p = n
- n = n.get_next()
- return False
- def del_from_end(self):
- if self.head is None:
- return "No node to delete"
- else:
- current = self.head
- while current.next.next is not None:
- current = current.next
- current.next = None
Add Comment
Please, Sign In to add comment