zoukankan      html  css  js  c++  java
  • python数据结构之链表

    链表(Linked List)

    很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,由于python是动态语言,可以直接把对象赋值给新的变量,于是在python一切皆为对象的原理上实现链表的各项操作。

    在实现链表python类的属性和方法操作之前,先整理一些链表的理论知识。

    一、链表的基本结构

    链表是通过一个个节点(Node)组成的,每个节点都包含了称为数据域(value)和指针域(next)的基本单元,它也是一种递归的数据结构。它能保持数据之间的逻辑顺序,但存储空间不必按照顺序存储。 

    链表的基本元素有:

    • 节点:每个节点有两个部分,左边部分称为值域,用来存放用户数据;右边部分称为指针域,用来存放指向下一个元素的指针。
    • head:head节点永远指向第一个节点
    • tail: tail永远指向最后一个节点
    • None:链表中最后一个节点的指针域为None值

    二、链表的种类以及和动态数组(Array List)的对比

     

    三、单向链表属性与各类操作方法代码

    #先定一个node的类
    class Node():                  #value + next
        def __init__ (self, value = None, next = None):
            self._value = value
            self._next = next
    
        def getValue(self):
            return self._value
    
        def getNext(self):
            return self._next
    
        def setValue(self,new_value):
            self._value = new_value
    
        def setNext(self,new_next):
            self._next = new_next
    
    #实现Linked List及其各类操作方法
    class LinkedList():
        def __init__(self):      #初始化链表为空表
            self._head = Node() 
            self._tail = None
            self._length = 0
    
        #检测是否为空
        def isEmpty(self):
            return self._head == None  
    
        #add在链表前端添加元素:O(1)
        def add(self,value):
            newnode = Node(value,None)    #create一个node(为了插进一个链表)
            newnode.setNext(self._head)   
            self._head = newnode
    
        #append在链表尾部添加元素:O(n)
        def append(self,value):
            newnode = Node(value)
            if self.isEmpty():
                self._head = newnode   #若为空表,将添加的元素设为第一个元素
            else:
                current = self._head
                while current.getNext() != None:
                    current = current.getNext()   #遍历链表
                current.setNext(newnode)   #此时current为链表最后的元素
    
        #search检索元素是否在链表中    
        def search(self,value):
            current=self._head
            foundvalue = False
            while current != None and not foundvalue:
                if current.getValue() == value:
                    foundvalue = True
                else:
                    current=current.getNext()
            return foundvalue
    
        #index索引元素在链表中的位置
        def index(self,value):
            current = self._head
            count = 0
            found = None
            while current != None and not found:
                count += 1
                if current.getValue()==value:
                    found = True
                else:
                    current=current.getNext()
            if found:
                return count
            else:
                raise ValueError ('%s is not in linkedlist'%value)
    
        #remove删除链表中的某项元素
        def remove(self,value):
            current = self._head
            pre = None
            while current!=None:
                if current.getValue() == value:
                    if not pre:
                        self._head = current.getNext()
                    else:
                        pre.setNext(current.getNext())
                    break
                else:
                    pre = current
                    current = current.getNext()
    
        #insert链表中插入元素
        def insert(self,pos,value):
            if pos <= 1:
                self.add(value)
            elif pos > self.size():
                self.append(value)
            else:
                temp = Node(value)
                count = 1
                pre = None
                current = self._head
                while count < pos:
                    count += 1
                    pre = current
                    current = current.getNext()
                pre.setNext(temp)
                temp.setNext(current)

    四、操作链表的原理知识

    1.遍历链表

        链表的基本操作:遍历next节点 

    • 在列表中查找一个元素 
    • 在列表中插入一个元素
    • 从列表中删除一列元素

        不可以用head来遍历列表 

    • 否则会丢失列表的一些节点 
    • 可以使用和head相同类型的索引变量:current

    2.插入

    3.删除

    4.双向链表

    5.循环链表

    学习参考:Python数据结构链表实现

  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/kumata/p/9147077.html
Copyright © 2011-2022 走看看