python-链表(无序链表、有序链表)

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=count+1
            current=current.getNext()
        return count
    
    def search(self,item):
        current=self.head
        found=False
        while current!-None and not foune:
            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())

class OrderedList:
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None
    
    def size(self):
        current = self.head
        count=0
        while current != None:
            count=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
        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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 答案: def countSort(lst): max_num = max(lst) count_list = [0] * (max_num + 1) for i in lst: count_list[i] += 1 j = 0 for i in range(max_num + 1): for k in range(count_list[i]): lst[j] = i j += 1 return lst ### 回答2: 链表计数排序是一种基于比较的排序算法,它利用链表的特性实现排序。具体步骤如下: 1. 定义一个链表节点的类,包含value和next两个属性。 ```python class Node: def __init__(self, value): self.value = value self.next = None ``` 2. 定义链表计数排序的函数,接受一个无序链表作为参数,返回排序后的链表。 ```python def linkedListCountingSort(head): if not head or not head.next: return head # 计算链表的最小值和最大值 min_val = head.value max_val = head.value cur = head.next while cur: min_val = min(min_val, cur.value) max_val = max(max_val, cur.value) cur = cur.next # 创建计数数组,并初始化为0 count = [0] * (max_val - min_val + 1) # 遍历链表并统计元素出现的次数 cur = head while cur: count[cur.value - min_val] += 1 cur = cur.next # 根据计数数组构建排序后的新链表 dummy = Node(0) cur = dummy for i in range(len(count)): for j in range(count[i]): cur.next = Node(i + min_val) cur = cur.next return dummy.next ``` 以下为一个使用示例: ```python # 创建无序链表:5 -> 2 -> 3 -> 1 -> 4 head = Node(5) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(1) head.next.next.next.next = Node(4) # 调用链表计数排序函数 sorted_head = linkedListCountingSort(head) # 输出排序后的结果 cur = sorted_head while cur: print(cur.value, end=" ") cur = cur.next ``` 输出结果为:1 2 3 4 5,表示链表已经按照升序进行了排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值