拉丁方陣和約瑟夫環 鍊錶 python

2021-10-05 01:53:53 字數 3790 閱讀 1616

# -*- coding=utf-8 -*-

# software: 演算法學習

# datetime:2020/4/9 9:14 上午

class

node

(object):

""" 鍊錶的節點物件:包含資料與和指標域

"""def__init__

(self, data=

none

,next

=none):

self._value = data

self._next =

next

defget_value

(self)

:return self._value

defget_next

(self)

:return self._next

defset_value

(self, new_data)

: self._value = new_data

defset_next

(self, new_next)

: self._next = new_next

class

cyclelinklist

(object):

def__init__

(self)

:# 宣告乙個尾節點

self._tail = node(

) self._tail.set_next(self._tail)

self._length =

0def

head

(self)

:"""

鍊錶的第乙個元素(除去頭節點)

:return:

"""return self._tail.get_next(

)def

tail

(self)

:"""

鍊錶的最後乙個元素

:return:

"""return self._tail

defis_empty

(self)

:"""

判斷鍊錶是否為空

:return:

"""return self._tail.get_next(

)== self._tail

defsize

(self)

:"""

鍊錶的大小

:return:

"""return self._length

defadd(self, value)

:"""

從頭部插入節點

:param value:

:return:

"""new_node = node(

) new_node.set_value(value)

new_node.set_next(self._tail.get_next())

self._tail.set_next(new_node)

self._length +=

1def

(self, value)

:"""

從尾部追加節點

:param value:

:return:

"""new_node = node(

) new_node.set_value(value)

new_node.set_next(self._tail)

head = self._tail.get_next(

) tmp = head

while head != self._tail:

tmp = head

head = head.get_next(

) tmp.set_next(new_node)

self._length +=

1def

cycle

(self, m)

:"""

約瑟夫環

:return:

"""str1 =

"" current = self._tail

tmp = self._tail

while current.get_next(

)!= current:

count = m

for i in

range(1

, count +1)

: tmp = current

current = current.get_next(

)if current == self._tail:

current = current.get_next(

) tmp.set_next(current)

if current.get_next(

)== current:

break

tmp.set_next(current.get_next())

str1 +=

str(current.get_value())

+"-->"

return str1

deflatin_policy

(self, n)

:"""

拉丁方陣

:param n:

:return:

"""current = self._tail

tmp = self._tail

for i in

range

(n):

str1 =

""for j in

range

(self.size())

: tmp = current

current = current.get_next(

) str1 +=

str(current.get_value())

+" "

print

(str1)

tmp = current

current = current.get_next(

)if current == self._tail:

current = current.get_next(

) tmp.set_next(current)

if __name__ ==

'__main__'

: cycle_list = cyclelinklist(

)for i in

range(1

,6):

current = cycle_list.tail(

).get_next(

) tmp = cycle_list.tail(

) l =

""while current.get_next(

)!= tmp:

l +=

str(current.get_value())

+" "

current = current.get_next(

) l +=

str(current.get_value())

print

("初始化遍歷鍊錶的值"

, l)

# 拉丁方陣

cycle_list.latin_policy(5)

# 死亡順序

print

(cycle_list.cycle(3)

)

迴圈鍊錶和約瑟夫環

迴圈鍊錶時另一種形式的鏈式儲存結構,把單鏈表中指向最後乙個結點的指標指向單鏈表的表頭結點,就形成了乙個迴圈鍊錶。迴圈鍊錶無論從鍊錶中的任意出發點出發均可找到表中的其他結點。約瑟夫問題是猶太歷史學家約瑟夫的乙個故事 在羅馬人占領喬塔帕特後,39 個猶太人與josephus及他的朋友躲到乙個洞中,39個...

拉丁方陣 單迴圈鍊錶實現

include includetypedef struct nodenode,linklist void create cyclelist tail linklist l,int number l next l 初始化了鍊錶 for count 1 count number count new da...

利用迴圈鍊錶列印拉丁方陣

拉丁方陣就是乙個n n 2 n 9 的方陣,每個格仔裡存放乙個數字,要求每一橫行每一縱行數字不重複。例如 1 22 1 n要求由使用者輸入 c語言實現 include include typedef struct ldla la newlist int void print la int intma...