zoukankan      html  css  js  c++  java
  • Python学习第90天(链表实现、jQuery事件绑定)

    此段为引用:

      数据结构是计算机科学必须掌握的一门学问,之前很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,有很多都是用模拟链表,不过这次,我不是用模拟链表来实现,因为python是动态语言,可以直接把对象赋值给新的变量。

      好了,在说我用python实现前,先简单说说链表吧。在我们存储一大波数据时,我们很多时候是使用数组,但是当我们执行插入操作的时候就是非常麻烦,看下面的例子,有一堆数据1,2,3,5,6,7我们要在3和5之间插入4,如果用数组,我们会怎么做?当然是将5之后的数据往后退一位,然后再插入4,这样非常麻烦,但是如果用链表,我就直接在3和5之间插入4就行,听着就很方便。

      那么链表的结构是怎么样的呢?顾名思义,链表当然像锁链一样,由一节节节点连在一起,组成一条数据链。

    链表的节点的结构如下:

      data为自定义的数据,next为下一个节点的地址。

      链表的结构为,head保存首位节点的地址:

      

      接下来我们来用python实现链表

        python实现链表

        首先,定义节点类Node:

    class Node:
        '''
        data: 节点保存的数据
        _next: 保存下一个节点对象
        '''
        def __init__(self, data, pnext=None):
            self.data = data
            self._next = pnext
    
        def __repr__(self):
            '''        用来定义Node的字符输出,
            print为输出data        '''
            return str(self.data)

      然后,定义链表类:

        链表要包括:

        属性:

        链表头:head

        链表长度:length

    class ChainTable(object):
          def __init__(self):
              self.head = None
              self.length = 0

      方法:

        判断是否为空: isEmpty()

    def isEmpty(self):
             return (self.length == 0)

      增加一个节点(在链表尾添加): append()

    def append(self, dataOrNode):
        item = None
        if isinstance(dataOrNode, Node):
            item = dataOrNode
        else:
            item = Node(dataOrNode)
    
        if not self.head:
            self.head = item
            self.length += 1
    
        else:
            node = self.head
            while node._next:
                node = node._next
            node._next = item
            self.length += 1

      删除一个节点: delete()

    #删除一个节点之后记得要把链表长度减一
    def delete(self, index):
        if self.isEmpty():
            print "this chain table is empty."
            return
    
        if index < 0 or index >= self.length:
            print 'error: out of index'
            return
        #要注意删除第一个节点的情况
        #如果有空的头节点就不用这样
        #但是我不喜欢弄头节点
        if index == 0:
            self.head = self.head._next
            self.length -= 1
            return
    
        #prev为保存前导节点
        #node为保存当前节点
        #当j与index相等时就
        #相当于找到要删除的节点
        j = 0
        node = self.head
        prev = self.head
        while node._next and j < index:
            prev = node
            node = node._next
            j += 1
    
        if j == index:
            prev._next = node._next
            self.length -= 1

      修改一个节点: update()

    def update(self, index, data):
        if self.isEmpty() or index < 0 or index >= self.length:
            print 'error: out of index'
            return
        j = 0
        node = self.head
        while node._next and j < index:
            node = node._next
            j += 1
        if j == index:
            node.data = data

      查找一个节点: getItem()

    def getItem(self, index):
        if self.isEmpty() or index < 0 or index >= self.length:
            print "error: out of index"
            return
        j = 0
        node = self.head
        while node._next and j < index:
            node = node._next
            j += 1
    
        return node.data

      查找一个节点的索引: getIndex()

    def getIndex(self, data):
        j = 0
        if self.isEmpty():
            print "this chain table is empty"
            return
        node = self.head
        while node:
            if node.data == data:
                return j
            node = node._next
            j += 1
    
        if j == self.length:
            print "%s not found" % str(data)
            return

      插入一个节点: insert()

    def insert(self, index, dataOrNode):
        if self.isEmpty():
            print "this chain tabale is empty"
            return
    
        if index < 0 or index >= self.length:
            print "error: out of index"
            return
    
        item = None
        if isinstance(dataOrNode, Node):
            item = dataOrNode
        else:
            item = Node(dataOrNode)
    
        if index == 0:
            item._next = self.head
            self.head = item
            self.length += 1
            return
    
        j = 0
        node = self.head
        prev = self.head
        while node._next and j < index:
            prev = node
            node = node._next
            j += 1
    
        if j == index:
            item._next = node
            prev._next = item
            self.length += 1

      清空链表: clear()

    def clear(self):
        self.head = None
        self.length = 0

      大致就是上面这些,前面部分已经搞清楚的差不多了,但是后面依然还存在没弄明白的,单独看没问题,但是放一起就懵了

      这个是我在力扣刷题的时候看到的,所以就写深入些,最近前端的进度太慢,感觉自己前端应该用的不多,我没必要看的太深入

    下面是今天的jQuery的事件绑定

      页面载入
        ready(fn) //当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。
      $(document).ready(function(){}) -----------> $(function(){})

      事件处理
        $("").on(eve,[selector],[data],fn) // 在选择元素上绑定一个或多个事件的事件处理函数。

        // .on的selector参数是筛选出调用.on方法的dom元素的指定子元素,如:
        // $('ul').on('click', 'li', function(){console.log('click');})就是筛选出ul下的li给其绑定
        // click事件;

      [selector]参数的好处:
        好处在于.on方法为动态添加的元素也能绑上指定事件;如:

        //$('ul li').on('click', function(){console.log('click');})的绑定方式和
        //$('ul li').bind('click', function(){console.log('click');})一样;我通过js给ul添加了一个
        //li:$('ul').append('<li>js new li<li>');这个新加的li是不会被绑上click事件的

        //但是用$('ul').on('click', 'li', function(){console.log('click');}方式绑定,然后动态添加
        //li:$('ul').append('<li>js new li<li>');这个新生成的li被绑上了click事件

      [data]参数的调用:
        function myHandler(event) {
        alert(event.data.foo);
        }
        $("li").on("click", {foo: "bar"}, myHandler)

     

    然后明天会针对上面写出对应的面板拖动和放大镜效果案例。

  • 相关阅读:
    Junit单元测试
    win7的6个网络命令
    WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
    WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp
    woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序
    woj1018(HDU4384)KING KONG 循环群
    woj1016 cherry blossom woj1017 Billiard ball 几何
    woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何
    woj1012 Thingk and Count DP好题
    woj1010 alternate sum 数学 woj1011 Finding Teamates 数学
  • 原文地址:https://www.cnblogs.com/xiaoyaotx/p/12977362.html
Copyright © 2011-2022 走看看