class linkedList:
def __init__(self, l=[]):
if(type(l) == list):
self.val = l[0]
if(not l[1:]):
self.next = None
self.tail = self
else:
self.next = linkedList(l[1:])
self.tail = self.next
else:
self.val = l
self.next = None
self.tail = self
def __getitem__(self, key):
if(key < 0):
raise IndexError("Index Out Of range")
elif(key == 0):
return self.val
return self.next[key - 1]
def __setitem__(self, key, val):
if(key < 0):
raise IndexError("Index Out Of range")
elif(key == 0):
self.val = val
else:
self.next[key - 1] = val
def __add__(self, y):
if(self.next):
self.next + y
else:
self.next = linkedList(y)
def __add__(self, y):
if(self.next):
self.next + y
else:
self.next = linkedList(y)
def __radd__(self, y):
return y + self.list()
def __mul__(self, y):
if(y == 0):
return None
else:
return self + self * (y - 1)
def __rmul__(self, y):
return self * y
def __contains__(self, x):
if(self.next):
return (self.val == x) or (x in self.next)
return (self.val == x)
def __len__(self):
if(self.next):
return 1 + len(self.next)
else:
return 1
def list(self):
if(self.next):
return [self.val] + self.next.list()
return [self.val]
def __iter__(self):
return linkediter(self)
def append(self, val):
if(self.next):
self.next.append(val)
return None
else:
self.next = linkedList(val)
return self.next
def __str__(self):
if(self.next):
return str(self.val) + "->" + str(self.next)
return str(self.val)
class linkediter:
def __init__(self, l):
self.list = l
def __next__(self):
if(self.list == None):
raise StopIteration
c = self.list[0]
self.list = self.list.next
print(self.list)
return c
链表操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这道题还是很简单的,我竟然十分钟不到就AC了,而且效率最佳12ms,有点不敢相信。 83. Remove Dupl...