zoukankan      html  css  js  c++  java
  • 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()
    
  • 相关阅读:
    解决绘图中闪烁的问题(C#)
    创建XML文件以及XML中的节点和更新Xml文件中的节点的值
    在C#中SendMessage和PostMessage的参数传递
    [置顶]在C#中SendMessage和PostMessage的参数传递
    C#中使用DOS命令关闭当前正在运行的程序并重新启动
    Winform中扩展Panel使之具备双缓存,防止闪屏
    用C#调用Windows API向指定窗口发送
    界面控件
    观察站模式
    利用dataview为datatable排序
  • 原文地址:https://www.cnblogs.com/SKEZhi/p/13345829.html
Copyright © 2011-2022 走看看