STUNUM

面有萌色,胸有丘壑。心有猛虎,细嗅蔷薇。

嗨,我是王鑫 (@stunum),一名 Python 开发者。


Python web开发,后端以Django框架为主,前端使用Vue.js...

python数据结构之单链

python实现单链的数据结构

class SingleChain(object):
    def __init__(self):
        self._header = None

    def add(self, data):
        newnode = Node(data)
        if self._header == None:
            self._header = newnode
        else:
            node = self._header
            print(node.next)
            while node.next != None:
                node = node.next
            node.next = newnode

    def length(self):
        if self._header == None:
            return 0
        node = self._header
        le = 1
        while node.next != None:
            le += 1
            node = node.next
        return le

    def get(self, index):
        leth = self.length()
        if index > 0 and index <= leth - 1:
            le = 0
            node = self._header
            if index == 0:
                return node.data
            while 1:
                le += 1
                if node.next != None:
                    node = node.next
                    if index == le:
                        return node.data
                else:
                    break
        elif index < 0 and abs(index) <= leth:
            le = 0
            node = self._header
            leth = self.length()
            if index == -leth:
                return node.data
            while 1:
                le += 1
                inx = le - leth
                if node.next != None:
                    node = node.next
                    if index == inx:
                        return node.data
                else:
                    break

    def pop(self):
        if self._header == None:
            print('list is empty!')
            return False

        nnode = self._header
        anode = nnode
        while 1:
            if nnode.next == None and nnode is anode:
                self._header = None
                return nnode.data
            elif nnode.next != None:
                anode = nnode.next
                if anode.next == None:
                    nnode.next = None
                    return anode.data
                nnode = anode.next
        return 0

if __name__=="__main__":
    sc = SingleChain()
    sc.add(1)
    sc.add(3)
    sc.add(5)
    sc.add(7)
    print(sc.length())
    print(sc.get(2))
    print(sc.pop())
    print(sc.length())

>>> 4
>>> 5
>>> 7
>>> 3
最近的文章

Django多数据库配置

Django多数据库配置setting.py文件配置多数据库DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'xxxx', #数据库名字 'USER': 'xxxx', #用户名,数据库的拥有者 'PASSWORD':'xxxx',#登录密码 'HOST':xxxx',#主机地址本地可配置l...…

水滴石穿继续阅读
更早的文章

python数据结构之二叉树

python实现二叉树的数据结构class TreeNode(object): def __init__(self, root): self._root = root @property def rootnode(self): return self._root @property def leftnode(self): if hasattr(self, '_left'): return self...…

水滴石穿继续阅读