下面的程序模拟约瑟夫问题,但是在删除头结点的时候删不掉是怎么回事?求大神指点
package com.wwy.thirdTest;public class Test3_61 {
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Node head = new Node("1");
Test3_61 t = new Test3_61();
t.create(head,4);
//System.out.println(head.next.next.next.next.next.data);
t.print(head);
t.display(head,1); }

//跳过n-1个节点 
public  void display(Node head,int n)
{
Node start = head;
int j = 0;
while(start.next != start)
{
j = 0;
while(j < n)
{
j++;
start = start.next;
}
remove(head,start);
start = start.next;
print(head);
}
}

//删除节点
public  Node remove(Node head,Node del_previous)
{
//System.out.println(123414);
Node del = del_previous.next;
System.out.print("delete:  " + del.data + "  ");
if(del == head)
{
//System.out.println("435");
//问题在这
head = del.next;
}
else  
{
del_previous.next = del.next;
}
return head;
}

//打印链表
public  void print(Node head)
{
Node p = head;
        System.out.print( "剩下的链表   :  ");
        do
        {
            System.out.print(p.data + " -> ");
            p = p.next;
        } while (p != head);
        System.out.println();
}

//创建n个节点的单向循环链表
public  void create(Node head,int n)
{
Node temp = head;
for(int i = 1;i <= n;i++)
{
if(i == n)
{
temp.next = new Node(" " + (i + 1));
temp = temp.next;
temp.next = head;
return;
}else
{
temp.next = new Node(" " + (i + 1));
temp = temp.next;
}

}

}

//节点类
public static class Node
{
Node next;
String data;

public Node(String data)
{
this.data = data;
}
}}

解决方案 »

  1.   

    public void delete(Node head,int m)
    {
    //当前节点
    Node current = head;
    while(current.next != current)
    {
    //寻找第m个节点
    for (int i = 1; i<m; i++){
    System.out.println(2354);
    current = current.next;
    }
    //输出被删除结点编号,删除他的下一个节点
    System.out.print(current.next.data+"  ");
    //被删结点脱链
    current.next = current.next.next;
    //从被删结点的下一个开始循环.
    current = current.next;
    this.print(current);
    }
    }
      

  2.   

    怎么还顺序结构呢?这种问题明显是链式结构来做:public class Test {
    @Test
    public void testYue(){
    int n=8,m=4,s=1;
    this.yueSeFu(n, s, m);
    }/**  约瑟夫环问题,利用单循环链表即可,注意边界处理
     * @param n     总的人数
     * @param s     一开始从第几个人开始报数
     * @param m     每次数的人数
     * 
     * 思想1、根据n,创建循环链表
     *     2、找到第一次的startNode
     *     3、大的循环执行n-1次,
     *     小的循环根据m的值,找到待删除节点的前驱节点(2到m-1次)
     *     删除节点,并让startNode指向已经删除节点的下一个节点
     *     4、最后别忘了输出startNode
     */
    public void yueSeFu(int n,int s,int m){
    Node top=new Node(1);
    top.setNext(top);
    Node last=top;
    //步骤1
    for(int i=2;i<=n;i++){
    Node p=new Node(i);
    last.setNext(p);
    p.setNext(top);
    last=p;
    }
    Node startNode=top;
    //步骤2
    for(int i=1;i<s;i++){
    startNode=top.getNext();
    }
    //步骤3
    for(int i=1;i<=n-1;i++){
    Node p=startNode;
    for(int k=2;k<=m-1;k++){
    p=p.getNext();
    }
    Node next=p.getNext();
    System.out.println(next);
    p.setNext(next.getNext());
    startNode=p.getNext();
    next=null;
    }
    //步骤4
    System.out.println(startNode);
    }
    }/**
     * 链表的节点
     *
     */
    public class Node{

    /**
     * 数据域
     */
    private int data;/**
     * 下一个
     */
    private Node next;public int getData() {
    return data;
    }
    public void setData(int data) {
    this.data = data;
    }
    public Node getNext() {
    return next;
    }
    public void setNext(Node next) {
    this.next = next;
    }@Override
    public String toString() {
    return "Node [data=" + data + "]";
    }
    public Node(int data) {
    super();
    this.data = data;
    }
    public Node() {
    super();
    }
    }
      

  3.   

    Sorry,一开始没认真看你的代码,你也是用的链式结构。说实话,没看懂你的思路。你能写一下你的伪代码么?