# 定义node类
class Node:
"""
属性:
_value 节点值
_next 下一节点
方法:
getValue 获取节点值
getNext 获取下一节点
setValue 设置节点值
setNext 设置下一节点
"""
节点值初始化时如果不传入默认为None
下一节点默认为None
def __init__(self, value_=None):
self._value = value_
self._next = None
获取当前节点值
获取下一节点
设置当前节点值
设置下一节点
def getValue(self):
return self._value
def getNext(self):
return self._next
def setValue(self, new_value):
self._value = new_value
def setNext(self, new_next):
self._next = new_next
# 定义链表类
class LinkedList:
"""
属性:
_head 链表头
_tail 链表尾
_length 链表长度
方法:
length() 返回链表长度
notEmpty() 判断链表是否为空
getAll() 打印当前链表
add() 在链表头增加元素
append() 在链表尾添加元素
insert() 指定位置插入元素
remove_target() 指定节点,删除改元素
remove_one() 指定元素,删除第一个匹配节点
remove() 指定元素,删除所有匹配节点
alert() 修改元素
search() 判断元素是否在链表中
index() 返回指定元素的索引
element() 返回指定索引的元素
"""
头结点初始化为空节点
尾节点在初始化时指向头结点
默认链表长度为1
def __init__(self):
self._head = Node()
self._tail = self._head
self._length = 1
返回链表长度
判断链表是否为空
获取当前链表所有元素
def length(self):
return self._length
def notEmpty(self):
if self._tail == self._head:
return False
else:
return True
def getAll(self):
current = self._head
linkedlist = []
while current:
linkedlist.append(current.getValue())
current = current.getNext()
return linkedlist
首先创建新节点(创建新节点后,链表长度随之加一)
然后需要对链表当前的状态进行判断
- 如果链表为空
- 设置链表头结点的下一节点为新节点
- 新建一节点并将其赋值给尾节点使头结点和尾节点分离
- 将新节点的下一节点设置为尾节点
- 如果链表不为空
- 将新节点的下一节点设置为当前头结点的下一节点
- 将头结点的下一节点设置为新节点
def add(self, value):
newnode = Node(value)
self._length += 1
if not self.notEmpty():
self._head.setNext(newnode)
self._tail = Node()
self._length += 1
newnode.setNext(self._tail)
else:
newnode.setNext(self._head.getNext())
self._head.setNext(newnode)
首先创建新节点(创建新节点后,链表长度随之加一)
然后也需要对链表的状态作判断
- 如果链表为空
- 设置链表头结点的下一节点为新节点
- 新建一节点并将其赋值给尾节点使头结点和尾节点分离
- 将新节点的下一节点设置为尾节点
- 如果链表不为空
- 先遍历链表找到尾节点的前一个节点
- 然后设置新节点的下一节点是尾节点
- 将尾节点前一节点的下一节点设置为新节点
def append(self, value):
newnode = Node(value)
self._length += 1
if not self.notEmpty():
self._head.setNext(newnode)
self._tail = Node()
self._length += 1
newnode.setNext(self._tail)
else:
current = self._head
while current.getNext() != self._tail:
current = current.getNext()
newnode.setNext(self._tail)
current.setNext(newnode)
既然要插入节点,首先就要判断链表当前的长度
- 如果插入位置小于等于一,则执行add方法
- 如果插入位置大于等于当前表长,则执行append方法
- 特殊情况判断完后,设定临时节点存储新插入的节点值(length+1)
- 遍历链表定位到待插入的节点,current表示,count标识
- 将临时节点的下一节点设置为current的下一节点
- 将current的下一节点设置为临时节点
def insert(self, pos, value):
if pos <= 1:
self.add(value)
elif pos >= self.length():
self.append(value)
else:
temp = Node(value)
self._length += 1
current = self._head
count = 0
while count < pos:
current = current.getNext()
count += 1
temp.setNext(current.getNext())
current.setNext(temp)
移除指定节点首先需要判断输入的索引下标是否合法
然后遍历链表定位到索引所对应的节点
执行完移除操作后记得length要减一
def remove_target(self, index):
current = self._head.getNext()
pre = self._head
count = 1
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
while count != index:
pre = current
current = current.getNext()
count += 1
pre.setNext(current.getNext())
self._length -= 1
注:移除节点并不是直接删除数据而是"下一节点"的重指向
即:将待删除节点的上一节点指向待删除节点的下一节点
- 首先定义变量:当前节点current,当前节点的前一节点pre
- 然后遍历链表匹配到待删除的值的位置
- 因为是删除操作,所以使链表长度减一
- 设置pre的下一节点为current的下一节点
注1:此处的元素删除,如果链表中存在多个相等的指定节点,删除第一个
注2:不需要特意进行内存的回收,python中如果一个对象没有了引用会自动销毁
def remove_one(self, value):
current = self._head.getNext()
pre = self._head
while current:
if current.getValue() == value:
pre.setNext(current.getNext())
self._length -= 1
break
else:
pre = current
current = current.getNext()
此处与"只删除第一个节点"不同的是,当找到匹配值并更新上下节点指向后,不立即退出,而是继续循环,直到将链表中所有符合条件的值全部移除
def remove(self, value):
current = self._head.getNext()
pre = self._head
while current:
if current.getValue() == value:
pre.setNext(current.getNext())
self._length -= 1
current = current.getNext()
else:
pre = current
current = current.getNext()
首先判断输入的索引值是否合法
然后从头结点开始遍历链表查找到指定下标的元素后执行赋值操作
def alter(self, index, value):
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
current = self._head
count = 0
while count != index:
current = current.getNext()
count += 1
current.setValue(value)
设置当前节点为头结点,寻找状态为False
然后遍历链表查找匹配值,找到就更新状态变量
最后返回状态变量
def search(self, value):
current = self._head
found = False
while current and not found:
if current.getValue() == value:
found = True
else:
current = current.getNext()
return found
设置当前节点为头结点,索引值为0,寻找状态为False
遍历链表
- 如果找到则更新寻找状态且调出循环返回索引值
- 没找到则继续遍历且索引值加一
注1:因为设定初始索引值为0,所以此链表中的索引和数组一样均是从0开始
注2:该方法只会返回匹配到的第一个符合条件的值的索引
def index(self, value):
current = self._head
count = 0
found = False
while current and not found:
if current.getValue() == value:
found = True
else:
current = current.getNext()
count += 1
if found:
return count
else:
return '{} is not in linkedlist'.format(value)
首先还是判断输入索引是否合法
然后从链表头开始遍历定位到指定索引
返回改节点的节点值
def element(self, index):
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
current = self._head
count = 0
while count != index:
current = current.getNext()
count += 1
element = current.getValue()
return element
# 定义node类
class Node:
"""
属性:
_value 节点值
_next 下一节点
方法:
getValue 获取节点值
getNext 获取下一节点
setValue 设置节点值
setNext 设置下一节点
"""
def __init__(self, value_=None):
self._value = value_
self._next = None
def getValue(self):
return self._value
def getNext(self):
return self._next
def setValue(self, new_value):
self._value = new_value
def setNext(self, new_next):
self._next = new_next
# 定义链表类
class LinkedList:
"""
属性:
_head 链表头
_tail 链表尾
_length 链表长度
方法:
length() 返回链表长度
notEmpty() 判断链表是否为空
getAll() 打印当前链表
add() 在链表头增加元素
append() 在链表尾添加元素
insert() 指定位置插入元素
remove_target() 指定节点,删除改元素
remove_one() 指定元素,删除第一个匹配节点
remove() 指定元素,删除所有匹配节点
alert() 修改元素
search() 判断元素是否在链表中
index() 返回指定元素的索引
element() 返回指定索引的元素
"""
def __init__(self):
self._head = Node()
self._tail = self._head
self._length = 1
def length(self):
return self._length
def notEmpty(self):
if self._tail == self._head:
return False
else:
return True
def getAll(self):
current = self._head
linkedlist = []
while current:
linkedlist.append(current.getValue())
current = current.getNext()
return linkedlist
def add(self, value):
newnode = Node(value)
self._length += 1
if not self.notEmpty():
self._head.setNext(newnode)
self._tail = Node()
self._length += 1
newnode.setNext(self._tail)
else:
newnode.setNext(self._head.getNext())
self._head.setNext(newnode)
def append(self, value):
newnode = Node(value)
self._length += 1
if not self.notEmpty():
self._head.setNext(newnode)
self._tail = Node()
self._length += 1
newnode.setNext(self._tail)
else:
current = self._head
while current.getNext() != self._tail:
current = current.getNext()
newnode.setNext(self._tail)
current.setNext(newnode)
def insert(self, pos, value):
if pos <= 1:
self.add(value)
elif pos >= self.length():
self.append(value)
else:
temp = Node(value)
self._length += 1
current = self._head
count = 0
while count < pos:
current = current.getNext()
count += 1
temp.setNext(current.getNext())
current.setNext(temp)
def remove_target(self, index):
current = self._head.getNext()
pre = self._head
count = 1
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
while count != index:
pre = current
current = current.getNext()
count += 1
pre.setNext(current.getNext())
self._length -= 1
def remove_one(self, value):
current = self._head.getNext()
pre = self._head
while current:
if current.getValue() == value:
pre.setNext(current.getNext())
self._length -= 1
break
else:
pre = current
current = current.getNext()
def remove(self, value):
current = self._head.getNext()
pre = self._head
while current:
if current.getValue() == value:
pre.setNext(current.getNext())
self._length -= 1
current = current.getNext()
else:
pre = current
current = current.getNext()
def alter(self, index, value):
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
current = self._head
count = 0
while count != index:
current = current.getNext()
count += 1
current.setValue(value)
def search(self, value):
current = self._head
found = False
while current and not found:
if current.getValue() == value:
found = True
else:
current = current.getNext()
return found
def index(self, value):
current = self._head
count = 0
found = False
while current and not found:
if current.getValue() == value:
found = True
else:
current = current.getNext()
count += 1
if found:
return count
else:
return '{} is not in linkedlist'.format(value)
def element(self, index):
if index <= 0 or index >= self.length():
return 'Index out of range'
else:
current = self._head
count = 0
while count != index:
current = current.getNext()
count += 1
element = current.getValue()
return element