'# -*- coding: utf-8 -*-'
class Lnode(object):
# data:節(jié)點保存的數(shù)據(jù)
# next:相當(dāng)于next指針俺叭,指向下一個節(jié)點
def __init__(self, data, pnext=None):
self.data = data
self.next = pnext
def __repr__(self):
# 用來定義node的字符輸出
return str(self.data)
class Linklist(object):
def __init__(self):
self.head = None
self.length = 0
# 在鏈表頭部添加節(jié)點
def prepend(self, value):
if self.head is None:
self.head = Lnode(value, None)
else:
newhead = Lnode(value, None)
newhead.next = self.head
self.head = newhead
self.length += 1
# 在鏈表尾部添加節(jié)點
def append(self, value):
if self.head is None:
self.head = Lnode(value, None)
else:
cursor = self.head
while cursor.next is not None:
cursor = cursor.next
cursor.next = Lnode(value, None)
self.length += 1
# 在鏈表的指定位置插入節(jié)點
def insert(self, index, value):
if self.head is None and index != 1:
return
if index <= 0 or index > self.getlength():
return
elif index == 1:
self.prepend(value)
elif index == self.getlength():
self.append(value)
else:
cursor = self.head
node = Lnode(value, None)
for i in range(1, index - 1):
cursor = cursor.next
node.next = cursor.next
cursor.next = node
# 刪除指定位置的節(jié)點
def delnode(self, index):
if self.head is None:
return
if index <= 0 or index > self.getlength():
return
elif index == 1:
self.head = self.head.next
self.length -= 1
elif index == self.getlength():
cursor = self.head
for i in range(1, self.getlength() - 1):
cursor = cursor.next
cursor.next = None
self.length -= 1
else:
cursor = self.head
for i in range(1, index - 1):
cursor = cursor.next
t = cursor.next
cursor.next = t.next
self.length -= 1
# 刪除值為value的鏈表節(jié)點元素
def delvalue(self, value):
if self.head is None:
return
elif self.head.data == value:
self.head = self.head.next
self.length -= 1
else:
pre = self.head
cursor = pre.next
while cursor is not None:
if cursor.data == value:
pre.next = cursor.next
cursor = cursor.next
self.length -= 1
else:
pre = cursor
cursor = cursor.next
# 按序號查找節(jié)點值
def getindex(self, index):
if self.head is None:
return
elif index <= 0 or index > self.getlength():
return
elif index == 1:
return self.head.data
else:
cursor = self.head
for i in range(1, index):
cursor = cursor.next
return cursor.data
# else:
# counter = 1
# cursor = self.head
# while cursor is not None:
# if index == counter:
# return cursor.data
# else:
# counter += 1
# cursor = cursor.next
# 求鏈表的長度
def getlength(self):
if self.head is None:
return 0
else:
count = 1
cursor = self.head
while cursor.next is not None:
count += 1
cursor = cursor.next
return count
# 單鏈表逆序
def diverse(self):
if self.head is None:
return
elif self.head.next is None:
return self.head
else:
pre = None
cursor = self.head
while cursor is not None:
ne = cursor.next
cursor.next = pre
pre = cursor
cursor = ne
self.head = pre
# 刪除鏈表中重復(fù)元素
def delrepret(self):
if self.head is None:
return
if self.head.next is None:
return self.head
else:
data = self.head.data
# print(data)
count = 1
dic = {data: 1}
pre = self.head
cursor = pre.next
while cursor is not None:
print(cursor.data)
if dic.get(cursor.data) is None:
dic[cursor.data] = dic.setdefault(cursor.data, 1)
pre = cursor
cursor = cursor.next
print(dic)
else:
pre.next = cursor.next
cursor = cursor.next
self.length -= 1
# 判斷鏈表是否為空
def isempty(self):
if self.head is None or self.getlength() == 0:
return True
else:
return False
# 刪除列表
def dellist(self):
if self.head is None or self.getlength() == 0:
return
else:
cursor = self.head
while cursor is not None:
cursor.data = None
cursor = cursor.next
self.head = None
# 打印鏈表元素
def print(self):
if self.head is None:
return
else:
cursor = self.head
while cursor is not None:
print(cursor.data, end=' ')
cursor = cursor.next
print()
if __name__ == "__main__":
linklist = Linklist()
print(linklist.isempty())
for i in range(1, 5):
linklist.append(i)
linklist.prepend(1)
linklist.print()
print("按序號查找")
print(linklist.getindex(4))
print("&&&&&&&&&&&&&&&&")
linklist.insert(3, 5)
linklist.print()
linklist.insert(1, 1)
linklist.print()
linklist.insert(2, 2)
print(linklist.isempty())
linklist.print()
print("刪除重復(fù)元素")
linklist.delrepret()
print("輸出刪除元素后的鏈表")
linklist.print()
# linklist.diverse()
# linklist.print()
linklist.delnode(5)
# linklist.delvalue(5)
# print("&&&&&&&&&&&&")
# linklist.print()
# print("**********")
# linklist.dellist()
# print("@@@@@@@@@@@@")
# length = linklist.getlength()
# print(length)
linklist.print()
鏈表
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門犬金,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人六剥,你說我怎么就攤上這事晚顷。” “怎么了疗疟?”我有些...
- 文/不壞的土叔 我叫張陵该默,是天一觀的道長。 經(jīng)常有香客問我策彤,道長栓袖,這世上最難降的妖魔是什么顿膨? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮叽赊,結(jié)果婚禮上恋沃,老公的妹妹穿的比我還像新娘。我一直安慰自己必指,他們只是感情好囊咏,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著塔橡,像睡著了一般梅割。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上葛家,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼桌吃!你這毒婦竟也來了朱沃?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布缴阎,位于F島的核電站,受9級特大地震影響简软,放射性物質(zhì)發(fā)生泄漏蛮拔。R本人自食惡果不足惜述暂,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望建炫。 院中可真熱鬧畦韭,春花似錦、人聲如沸肛跌。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽衍慎。三九已至转唉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稳捆,已是汗流浹背赠法。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 1.頭指針和頭結(jié)點 頭指針 指向第一個模塊壕鹉。頭結(jié)點 在鏈表的第一個結(jié)點之前附設(shè)一個結(jié)點,這個結(jié)點可以不存儲信息剃幌,也...
- 靜態(tài)鏈表 用數(shù)組描述的鏈表叫做靜態(tài)鏈表脊凰; 數(shù)組的元素由兩部分組成抖棘, data和cur, data存儲數(shù)據(jù)狸涌;cur存...
- 實現(xiàn)方式有二:1切省、常規(guī)方法: (1)遍歷一遍單鏈表,計算單鏈表的長度L (2)計算中間節(jié)點j為L/2,(或者當(dāng)L為...
- // 折半查找 int search(int *a, int n, int key) { int min, m...