链表
单向链表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/52e7015bc940a342679bdb3c29e952b9.jpeg#pic_center)
class SingleNode(object):
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
def __init__(self, node=None):
self.__head = node
def is_empty(self):
return self.__head == None
def length(self):
cur = self.__head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
cur = self.__head
while cur != None:
print(cur.elem, end=" ")
cur = cur.next
print()
def add(self, item):
node = SingleNode(item)
node.next = self.__head
self.__head = node
def append(self, item):
node = SingleNode(item)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur != None:
cur = cur.next
cur.next = node
def insert(self, item, pos):
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
node = SingleNode(item)
cur = self.__head
count = 0
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
cur = self.__head
pre = None
while cur!= None:
if cur.elem == item:
if cur == self.__head:
self.__head = cur.next
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next
def search(self, item):
cur = self.__head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
if __name__ == "__main__":
ll = SingleLinkList()
print(ll.is_empty())
print(ll.length())
ll.append(1)
ll.add(3)
print(ll.is_empty())
print(ll.length())
ll.insert(2, 1)
ll.travel()
print(ll.search(2))
print(ll.search(4))
ll.add(4)
ll.remove(2)
ll.travel()
ll.remove(4)
ll.travel()
ll.remove(1)
ll.travel()
双向链表![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fb28c036aaffdade623fab52949304e6.png)
class SingleNode(object):
def __init__(self, elem):
self.elem = elem
self.next = None
self.prev = None
class SingleLinkList(object):
def __init__(self, node=None):
self.__head = node
def is_empty(self):
return self.__head is None
def length(self):
cur = self.__head
count = 0
while cur is not None:
count += 1
cur = cur.next
return count
def travel(self):
cur = self.__head
while cur is not None:
print(cur.elem, end=" ")
cur = cur.next
print()
def add(self, item):
node = SingleNode(item)
node.next = self.__head
self.__head.prev = node
self.__head = node
def append(self, item):
node = SingleNode(item)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur is not None:
cur = cur.next
cur.next = node
node.prev = cur
def insert(self, item, pos):
if pos <= 0:
self.add(item)
elif pos > (self.length() - 1):
self.append(item)
else:
node = SingleNode(item)
cur = self.__head
count = 0
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
node.prev = cur
cur.next.prev = node
cur.next = node
def remove(self, item):
if self.is_empty():
return
else:
cur = self.__head
while cur is not None:
if cur.elem == item:
if cur == self.__head:
self.__head = cur.next
self.__head.prev = None
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
cur = cur.next
def search(self, item):
cur = self.__head
while cur is not None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
if __name__ == "__main__":
ll = SingleLinkList()
print(ll.is_empty())
print(ll.length())
ll.append(1)
ll.add(3)
print(ll.is_empty())
print(ll.length())
ll.insert(2, 1)
ll.travel()
print(ll.search(2))
print(ll.search(4))
ll.add(4)
ll.remove(2)
ll.travel()
ll.remove(4)
ll.travel()
ll.remove(1)
ll.travel()
单向循环链表
class SingleNode(object):
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
def __init__(self, node=None):
self.__head = node
if node:
self.__head.next = node
def is_empty(self):
return self.__head is None
def length(self):
if self.is_empty():
return 0
cur = self.__head
count = 1
while cur.next is not self.__head:
count += 1
cur = cur.next
return count
def travel(self):
if self.is_empty():
return
cur = self.__head
while cur.next is not self.__head:
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem)
def add(self, item):
node = SingleNode(item)
if self.is_empty():
self.__head = node
self.__head.next = node
return
cur = self.__head
while cur.next is not self.__head:
cur = cur.next
node.next = self.__head
self.__head = node
cur.next = self.__head
def append(self, item):
node = SingleNode(item)
if self.is_empty():
self.__head = node
self.__head.next = node
return
else:
cur = self.__head
while cur is not self.__head:
cur = cur.next
node.next = self.__head
cur.next = node
def insert(self, item, pos):
if pos <= 0:
self.add(item)
elif pos > (self.length() - 1):
self.append(item)
else:
node = SingleNode(item)
cur = self.__head
count = 0
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next is not self.__head:
if cur.elem == item:
if cur == self.__head:
rear = self.__head
while rear.next is not 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.elem == item:
if cur is not self.__head:
pre.next = cur.next
else:
self.__head = None
def search(self, item):
if self.is_empty():
return False
cur = self.__head
while cur.next is not self.__head:
if cur.elem == item:
return True
else:
cur = cur.next
if cur.elem == item:
return True
return False
if __name__ == "__main__":
ll = SingleLinkList()
ll.append(1)
ll.add(3)
ll.insert(2, 1)
ll.travel()
ll.add(4)
ll.travel()
ll.remove(2)
ll.travel()
ll.remove(4)
ll.travel()
ll.remove(1)
ll.travel()