Package list;
Import java.util.ArrayList;
/**
* Java Joseph Question: n Individuals (different IDs) in a circle, from Startid (any number) start off m (any number) number, a few m of people row into a new queue, M clear Zero,
* and then starting with the number of m from the next person, count to M to the end of the new queue, so repeat, until everyone is out.
* Print the new queue after the column
*
* EG
* int n = total number of 10;//
* int m = 3; Number of papers
* int startIndex = 1; Start position
* @author Hulk 2014 03 20
*
*/
public class Josephlisttest {
public static void Main (string[] args) {
Long starttime = System.currenttimemillis ();
Josephlisttest test = new Josephlisttest ();
int n = 10; Total number
int m = 3; Number of papers
int startIndex = 12; Start position
System.out.println ("josephlisttest:n=" + N + ", m=" + M +
", startindex=" + StartIndex + "\n\nqueue result:");
arraylist<person> queuelist = Test.queuepreson (n, M, StartIndex);
for (person Person:queuelist) {
System.out.println ("Out person:" + person);
}
System.out.println ("Use time=" +
(System.currenttimemillis ()-starttime));
}
Private arraylist<person> Queuepreson (int n, int m, int startIndex) {
Arraylist<person> queuelist = null;
Personlist list = CreateList (n);
List.search ();
if ((list!= null) && (list.head!= null)) {
Queuelist = new arraylist<josephlisttest.person> ();
Pnode pnode = List.head;
if (StartIndex > 0) {
StartIndex = startIndex% n;
Pnode = List.getnode (StartIndex);
} else {
Pnode = List.head;
}
int count = 1;
while (List.size > 0) {
person Outperson = null;
Find
if (count = = (m-1)) {
Delete Next node
Person prev = Pnode.person;
Outperson = List.remove (prev);
Queuelist.add (Outperson);
System.out.println ("Out of Person:" + Outperson + ", size=" + list.size);
Count = 0;
}
Pnode = Pnode.next;
count++;
}
}
return queuelist;
}
Private personlist createlist (int n) {
Personlist list = new Personlist ();
for (int i = 0; i < n; i++) {
person person = new person (i, "name_" + (i + 1));
List.add (i, person);
}
return list;
}
public class Personlist {
Pnode head = null;
int size = 0;
Public Personlist () {
}
Public personlist (person person) {
Head = new Pnode (person, head);
size++;
}
Public Personlist (Pnode head) {
This.head = head;
Head.setnext (head);
size++;
}
Public Pnode GetHead () {
return head;
}
public void Sethead (Pnode head) {
This.head = head;
}
public int GetSize () {
return size;
}
public void setSize (int size) {
this.size = size;
}
public void size (int size) {
this.size = size;
}
public Boolean IsEmpty () {
return this.size <= 0;
}
public void Inithead (person person) {
if (size = = 0) {
Head = new Pnode (person, head);
} else {
Pnode no = head;
Head = new Pnode (person, no);
}
size++;
}
public void Add (int index, person person) {
if (size = = 0) {
Head = new Pnode (person, head);
Head.setnext (head);
System.out.println ("head:" + head);
} else {
if (Index < 0) {
index = 0;
}
if (Index > size) {
index = size;
}
Pnode no = head;
for (int i = 0; i < (index-1); i++) {
no = No.next;
}
Pnode NewNode = new Pnode (person, no.next);
No.next = NewNode;
}
size++;
}
Public person Delete (int index) {
Pnode Pnode = Remove (index);
if ((Pnode!= null) && (pnode.next!= null)) {
return PNode.next.person;
}
return null;
}
Public pnode Remove (int index) {
if (size = = 0) {
return null;
} else {
if ((Index < 0) | | (index >= size)) {
return null;
}
}
Pnode no = head;
for (int i = 0; i < (index-1); i++) {
no = No.next;
}
No.next = No.next.next;
size--;
if ((no!= null) && (no.next!= null)) {
return no.next;
}
return null;
}
/**
* Remove next node of person node, and return the deleted person
* @param Preperson
* @return Removed person
*/
Public person Remove (person Preperson) {
if (Preperson = = null) {
return null;
}
if (size = = 0) {
return null;
}
Pnode Prenode = head;
int index =-1;
for (int i = 0; i < size; i++) {
if (preNode.person.id = = preperson.id) {
index = i;
Break
} else {
Prenode = Prenode.next;
}
}
person Remperson = null;
if (size <= 1) {
Only one node, get-its and set it as null
Remperson = Prenode.person;
Prenode = null;
} else {
PreNode.next.person is dest one
Remperson = PreNode.next.person;
Prenode.next = PreNode.next.next;
}
size--;
System.out.println ("deleteing index=" + Index + ":" + Remperson + ", size=" + size);
return Remperson;
}
public int update (person src, person dest) {
if (src = null) {
return-1;
}
int index =-1;
Pnode no = head;
for (int i = 0; i < size; i++) {
if (src.id = = no.person.id) {
No.person = dest;
Break
} else {
no = No.next;
}
}
return index;
}
public boolean set (int index, person person) {
if (person = = null) {
return false;
}
if (size = = 0) {
return false;
} else {
if ((Index < 0) | | (index >= size)) {
return false;
}
}
Pnode no = head;
for (int i = 0; i < index; i++) {
no = No.next;
}
No.person = person;
return true;
}
Public person get (int index) {
Pnode no = getnode (index);
if (no!= null) {
return No.person;
}
return null;
}
Public pnode getnode (int index) {
if (size = = 0) {
return null;
} else {
if ((Index < 0) | | (index >= size)) {
return null;
}
}
Pnode no = head;
for (int i = 0; i < index; i++) {
no = No.next;
}
return no;
}
public void Search () {
int sizelong = size;
Pnode no = head;
for (int i = 0; i < Sizelong; i++) {
System.out.println ("Search index=" + i + "," + No);
no = No.next;
}
}
}
public class Pnode {
Person of person;
Pnode next = null;
Public Pnode () {
}
Public Pnode (person person) {
This.person = person;
}
Public Pnode (person, Pnode next) {
This.person = person;
This.next = Next;
}
@Override
Public String toString () {
Return "Pnode [person=" + Person.id + ", next=" + Next.person.id +
"]";
}
Public Person Getperson () {
return person;
}
public void Setperson (person person) {
This.person = person;
}
Public Pnode GetNext () {
return to Next;
}
public void Setnext (Pnode next) {
This.next = Next;
}
}
public class Person {
int id = 0;
String name = "";
Public person () {
}
public person (int ID, String name) {
Super ();
This.id = ID;
THIS.name = name;
}
@Override
Public String toString () {
return "person [id=" + ID + ", name=" + name + "]";
}
}
}