# python数据结构与算法：双向链表

双向链表：

######################  P4.13-P4. 双向链表  ###########################

class Node(object):
def __init__(self,item):
self.elem = item
self.next = None
self.prev = None

def __init__(self, node = None):

def is_empty(self):

def travel(self):
while cur.next != None:
print(cur.elem, end=" ")
cur= cur.next
print("")

def length(self):
"""length of list 遍历 借助 Cursor 指针或者游标计数"""
count = 0
while cur != None:# cur.next = None
count+=1
cur = cur.next
return count

node = Node(item)
node.next.prev = node

def append(self, item):
node = Node(item)  # node 实例化
if self.is_empty():
else:
while cur.next != None:
cur = cur.next
cur.next = node
node.prev = cur

def insert(self, pos, item):
""":param pos  从0 开始 """
if pos < 0:
elif pos > (self.length() - 1):
self.append(item)
else:
count =0
while (count < pos):
count += 1
cur = cur.next
node = Node(item)
node.next = cur
node.prev = cur.prev
cur.prev = node
cur.prev.next = node

def remove(self, item):
while cur != None:
if cur.elem == item:
if cur.next:
cur.next.prev = None
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
pre = cur
cur = cur.next

def search(self, item):
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False

if __name__ =="__main__":
print(ll.is_empty())
print(ll.length())
ll.append(1)
print(ll.is_empty())
print(ll.length())

ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.append(6)
ll.insert(-1, 9)
ll.insert(3, 100)
ll.insert(8, 1234)
ll.travel()
### 9 8 1 3 119 1 3 1234
ll.remove(1234)
ll.travel()

06-28 53

12-05 86

08-27 33

06-25 25

09-11 78

06-29 24

07-08 27

#### python算法与数据结构-双向链表

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

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