python创建一个有序链表_Python有序链表 - 类的声明

有序链表的类的声明:

节点

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())

def append(self, content): # 在末尾加

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值