链表
单链表
__author__ = "Hao Rui Chun"
class Node():
def __init__(self, item):
self.item = item
self.next = None
class SingleLink():
def __init__(self):
self._head = None
def add(self, item):
node = Node(item)
node.next = self._head
self._head = node
def travel(self):
cur = self._head
while cur:
print(cur.item)
cur = cur.next
def len(self):
cur = self._head
i = 0
while cur:
i += 1
cur = cur.next
return i
def isEmpty(self):
return self._head == None
def append(self, item):
node = Node(item)
if self.isEmpty():
self._head = node
return
else:
cur = self._head
while cur.next:
cur = cur.next
cur.next = node
def insert(self,index,item):
node = Node(item)
cur = self._head
count = 0
if self.isEmpty():
self.add(item)
elif index > self.len()-1:
self.append(item)
else:
while count < index-1:
cur = cur.next
count += 1
node.next = cur.next
cur.next = node
def remove(self,item):
if self.isEmpty():
return False
else:
cur = self._head
pre =None
while cur.next:
if cur.item == item:
if cur == self._head:
self._head = cur.next
else:
pre.next =cur.next
return
else:
pre =cur
cur =cur.next
else:
if cur.item == item:
if cur == self._head:
self._head = None
else:
pre.next = cur.item
def search(self,item):
cur =self._head
while cur.next:
if cur.item == item:
return True
else:
cur =cur.next
if cur.item == item:
return True
else:
return False
if __name__ == '__main__':
link = SingleLink()
link.add(3)
link.add(4)
link.add(5)
link.append(100)
link.insert(1,200)
link.travel()
print(link.search(100))
link.remove(3)
link.travel()
单向循环链表
__author__ = "Hao Rui Chun"
class Node():
def __init__(self, item):
self.item = item
self.next = None
class WhileLink(object):
def __init__(self,node=None):
if node:
node.next = node
self._head = node
def isEmpty(self):
return self._head == None
def len(self):
if self.isEmpty():
return 0
cur = self._head
count = 1
while cur.next != self._head:
count += 1
cur = cur.next
return count
def travel(self):
if self.isEmpty():
return None
cur = self._head
while cur.next != self._head:
print(cur.item,end=' ')
cur = cur.next
print(cur.item)
def add(self,item):
node = Node(item)
cur = self._head
if self.isEmpty():
node.next = self._head
self._head = node
return
while cur.next != self._head:
cur = cur.next
node.next = self._head
self._head = node
cur.next = self._head
def append(self,item):
node = Node(item)
cur = self._head
if self.isEmpty():
self._head = node
node.next = self._head
return
while cur.next != self._head:
cur = cur.next
node.next = cur.next
cur.next = node
def insert(self,index,item):
if self.isEmpty():
self.add(item)
elif index > self.len()-1:
self.append(item)
else:
node = Node(item)
cur = self._head
count = 0
while count < index-1:
cur = cur.next
count +=1
node.next = cur.next
cur.next = node
def remove(self,item):
cur = self._head
pre =None
while cur.next != self._head:
if cur.item == item:
if cur == self._head:
rear = self._head
while rear.next != self._head:
rear =rear.next
self._head = cur.next
rear.next =self._head
else:
pre.next =cur.next
return
else:
pre = cur
cur = cur.next
if cur.item == item:
if cur == self._head:
self._head = None
else:
pre.next = cur.next
def search(self,item):
if self.isEmpty():
return False
cur = self._head
while cur.next != self._head:
if cur.item == item:
return True
else:
cur =cur.next
if cur.item == item:
return True
else:
return False
if __name__ == '__main__':
lis = WhileLink()
lis.append(3)
lis.append(4)
lis.append(5)
lis.add(7)
lis.add(8)
lis.add(9)
lis.insert(2,100)
lis.travel()
print(lis.search(100))
print(lis.len())
lis.remove(5)
lis.travel()