拉丁方阵和约瑟夫环(链表)python

# -*- 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

    def get_value(self):
        return self._value

    def get_next(self):
        return self._next

    def set_value(self, new_data):
        self._value = new_data

    def set_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 = 0

    def head(self):
        """
        链表的第一个元素(除去头节点)
        :return:
        """
        return self._tail.get_next()

    def tail(self):
        """
        链表的最后一个元素
        :return:
        """
        return self._tail

    def is_empty(self):
        """
        判断链表是否为空
        :return:
        """
        return self._tail.get_next() == self._tail

    def size(self):
        """
        链表的大小
        :return:
        """
        return self._length

    def add(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 += 1

    def append(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 += 1

    def 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

    def latin_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):
        cycle_list.append(i)
    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))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辛勤的搬砖者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值