python 实现有序链表和无序链表

1.无序列表

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 UnorderedList:
    def __init__(self):
        self.head = None
    def isempty(self):
        return self.head ==None
    def add(self,item):
        temp = Node(item)
        temp.setnext(self.head)
        self.head = temp
    def size(self):
        current = self.head
        count = 0
        while current != None:
            count += 1
            current = current.getnext()
        return count
    def search(self,item):
        current = self.head
        found = False
        while current != None and not found:
            # print(current.getdata())
            if current.getdata() == item:
                found = True
            else:
                current = current.getnext()
        return found
    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())

mylist = UnorderedList()
mylist.add(31)
mylist.add(77)
mylist.add(17)
mylist.add(93)
mylist.add(26)
mylist.add(54)
print(mylist.size())
print(mylist.search(17))
mylist.remove(31)
print(mylist.size())
print(mylist.search(31))

2.有序列表

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 isempty(self):
        return self.head ==None
    def size(self):
        count = 0
        current = self.head
        while current != None:
            count += 1
            current = current.getnext()
        return count
    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())
    def search(self,item):
        current = self.head
        found = False
        while not found and current.getdata() < item and current != None:
            if current.getdata() == item :
                found = 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 vision(self):
        current = self.head
        list = []
        while current != None:
            list.append(current.getdata())
            current = current.getnext()
        return list
mylist = OrderedList()
mylist.add(31)
mylist.add(77)
mylist.add(17)
mylist.add(93)
mylist.add(26)
mylist.add(54)
print(mylist.size())
print(mylist.vision())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值