# 定义链表类
# 链表要包括:
# 属性:
# 链表头:head
# 链表长度:length
class ChainTable(object):
def __init__(self):
# 初始化链表
self.head = None
self.length = 0
def isEmpty(self):
return self.length == 0
# 增加一个节点(在链表尾添加): append()
def append(self, dataOrNode):
item = None
if isinstance(dataOrNode, Node):
item = dataOrNode # 节点类的实例
else:
item = Node(dataOrNode) # 节点类的实例
if not self.head:
self.head = item
self.length += 1
else:
node = self.head
while node._next:
node = node._next
node._next = item
self.length += 1
# 增加一个节点(在链表头添加): add()
def add(self, dataOrNode):
item = None
if isinstance(dataOrNode, Node):
item = dataOrNode # 节点类的实例
else:
item = Node(dataOrNode) # 节点类的实例
if not self.head:
self.head = item
self.length += 1
else:
nextnode = self.head
self.head = item
item._next = nextnode
self.length += 1
# 删除一个节点:delete()
def delete(self, index):
if self.isEmpty():
print("this chain table is empty.")
return
if index < 0 or index >= self.length:
print('error: out of index')
return
# 要注意删除第一个节点的情况
# 如果有空的头节点就不用这样
# 但是我不喜欢弄头节点
if index == 0:
self.head = self.head._next
self.length -= 1
return
# prev为保存前导节点
# node为保存当前节点
# 当j与index相等时就
# 相当于找到要删除的节点
j = 0
node = self.head
prev = self.head
while j < index:
prev = node
node = node._next
j += 1
if j == index:
prev._next = node._next
self.length -= 1
# 修改一个节点:update()
def update(self, index, data):
if self.isEmpty() or index < 0 or index >= self.length:
print('error: out of index')
return
j = 0
node = self.head
while j < index:
node = node._next
j += 1
if j == index:
node.data = data
# 查找一个节点: getItem()
def getItem(self, index):
if self.isEmpty() or index < 0 or index >= self.length:
print('error: out of index')
return
j = 0
node = self.head
while j < index:
node = node._next
j += 1
if j == index:
return node.data
# 查找一个节点的索引: getIndex()
def getIndex(self, data):
if self.isEmpty():
print('error: out of index')
return
j = 0
node = self.head
while node:
if node.data == data:
return j
node = node._next
j += 1
if j == self.length:
print("%s is not found" % str(data))
return
# 插入一个节点: insert()
def insert(self, index, dataOrNode):
if self.isEmpty():
print('this chain table is empty.')
return
if index < 0 or index >= self.length:
print("error: out of index")
return
item = None
if isinstance(dataOrNode, Node):
item = dataOrNode
else:
item = Node(dataOrNode)
if index == 0:
item._next = self.head
self.head = item
self.length += 1
return
j = 0
node = self.head
prev = self.head
while j < index:
prev = node
node = node._next
j += 1
if j == index:
prev._next = item
item._next = node
self.length += 1
# 清空链表
def clear(self):
self.head = None
self.length = 0
# 展示链表
def show(self):
n = 0
if self.isEmpty():
print("this chain table is empty.")
return
node = self.head
while n < self.length:
print("node %d: %s" % (n, node.data))
node = node._next
n += 1
chainTable = ChainTable()
for i in range(10):
chainTable.append(i)
chainTable.show()
print(chainTable.getIndex(5))
print(chainTable.getItem(5))
print(chainTable.update(1, 99))
print(chainTable.delete(4))
print(chainTable.insert(5, 100))
chainTable.show()