Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用linkedlist查找数字的频率_Python_Python 3.x_Linked List - Fatal编程技术网

Python 使用linkedlist查找数字的频率

Python 使用linkedlist查找数字的频率,python,python-3.x,linked-list,Python,Python 3.x,Linked List,使用linkedlist查找数字的频率 运行以下代码时,获取SIGTSTP-时间限制超出错误。谁能帮我一下我哪里弄错了 class Element(object): def __init__(self,value): self.value = value self.next = None class LinkedList(object): def __init__(self, head = None): self.head = h

使用linkedlist查找数字的频率

运行以下代码时,获取SIGTSTP-时间限制超出错误。谁能帮我一下我哪里弄错了

class Element(object):
    def __init__(self,value):
        self.value = value
        self.next = None

class LinkedList(object):
    def __init__(self, head = None):
        self.head = head

    def append(self, new):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new
        else:
            self.head = new

    def traverse(self):
        current = self.head
        while current != None:
            print(current.value)
            current = current.next            

arr = list(map(int, input().split()))

ll = LinkedList()
for i in arr:
    e = Element(i)
    ll.append(e)

ll.traverse()

def frequency(a):
    current = a.head
    while current != None:
        count = 1
        while current.next != None:
            if current.value == current.next.value:
                current+=1
                if current.next.next != None:
                    current.next = current.next.next
                else:
                    current.next = None
        print(str(current.value)+" : " + str(count))
        current = current.next

frequency(ll)        

除了频率之外,一切看起来都很好。您需要保留两个引用,一个指向当前元素,另一个将从当前元素开始遍历列表的其余部分。这能让你继续下去吗


还要注意,您当前的实现将修改底层链表,而您确实可以使用指针进行“跳过”,以防止多次列出同一元素,我建议避免以这种方式修改基础结构。

为什么要在链接列表中添加项目的值?@FarhoodET我正在将元素对象添加到链接列表中