python單鏈表的好多種操作
阿新 • • 發佈:2019-01-10
資料結構好久沒有看了,基礎幾乎也沒有,所以開始慢慢摸索學習基本的一些資料結構,去理解然後實現。下面是我學習的一些程式碼,如有錯誤還望指出來,讓我及時改正
class Node(object):
def __init__(self, init_data):
self.data = init_data
self.next = None
class NewList(object):
def __init__(self):
self.head = None
def is_empty(self):
if self.head is None:
print('連結串列為空')
# 在連結串列前新增節點
def add(self, item):
temp = Node(item)
if self.head is None:
self.head = temp
else:
temp.next = self.head
self.head = temp
print(id(self.head),id(temp),id(self.head.data),id(temp.data))
def append(self, item):
temp = Node(item)
origin = self.head
if self.head is None:
self.head = temp
else:
while origin.next is not None:
origin = origin.next
else:
origin.next = temp
def search(self, item) :
origin = self.head
flag = False
while origin is not None:
if origin.data == item:
print('你要找的元素在裡面')
flag = True
break
else:
origin = origin.next
if flag is False:
print('未找到')
def location(self, item):
location = 0
origin = self.head
lista = []
while origin is not None:
if origin.data == item:
lista.append(location)
origin = origin.next
location += 1
print(lista)
def index(self, item):
origin = self.head
count = 0
while origin is not None:
if origin.data == item:
count += 1
break
else:
origin = origin.next
if count != 0:
print(count-1)
else:
print('未找到')
# 刪除所有值為item的節點
def delete(self, item):
if self.head is None:
print('連結串列是空的')
else:
while True:
if self.head.data == item:
self.head = self.head.next
else:
break
previous = self.head
current = previous.next
while current is not None:
if current.data == item:
previous.next = current.next
current = current.next
continue
else:
previous = current
current = current.next
# 仿照列表中的remove方法
def remove(self, item):
flag = False
if self.head is None:
print('連結串列是空的')
else:
if self.head.data == item:
self.head = self.head.next
else:
previous = self.head
current = self.head.next
while flag is False:
if current.data == item:
previous.next = current.next
flag = True
else:
previous = current
current = current.next
if flag is True:
print('已經移除')
else:
print('未找到相應的數字')
# 遍歷單鏈表中的值
def bianli(self):
current = self.head
lista = []
while current is not None:
lista.append(current.data)
current = current.next
print(lista)
# 刪除連結串列制定位置上的節點
def delnode(self, index):
# current 代表當前位置
# previous 代表上一個位置
# 一開始都指向head
current = self.head
previous = self.head
if index == 1:
self.head = self.head.next
else:
# 用來定位指定位置,執行一次current往後挪一個,previous也後移一個
for i in range(index-1):
if current.next is None:
print('超出了範圍')
break
else:
print('成功執行')
previous = current
current = current.next
# 此時current到了指定的位置上
# 未超出範圍,執行刪除操作
else:
previous.next = current.next
# 檢視連結串列的長度:
def len(self):
length = 0
current = self.head
while current is not None:
length += 1
current = current.next
return length
# 有了len()之後,刪除制定節點的另一種方法
def deleNode(self, index):
if self.head is None:
print('連結串列為空')
elif index > self.len():
print('超出範圍')
return
elif index == 1:
self.head = self.head.next
else:
previous = self.head
current = previous.next
count = 2
# 因為刪除的那個節點一定存在所以while true沒問題
while True:
if index == count:
previous.next = current.next
break
else:
previous = current
current = current.next
count += 1
# 指定位置前插入節點
def insert(self, index, value):
if self.head is None:
print('連結串列為空')
if index > self.len():
print('連結串列超出範圍')
elif index == 1:
self.add(value)
elif index == self.len()+1:
self.append(value)
else:
count = 2
previous = self.head
current = self.head.next
while True:
if count == index:
temp = Node(value)
temp.next = current
previous.next = temp
break
else:
previous = current
current = current.next
count += 1
# 刪除連結串列以及內部的所有元素
def clear(self):
self.head = None
# 獲得指定位置節點的值
def getvalue(self, index):
if self.head is None :
return '連結串列為空'
elif index > self.len():
return '連結串列超出長度'
else:
count = 1
current = self.head
while True:
if index != count:
current = current.next
count += 1
else:
return current.data
# 仿pop:獲取連結串列尾部的值,並刪除該尾部的節點
def pop(self):
if self.head is None:
return '當前連結串列為空'
elif self.head.next is None:
temp = self.head.data
self.head = None
return temp
else:
current = self.head
while current.next.next is not None:
current = current.next
temp2 = current.data
current.next = None
return temp2
# 逆向連結串列
def reverse(self):
if self.head is None or self.head.next is None:
return '連結串列就一個,或者沒有 逆向沒意義'
else:
previous = None
current = self.head
while current is not None:
# post 關鍵了!如果我直接修改current.next,沒有了指向下一節點的引用,連結串列直接斷咯~
post = current.next
current.next = previous
previous = current
current = post
# 把最後一個節點的頭給揪出來,不然無從下手啊~
self.head = previous
# 去重
def delrepeat(self):
if self.head is None or self.len() == 1:
return
else:
# 用一個字典來儲存data出現的次數
dic = {}
temp = self.head
# 先遍歷一波,給所有的data 作為鍵 值都為0,作為初始化
while temp is not None:
dic[str(temp.data)] = 0
temp = temp.next
previous = None
current = self.head
while current is not None:
# 如果值出現過了,那麼它的值就是1,刪除
if dic[str(current.data)] == 1:
previous.next = current.next
# 這個很關鍵,刪掉了之後,我的current還要繼續向後延伸,因為我還要繼續檢查後面的
current = current.next
else:
# 第一次出現,打上標籤記為1
dic[str(current.data)] += 1
previous = current
current = current.next
# 刪除連結串列中最小的元素
def del_minimal(self):
current = self.head
min = self.head.data
while current is not None:
if current.data < min:
min = current.data
current = current.next
self.delete(min)