# 本节默认队列使用书中源代码队列# 单链表应该以None结尾,并未给出链表示列# 简述:next为共有节点,可公共访问,first()返回头节点,没有保存尾节点# 7.1deftext1(link):'''yield every node since after the second node from a link '''
node = link # get the firest nodewhile node !=Noneand node.next!=None:
node = node.nextyield node.element # yield emlement# 7.2deftext2(L, M):'''piece two link'''
node = L # get the first nodewhile node !=Noneand node.next!=None:
node = node.next
node.next= M # make the last node's.next is M's firstdeftext3(L, t=0):''' caculate the length of link L'''if L ==None:return t
return text3(L.next, t +1)# 7.4 交换 f,r两节点的位置# 单向链表:# 从头遍历找到f节点的前一个节点F,令F.next=r# 从f开始遍历找到r节点的前一个节点R,令R.next=f# 利用py解释器, r.next,f.next=f.next,r.next 交换两节点位置# 双向链表# f.next,r.next=r.next,f.next# f.prev.next,r.prev.next=r.prev.next,f.prev.next# f.prev,r.prev=r.prev,f.prev# 单向链表更耗时# 7.5deftext5(circle_link):'''sum node num'''
head = circle_link
if head ==None:# empty list return 0return0else:
temp = head.next
node_sum =1while temp != head:# circule to the tail
node_sum +=1
temp = temp.nextreturn node_sum
# 7.6deftext6(p, q):'''search all item of circle link to find q'''
temp = p.nextwhile temp != p:if temp == q:returnTrue
temp = temp.nextreturnFalse# 7.7from TheCode.ch07.linked_queue import LinkedQueue
classLinkedQueue1(LinkedQueue):"""additon class"""defrotate(self, k):if self.is_empty():raise Exception('Emptuy!')for i inrange(k):
temp = self._head
temp._next =None
self._head = self._head._next
self._tail._next = temp
self._tail = temp # exchange to nodeyield temp._element
# 7.8 在链表中,控制两个临时节点A,B,A节点每次一步,B节点每次两步。当B节点结束时返回A节点# 时间复杂度为O(n),两个节点需要遍历链表1.5遍deftext8(L):'''by link hopping to get the mid of the link'''
temp1 = L
temp2 = L
whileTrue:for i inrange(2):# hop 2 node every timesif temp2.next==None:return temp1
else:
temp2 = temp2.next
temp1 = temp1.next# 7.9'''
def text9(L,M):
tail : get L's tail node(last sentry's prev
--the sentry.prev need be None.beacuse it need be release)
head : get M's head node(frist sentry's next
--the sentry.next need be None.beacuse it need be release)
tail.next=head.next
head.prev=tail
L.tail=M's tail
'''# 7.10 add_first和add_last是链表操作的重要方法,如果用L.add_before(L.first(),e)代替,会减慢程序的运行速度# 7.11deftext11(L):'''return max node of the PositionalList l'''
maxitem = L.first()# get max item
temp = L.first()while temp !=None:if maxitem.element()< temp.element():
maxitem = temp
temp = L.after(temp)# deliver next position nodereturn maxitem
# a=PositionalList()# for i in range(10):# a.add_first(i)# m=text11(a)# print(m,m.element())# 7.12from TheCode.ch07.positional_list import PositionalList
classPositionalList1(PositionalList):'''herit the PositionalList class for another method'''defmax(self):'''return max node of the l'''
maxitem = self._header._next # get max item
temp = self._header._next
while temp._element !=None:if maxitem._element < temp._element:
maxitem = temp
temp = temp._next # deliver next position nodereturn self._make_position(maxitem)# 7.13deffind(self, e):'''need a parameter which item from list
7.13 find e in PostionalList,return length betwend e and tail'''
index =0
temp = self._header._next # temp node for finding next nodewhile temp._element !=None:if temp._element == e:return index # if find node equal e ,return index
temp = temp._next
index +=1returnNone# 7.14defrec_find(self, e, node=None, index=0):# 类中方法的参数不可使用self'''index is default'''if node ==None:# 参数不能调用self空间,所以设置默认节点
node = self._header._next
if node._element ==None:returnNoneif node._element == e:return index
return self.rec_find(e, node._next, index +1)def__reversed__(self):'''return a reversed list'''
temp = self._header._next # the first node
lis =[]while temp._element !=None:
lis.append(self._make_position(temp))
temp = temp._next # next nodereturn lis
#a = PositionalList1()# for i in range(10):# a.add_first(i)#m = a.max()#print('7.12:', m.element())# print('7.13:',a.find(3),a.find(10))# print('7.14:',a.rec_find(3),a.rec_find(10))# print('7.15:',len(reversed(a)),reversed(a))# 7.13 在7.12代码中 find方法中体现# 7.14 在7.12代码中 rec_find方法中体现 使用尾递归,没什么额外空间的占用# 7.15 在7.12代码中 __reversed__方法实现# 7.16 add_last(e):add_after(last(),e)就可以实现# add_before(p,e):add_after(prev(p),e)# 7.17from TheCode.ch07.favorites_list_mtf import FavoritesListMTF
classFavoritesListMTF1(FavoritesListMTF):'''FavoritesListMTF class additon'''defmove_to_front(self, P):'''这个方法需要早access中使用'''
P._node._prev._next = P._node._next # P is a Position class of PositionalList# P._node is a _Node class of _DoublyLinkedBase
P._node._next._prev = P._node._prev
P._node._prev = self._header # P._node._value is a num!!!!
P._node._next = self._header._next
# 7.18 顺序为{e,d,b,f,c,a},从访问顺序尾部向前排序即可# 7.19 maxnum:0-k%size minnum:k-size# 7.20 从第二个元素开始以MTF方式访问L中的元素,当访问到最后一个元素时L被逆置# 7.21 access本地节点时,需要遍历寻找,所以每次访问第一个节点(是反复访问同一个节点)即可# 7.22from TheCode.ch07.favorites_list import FavoritesList
classFavoritesList1(FavoritesList):defclear(self):'''clear all of item'''
temp = PositionalList()# creat a new data to dele the older data
self._data = temp
defreset_counts(self, num=0):'''reset counts of FavoritesList1'''
temp = self._data.first()while temp !=None:
temp.element()._count = num
temp = self._data.after(temp)
a = FavoritesList1()#7.22测试# for i in range(10):# a.access(i)# print(len(a))# a.clear()# print(len(a))#7.23测试# for i in range(3):# for j in range(10):# a.access(j)# a.reset_counts()# for i in range(10, 11):# a.access(i)# for i in a.top(11):# print(i)# 7.23 在7.22代码中 reset_counts()方法体现#7.24classLinkStack(object):'''sigle link stack ADT'''class_Node:
__slots__ ='_value','_next'def__init__(self,e):
self._value=e
self._next=Nonedef__init__(self):
self._head=self._Node(None)#head is pop stack sign
self._size=0def__len__(self):return self._size
defpush(self,e):'''input a item to the stack'''
temp=self._Node(e)
temp._next=self._head
self._head=temp
self._size+=1defpop(self):'''pop a item fron the stack'''if self._size==0:raise Exception('empty stack')
temp=self._head #get the head node
self._head=self._head._next
temp._next=None
self._size-=1return temp._value
# a=LinkStack()# for i in range(10):# a.push(i)# for i in range(10):# print(a.pop())#7.25classLinkQueue(object):'''a sigle link queue'''class_Node:
__slots__='_next','_value'def__init__(self,e):
self._value=e
self._next=Nonedef__init__(self):
self._head=self._Node(None)#head is a out sign
self._tail=self._head #tail is a input sign
self._size=0defenqueue(self,e):
self._tail._next=self._Node(e)
self._tail=self._tail