How to Solve Josephus Problem using Linked List in Python
In this tutorial, we will learn how to program "How to Solve the Josephus Problem Using a Linked List in Python." The objective is to solve the Josephus problem. This tutorial will guide you step by step through the process of solving the Josephus problem using a linked list. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.
This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of solving the Josephus problem. So, let's dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class CircularLinkedList:
- def __init__(self):
- self.head = None
- def append(self, data):
- node = Node(data)
- if self.head is None:
- self.head = node
- node.next = node
- else:
- temp = self.head
- while temp.next != self.head:
- temp = temp.next
- temp.next = node
- node.next = self.head
- def remove(self, node):
- if self.head.next == self.head:
- self.head = None
- return
- prev = self.head
- while prev.next != node:
- prev = prev.next
- prev.next = node.next
- if node == self.head:
- self.head = node.next
- def display(self):
- if not self.head:
- return
- temp = self.head
- while True:
- print(temp.data, end=" ")
- temp = temp.next
- if temp == self.head:
- break
- print()
- def josephus(cllist, k):
- execution_order = []
- current = cllist.head
- while cllist.head and cllist.head.next != cllist.head:
- for _ in range(k - 1):
- current = current.next
- execution_order.append(current.data)
- print(f"Executed: {current.data}")
- next_node = current.next
- cllist.remove(current)
- current = next_node
- print("Remaining:", end=" ")
- cllist.display()
- return execution_order, cllist.head.data
- while True:
- print("\n============= Solve Josephus Problem using Linked List =============\n")
- n = int(input("Enter number of people: "))
- k = int(input("Enter k (step count): "))
- index_type = input("Use 0-based indexing? (yes/no): ").strip().lower()
- if index_type == "yes":
- start = 0
- else:
- start = 1
- cllist = CircularLinkedList()
- for i in range(start, start + n):
- cllist.append(i)
- print("\nInitial circle:")
- cllist.display()
- order, survivor = josephus(cllist, k)
- print("\nExecution Order:", order)
- print("Survivor:", survivor)
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == "no":
- print("Exiting program...")
- break
- elif opt != "yes":
- print("Invalid choice. Exiting program...")
- break
This program solves the Josephus problem using a circular linked list. The user inputs the number of people (`n`) and the step count (`k`), and the program creates a circular linked list representing the people standing in a circle. It then iteratively removes every `k`‑th person, displaying each executed person and the remaining circle after every step. The program continues this process until only one person remains, ultimately displaying the full execution order of removed people and the survivor. Users can choose whether to use 0-based or 1-based numbering, and the simulation can be repeated multiple times.
Output:
There you have it we successfully created How to Solve Josephus Problem using Linked List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language