587-王同学

, ,

链表

• 一个节点分为两部分

数据区

链接区：

指向下一个结点的地址

单向链表

#单链表
class node(object):
def __init__(self, ele):
self.ele = ele
self.next = None

def __init__(self, node = None):
#私有变量双下划线

def is_empty(self):

def length(self):
count = 0
while cur != None:
count += 1
cur = cur.next
return count

def travel(self):
print('travel_result = ')
while cur != None:
print(cur.ele)
cur = cur.next

other_node = node(other)

def append(self, other):
other_node = node(other)
if self.is_empty():
else:
while cur.next != None:
cur = cur.next
cur.next = other_node

def insert(self, pos, other):#pos从0开始
other_node  = node(other)
count = 0
if pos == 0:
elif pos >= self.length() - 1:
self.append(other)
else:
while count != pos - 1:
count += 1
cur = cur.next
other_node.next = cur.next
cur.next = other_node

def remove(self, item):
pre = None
while cur != None:
if cur.ele == item:
return True
else:
pre.next = cur.next
return True
else:
pre = cur
cur = cur.next

def search(self, item):
count = 0
while cur.ele != item:
cur = cur.next
count += 1
return count

if __name__ == '__main__':
print(ll.is_empty())
print(ll.length())
ll.append(1)
ll.append(5)
ll.insert(4, 7)
ll.remove(9)
ll.travel()
print('length = ', ll.length())
# print(ll.search(7))

双向链表

• 结构

#双向链表

class node(object):
def __init__(self, item):
self.ele = item
self.prev = None
self.next = None
def __init__(self, node=None):
# 私有变量双下划线

def is_empty(self):

def length(self):
count = 0
while cur != None:
count += 1
cur = cur.next
return count

def travel(self):
print('travel_result = ')
while cur != None:
print(cur.ele)
cur = cur.next

other_node = node(other)
other_node.next.prev = other_node

def append(self, other):
other_node = node(other)
if self.is_empty():
else:
while cur.next != None:
cur = cur.next
cur.next = other_node
other_node.prev = cur

def insert(self, pos, other):  # pos从0开始
other_node = node(other)
count = 0
if pos == 0:
elif pos >= self.length() - 1:
self.append(other)
else:
while count != pos - 1:
count += 1
cur = cur.next
other_node.next = cur.next
other_node.prev = cur.prev
other_node.prev.next = other_node
cur.prev = other_node

def remove(self, item):
while cur != None:
if cur.ele == item:
if cur.next:#判断链表是否只有一个节点
cur.next.prev = None
return True
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
return True
else:
pre = cur
cur = cur.next
return False

def search(self, item):
count = 0
while cur.ele != item:
cur = cur.next
count += 1
return count
if __name__ == '__main__':
print(ll.is_empty())
print(ll.length())
ll.append(1)
ll.append(5)
ll.insert(4, 7)
ll.remove(9)
ll.travel()
print('length = ', ll.length())
# print(ll.search(7))

单向循环链表

#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author: Caramel
@file: 3.2.py
@time: 2020/02/09
@desc:单向循环链表
"""
class node(object):
def __init__(self, ele):
self.ele = ele
self.next = None

def __init__(self, node = None):
if node:
node.next = node
#私有变量双下划线

def is_empty(self):

def length(self):
if self.is_empty():
count = 0
return count
count = 1
#单项循环链表从一开始
count += 1
cur = cur.next
return count

def travel(self):
if self.is_empty():
return 0
print('travel_result = ')
print(cur.ele)
cur = cur.next
#尾节点元素未被打印
print(cur.ele)

"""链表头部添加元素，头插法"""
other_node = node(item)
if self.is_empty():
other_node.next = other_node
else:
cur = cur.next
# 退出循环，cur指向尾节点
# cur.next = node

def append(self, other):
other_node = node(other)
if self.is_empty():
other_node.next = other_node
else:
cur = cur.next
cur.next = other_node

def insert(self, pos, other):#pos从0开始
other_node  = node(other)
count = 0
if pos == 0:
elif pos >= self.length() - 1:
self.append(other)
else:
while count != pos - 1:
count += 1
cur = cur.next
other_node.next = cur.next
cur.next = other_node

def remove(self, item):
pre = None
if self.is_empty():
return
else:
if cur.ele == item:
while cur_last.next != cur:
cur_last = cur_last.next

else:
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
#cur指向尾节点
if cur.ele == item:
else:
pre.next = cur.next

def search(self, item):
count = 0
while cur.ele != item:
cur = cur.next
count += 1
return -1
return count

if __name__ == '__main__':
print(ll.is_empty())
print(ll.length())
ll.append(1)
ll.append(5)
ll.insert(4, 7)

ll.remove(9)
ll.travel()
print('length = ', ll.length())
# print(ll.search(7))

Vieu3.3主题

Q Q 登 录