Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 15 20:02:51 2015
- @author: gregory.mendes
- """
- class LinkedList:
- def __init__(self, value = None):
- self.value = value
- self.next = None
- def is_empty(self):
- if(self.head) == None:
- return True
- else:
- return False
- def __len__(self):
- if self.is_empty() == True:
- return 0
- else:
- if self.next != None:
- return 1 + self.next.size()
- else:
- return 1
- def head(self):
- return self.value
- def tail(self):
- if self.is_empty == True or self.next == None:
- return self.value
- else:
- return self.value + self.tail()
- def extend(self, L):
- if self.is_empty() == True:
- self.value = L.value
- # terminar aqui
- def append(self, value):
- if self.value is None:
- self.value = value
- elif self.next is None:
- self.next = LinkedList(value)
- else:
- self.next.append(value)
- def prepend(self, value):
- if self.value is None:
- self.value = value
- else:
- link = LinkedList(self.value)
- link.next = self.next
- self.value = value
- self.next = link
- def __eq__(self, other):
- if not type(other) is LinkedList:
- return False
- if other.is_empty():
- if self.is_empty():
- return True;
- else:
- return False
- if other.value == self.value and \
- other.next == self.next:
- return True
- else:
- return False
- def __str__(self):
- return "[" + str(self.__str_values__()) + "]"
- def __str_values__(self):
- if self.is_empty():
- return ""
- elif self.next is None:
- return str(self.value)
- else:
- return str(self.value) + ", " + self.next.__str_values__()
- def __bool__(self):
- if(self.head) == None:
- return False
- else:
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement