无序表链表实现、有序表
一、节点
# 节点
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
二、无序表
# 无序表
class UnorderedList:
def __init__(self):
self.head = None
def add(self,items):
# [a,b,c]c先插入表
for i in range(len(items)):
temp = Node(items[len(items)-1-i])
temp.setNext(self.head)
self.head = temp
def size(self):
k = 0
current = self.head
while current != None:
current = current.next
k += 1
def search(self,item):
current = self.head
while current != None:
if current.data == item:
return True
else:
return False
current = current.next
def show_list(self):
current = self.head
result = []
while current != None:
result.append(current.data)
current = current.next
print(result)
def remove(self,item):
current = self.head
while current.data == item:
self.head = current.next
current = self.head
previous = self.head
current = previous.next
while current != None:
if current.data == item:
previous.next = current.next
current = previous.next
else:
previous = current
current = current.next
def isEmpty(self):
return self.head == None
def size(self):
current = self.head
k = 0
while current != None:
current = current.next
k += 1
return k
def append(self,item):
temp = Node(item)
current = self.head
if current.data == None:
current.setNext(temp)
else:
while True:
if current.next == None:
current.setNext(temp)
break
current = current.next
def index(self,item):
current = self.head
k = 0
result = []
if self.size == 0:
return None
else:
while current != None:
if current.data == item:
result.append(k)
current = current.next
k += 1
return result
def insert(self,pos,item):
temp = Node(item)
if pos == 0:
temp.setNext(self.head)
self.head = temp
elif pos >= self.size() + 1:
print(f'表长:{self.size()},pos取值范围0-{self.size()}')
elif pos == self.size:
current = self.head
while True:
if current.next == None:
current.setNext(temp)
break
current = current.next
else:
k = 1
previous = self.head
current = previous.next
while True:
if k == pos:
temp.setNext(current)
previous.setNext(temp)
break
previous = current
current = current.next
k += 1
def pop(self,pos=None):
if pos == None:
pos = self.size() - 1
if self.size() == 0:
print('长度为0')
elif pos >= self.size():
print(f'pos取值范围0-{self.size() - 1}')
elif pos == 0:
self.head = self.head.next
else:
k = 1
previous = self.head
current = previous.next
while True:
if k == pos:
previous.setNext(current.next)
break
previous = previous.next
current = current.next
k += 1
三、有序表
- show_list,isEmpty,size,remove,pop方法同无序表的
# 有序表
class UnorderedList:
def __init__(self):
self.head = None
def show_list(self):
current = self.head
result = []
while current != None:
result.append(current.data)
current = current.next
print(result)
def isEmpty(self):
return self.head == None
def size(self):
current = self.head
k = 0
while current != None:
current = current.next
k += 1
return k
def remove(self,item):
current = self.head
while current.data == item:
self.head = current.next
current = self.head
previous = self.head
current = previous.next
while current != None:
if current.data == item:
previous.next = current.next
current = previous.next
else:
previous = current
current = current.next
def pop(self,pos=None):
if pos == None:
pos = self.size() - 1
if self.size() == 0:
print('长度为0')
elif pos >= self.size():
print(f'pos取值范围0-{self.size() - 1}')
elif pos == 0:
self.head = self.head.next
else:
k = 1
previous = self.head
current = previous.next
while True:
if k == pos:
previous.setNext(current.next)
break
previous = previous.next
current = current.next
k += 1
def add(self,item):
temp = Node(item)
previous = None
current = self.head
while current != None:
if current.data > item:
break
previous = current
current = current.next
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.data == item:
found = True
else:
if current.data > item:
stop = True
else:
current = current.next
return found
def index(self,item):
stop = False
result = []
current = self.head
k = 0
while current != None and not stop:
if current.data == item:
result.append(k)
current = current.next
k += 1
else:
if current.data > item:
stop = True
else:
current = current.next
k += 1
if result == []:
print('不存在')
else:
print(result)
标签:current,head,Python,self,None,next,链表,p26,previous
From: https://blog.csdn.net/m0_65199047/article/details/142849600