设计一个整数链表类,满足队列操作,取10次随机数,范围在100到100000之间,每取到一个随机数与之前一个随机数之差的绝对值>6时,放入链表中,否则,略去
时间: 2023-05-17 09:02:40 浏览: 74
好的,以下是整数链表类的代码:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class IntegerLinkedList:
def __init__(self):
self.head = None
self.tail = None
def enqueue(self, val):
node = ListNode(val)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def dequeue(self):
if self.head is None:
return None
val = self.head.val
self.head = self.head.next
if self.head is None:
self.tail = None
return val
def is_empty(self):
return self.head is None
def print_list(self):
node = self.head
while node is not None:
print(node.val, end=' ')
node = node.next
# 取10次随机数
import random
for i in range(10):
num = random.randint(100, 100000)
if i == 0 or abs(num - prev_num) > 6:
linked_list.enqueue(num)
prev_num = num
# 打印链表
linked_list.print_list()
```
如果您有更多的问题,请继续提问。
阅读全文