
你的iter=all.iterator()这句后少了iter.next()如果不指向一个元素那你的iter.remove()就不知道要删除那个元素了,所以获取新迭代器后要用next方法指定删除的是哪个,否则会抛出异常的。
for(int i=0i<3i++){if(iter.hasNext())
iter.next()
else{
iter=all.iterator()
iter.next()
}
}
Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零,然后又从下一个人开始数m个数开始,数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止。package list
import java.util.ArrayList
* 打印 出列后的新队列
*
* eg
* int n = 10//总人数
* int m = 3 //报数个数
* int startIndex = 1 //起点位置
* @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//总人数
int m = 3//报数个数
int startIndex = 12//起点位置
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 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 = 0i <ni++) {
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 = 0i <(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 = 0i <(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 = 0i <sizei++) {
if (preNode.person.id == prePerson.id) {
index = i
break
} else {
preNode = preNode.next
}
}
Person remPerson = null
if (size <= 1) {
//only one node, get its person 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 = 0i <sizei++) {
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 = 0i <indexi++) {
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 = 0i <indexi++) {
no = no.next
}
return no
}
public void search() {
int sizeLong = size
PNode no = head
for (int i = 0i <sizeLongi++) {
System.out.println("search index= " + i + ", " + no)
no = no.next
}
}
}
public class PNode {
Person person
PNode next = null
public PNode() {
}
public PNode(Person person) {
this.person = person
}
public PNode(Person 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 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 + "]"
}
}
}
LinkList p/link,int k*循环地删除队列结点*,m为出列者喊到的数{
/
}
p-n最后被删除的元素是
for(int i=0data)link
for(i=0*使链表循环起来*,int m) /m-1,P-;
p=p-;i++)
{
p=(LinkList)malloc(sizeof(LNode))/link,p-ilt
}
r-n为总人数link
p=p-
p=list
p=r-
}
,k为第一个开始报数的人i++)
{
r=p* p为当前结点 r为辅助结点;
⒊不断地从链表中删除链结点
else
r-link=p
free(p),r
p-data),无头结点的循环链表*建立循环链表*:%4dquot;ilt*使p指向头节点*!=p)
{
for(i=0; /n:%4d link=p-data=i
if(list==NULL)
list=p
printf(iltk
}
printf(
r=plink=list。
void JOSEPHUS(int n*把当前指针移动到第一个报数的人*/
/i++)
{
r=p,指向p的前驱结点 list为头节点*/link解决问题的核心步骤,list:(程序的基本算法)
⒈建立一个具有n个链结点;
⒉确定第1个报数人的位置
while(p-,直到链表为空被删除的元素
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)