Python有序鍊錶 類的宣告

2022-06-01 21:06:11 字數 3358 閱讀 5933

class node:  # 這裡不用加括號,具體引數都在init函式裡,這也是和函式區別的一部分,函式的公升級和高階有序集合

def __init__(self, val):

self.data = val

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 orderedlist:  # 只儲存表頭的資訊

def __init__(self):

self.head = none # 注意這裡是大寫

def isempty(self):

return self.head == none

def add(self, item): # 由於是從首向尾遍歷,那麼自然是向首新增元素方便

newnode = node(item)

previous = none

current = self.head

found = false

while current != none and not found:

if current.getdata() > item:

found = true

else:

previous = current

current = current.getnext()

if previous == none:

newnode.setnext(self.head)

self.head = newnode # 這裡是直接賦值

else:

newnode.setnext(current)

previous.setnext(newnode)

def size(self):

count = 0

p = self.head

while p != none:

count = count + 1

p = p.getnext()

return count

def search(self, content):

p = self.head

found = false # 大寫!!有了found這個較為特殊的變數時,**更加易讀清晰

stop = false

while p != none and not found and not stop:

if p.getdata == content:

found = true

else:

if p.getdata > content:

stop = true # 那麼found肯定等於false, 這樣寫**更加易讀

else:

p = p.getnext()

return found

def remove(self, content):

previous = none

current = self.head

found = false

while current != none and not found:

if current.getdata() == content:

found = true

else:

previous = current

current = current.getnext()

if found:

if previous == none:

self.head = self.head.getnext()

else:

previous.setnext(current.getnext())

newnode = node(content)

current = self.head

previous = none

while current != none:

previous = current

current = current.getnext()

if previous != none:

previous.setnext(newnode) # 注意這是函式而不是引數不能用等於,而且修改物件(這裡是node)的引數也只能通過方法

else:

current.head = newnode # 一定要區分上面的的if

def pop(self): # 刪掉鏈尾的元素

current = self.head

if current.getnext == none:

print("error: list is empty!")

else:

previous = none

while current.getnext():

previous = current

current = current.getnext()

previous.setnext(none)

def pop(self, pos): # 刪除某個位置的節點

current = self.head

previous = none

count = 0

if pos == 0:

self.head = self.head.getnext()

elif pos > self.size(): # 呼叫類的函式

print("error!")

else:

while current != none and count < pos:

previous = current

current = current.getnext()

count = count + 1

previous.setnext(current.getnext())

def printlist(self):

p = self.head

while p:

print(p.data)

p = p.getnext()

mylist = orderedlist()

mylist.add(1)

mylist.add(3)

mylist.add(2)

mylist.add(4)

mylist.pop(1)

mylist.printlist()

Python無序鍊錶 類的宣告

class node 這裡不用加括號,具體引數都在init函式裡,這也是和函式區別的一部分,函式的公升級和高階有序集合 def init self,val self.data val self.next none def getdata self return self.data def getne...

鍊錶的有序

time limit 1000ms memory limit 65536kb submit statistic discuss problem description 集合有乙個重要的特性 互異性,即集合中任意兩個元素都是不同的,互異性使得集合中的元素沒有重複。給你 n 個包含重複數字的無序正整數序...

有序鍊錶的歸併

includeusing namespace std typedef struct lnode 定義單鏈表 lnode,linklist void initlist l linklist l 建立單鏈表 void input linklist l,int n 依次往單鏈表l裡輸入資料 void ou...