【数据结构与算法python】有序链表的python实现

1、有序链表的介绍
有序表是一种数据项依照其某可比性质(如整数大小、字母表先后)来决定在列表中的位置

2、有序链表的性质
与无序链表相比较,有序链表中,越“小”的数据项越靠近列表的头,越靠“前”

3、有序链表的基本操作
(1)节点Node

class Node:
	# 链表初始化
    def __init__(self,initdata):
        self.data = initdata
        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

(2)无序链表初始化
无序表必须要有对第一个节点的引用信息,设立一个属性head,保存对第一个节点的引用
空表的head为None,随着数据项的加入,无序表的head始终指向链条中的第一个节点

def __init__(self):
	self.head = None

(3)判断无序表是否为空
包含的head只是对首个节点Node的引用,判断空表的isEmpty()很容易实现,实现方式如下所示

def isEmpty(self):
	return self.head == None

(4)添加元素
相比无序表,改变最大的方法是add,因 为add方法必须保证加入的数据项添加在合适的位置,以维护整个链表的有序性,比如在(17, 26, 54, 77, 93)的有序表中,加入数据项31,我们需要沿着链表,找到第一个比31大的数据项54,将31插入到54的前面

def add(self,item):
    current = self.head
    previous = None
    stop = False
    while current != None and not stop:
        if current.getData() > item:
            stop = True
        else:
            previous = current
            current = current.getNext()

    temp = Node(item)
    if previous == None:
        temp.setNext(self.head)
        self.head = temp
    else:
        temp.setNext(current)
        previous.setNext(temp)    

(5)链表大小
从链条头head开始遍历到表尾同时用变量累加经过的节点个数

def length(self):
    current = self.head
    count = 0
    while current != None:
        count = count + 1
        current = current.getNext()

    return count

(6)元素搜索
在无序表的search中,如果需要查找的数据项不存在,则会搜遍整个链表,直到表尾,对于有序表来说,可以利用链表节点有序排列的特性,来为search节省不存在数据项的查找时间,一旦当前节点的数据项大于所要查找的数据项,则说明链表后面已经不可能再有要查找的数据项,可以直接返回False

def search(self,item):
    current = self.head
    found = False
    stop = False
    while current != None and not found and not stop:
        if current.getData() == item:
            found = True
        else:
            if current.getData() > item:
                stop = True
            else:
                current = current.getNext()

    return found

(7)元素删除
首先要找到item,这个过程跟search一 样,但在删除节点时,需要特别的技巧current指向的是当前匹配数据项的节点,而删除需要把前一个节点的next指向current的下一个节点,所以我们在search current的同时,还要维护前一个(previous)节点的引用

def remove(self,item):
    current = self.head
    previous = None
    found = False
    while not found:
        if current.getData() == item:
            found = True
        else:
            previous = current
            current = current.getNext()

    if previous == None:
        self.head = current.getNext()
    else:
        previous.setNext(current.getNext())

4、例子

class Node:
    def __init__(self,initdata):
        self.data = initdata
        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 search(self,item):
        current = self.head
        found = False
        stop = False
        while current != None and not found and not stop:
            if current.getData() == item:
                found = True
            else:
                if current.getData() > item:
                    stop = True
                else:
                    current = current.getNext()

        return found
        
    def add(self,item):
        current = self.head
        previous = None
        stop = False
        while current != None and not stop:
            if current.getData() > item:
                stop = True
            else:
                previous = current
                current = current.getNext()

        temp = Node(item)
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(temp)       
            
    def isEmpty(self):
        return self.head == None

    def length(self):
        current = self.head
        count = 0
        while current != None:
            count = count + 1
            current = current.getNext()

        return count
        
              
mylist = OrderedList()
mylist.add(31)
mylist.add(77)
mylist.add(17)
mylist.add(93)
mylist.add(26)
mylist.add(54)

print(mylist.length())
print(mylist.search(93))
print(mylist.search(100))

你可能感兴趣的:(数据结构与算法,数据结构与算法,有序链表,python)