zoukankan      html  css  js  c++  java
  • 8.30前端jQuery和数据结构知识

    2018-8-30 16:37:17

    单链表的demo

    从俺弟家回来了!

    发现,还是要努力学习是很重要的!!努力学习新的感兴趣的东西!!

    多读书还是很重要的!!!

    越努力,越幸运!

    #    coding:utf-8        2018-8-30 16:36:30
    
    class Node(object):
        """节点"""
        def __init__(self, elem):
            self.elem = elem
            self.next = None
    
    # node = Node(100)
    
    class SingleLinkList(object):
        """单链表"""
        def __init__(self, node=None):
            self.__head  = node
    
        def is_empty(self):
            """判断链表为空"""
            return self.__head == None
    
        def length(self):
            """链表长度"""
            # cur游标,用来移动遍历节点
            cur = self.__head
            # count记录数量
            count = 0
            while cur !=None : 
                count += 1
                cur = cur.next
            return count
    
    
        def travel(self):
            """遍历整个链表"""
            cur = self.__head
            while cur != None:
                print(cur.elem)
                cur = cur.next
    
        def add(self, item):
            """链表头部添加元素"""
            pass
    
        def append(self, item):
            """链表尾部添加元素"""
            node = Node(item)
            if self.is_empty():
                self.__head = node
            else:
                cur = self.__head
                while cur.next != None:
                    cur = cur.next
                # 添加节点
                cur.next = node
    
        def insert(self, pos, item):
            """指定位置添加元素"""
            pass
    
        def remove(self,item):
            """删除节点"""
            pass
    
        def search(self, item):
            """查找节点是否存在"""
            pass
    
    
    if __name__ == '__main__':
        ll = SingleLinkList()
        print(ll.is_empty())
        print(ll.length())
    
        ll.append(1)
        print(ll.is_empty())
        print(ll.length())
    
    
        ll.append(2)
        ll.append(3)
        ll.append(4)
        ll.append(5)
        ll.append(6)
        ll.travel()
  • 相关阅读:
    又是运行不到main的问题
    stlink问题
    AD7124踩过的坑
    stm32上调试AD5410
    linux读xml文件问题
    stm8问题记录
    430 仿真器 问题
    虚拟机VMware显示“内部错误”的解决方法
    VS2008 如何设置字体大小?
    Hyperledger Indy项目
  • 原文地址:https://www.cnblogs.com/zhen1996/p/9561067.html
Copyright © 2011-2022 走看看