Python单链表及函数功能代码实现
Python单链表及函数功能代码实现
#代码实现单向链表by Fan
class Node(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):
print(self._head == None)
#获取链表的长度
def length(self):
cur = self._head
count = 0
while cur != None:
cur = cur.next
count += 1
return count
#遍历链表
def travel(self):
cur = self._head
while cur != None:
print(cur.elem,end="")
cur = cur.next
#头部插入
def add(self,item):
node = Node(item)
node.next = self._head
self._head = node
#尾部插入
def append(self,item):
node = Node(item)
if self._head == None:
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
cur.next = node
#指定位置插入
def insert(self,pos,item):
if pos < 1:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
node = Node(item)
pre = None
cur = self._head
count = 0
while count < pos:
pre = cur
cur = cur.next
count += 1
pre.next = node
node.next = cur
#查找特定元素是否存在
def search(self,item):
cur = self._head
while cur != None:
if cur.elem == item:
print("在")
return True
else:
cur = cur.next
print("不在")
return False
#删除某个特定元素
def shanchu(self,item):
pre = None
cur = self._head
while cur != None:
if cur.elem == item:
#判断该节点是否为头节点
if cur == self._head:
self._head = cur.next
return True
else:
pre.next = cur.next
return True
else:
pre = cur
cur = cur.next
return False
#删除某个位置的元素
def delete(self,pos):
pre = None
cur = self._head
i = 0
j = 0
if cur == None:
print("删除请求不合法")
elif pos == 0:
self._head = cur.next
else:
if pos > self.length() - 1:
print("删除请求不合法")
else:
while i <= pos:
if j < pos:
pre = cur
cur = cur.next
j += 1
else:
pre.next = cur.next
i += 1
新手上路,欢迎指导!