十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你的remove方法不对,你的方法每次删掉的是从head开始第m个位置的节点,
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的青秀网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
但约瑟夫环需要的是要删掉每次循环数到m的位置的节点。
remove方法可以去掉,再把out方法改一下就可以了。
public void out(int m) throws Exception {
Node p = head;
Node pre = null;
int count = 1;
while (curlen 0) {
if (count == m) {
System.out.print(p.getData() + " ");
if(pre != null){
pre.setNext(p.getNext());
}
p = p.getNext();
curlen = curlen - 1;
count = 1;
} else {
pre = p;
p = p.getNext();
count++;
}
}
}
看了你的代码,不是很明白,给你提几个建议吧:
1、不需要tail节点
2、remove方法应该对删除节点前面的节点操作,而不是使用数字找
给你我修改的LinkList类,你参考一下:
public class LinkList {
private Node head;
int curlen = 0;
// 创建链表
public void createlist(int code) throws Exception {
insert(curlen, code);
}
public void insert(int i, int code) throws Exception {
Node s = new Node(code);
if (i == 0) {
s.setNext(head);
head = s;
}
Node p = head;
int j = 0;
while (p != null j i - 1) {
p = p.getNext();
j++;
}
if (j i || p == null) {
throw new Exception("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
// tail = s;
// tail.setNext(head);
curlen = curlen + 1;
}
public void remove(int i) throws Exception {
Node p = head, q = null;
int j = 0;
i = i - 1;
while (j i) {
q = p;
p = p.getNext();
j++;
}
if (j i || p == null)
throw new Exception("删除位置不合法");
if (q == null) {
// tail.setNext(p.getNext());
head = head.getNext();
} else
q.setNext(p.getNext());
curlen = curlen - 1;
}
/**
* 按照节点删除
* @param i
* @throws Exception
*/
public void remove(Node p) throws Exception {
if(p.getNext()==p){
p=null;
head=null;
}
else{
Node q = p.getNext();
p.setNext(q.getNext());
}
curlen = curlen - 1;
}
public void out(int m) throws Exception {
Node p = head;
if(m==1){
System.out.print("按照顺序出列");
return;
}
int count = 1;
int n=m-1;
while (curlen 0) {
if (count == n) {
System.out.print(p.getNext().getData() + " ");
remove(p);
count = 1;
} else {
count++;
}
p = p.getNext();
}
}
public void display() {
Node node = head;
for (int i = 0; i 2 * curlen; i++) {
System.out.print(node.getData() + " ");
node = node.getNext();
}
System.out.println();
}
}
LinkList p;; /link,int k;*循环地删除队列结点*,m为出列者喊到的数
{
/
}
p-;n最后被删除的元素是;
for(int i=0;data);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(ilt;k;
}
printf(
r=p;link=list。
void JOSEPHUS(int n;*把当前指针移动到第一个报数的人*/
/i++)
{
r=p,指向p的前驱结点 list为头节点*/link解决问题的核心步骤,list:(程序的基本算法)
⒈建立一个具有n个链结点;
⒉确定第1个报数人的位置;
while(p-,直到链表为空;被删除的元素
这是我写的一个差不多,你看一下吧:
package com.test.list;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LinkedList {
public static void main(String[] args) {
MyList l = new MyList();
MyListNode node = l.createList();
l.printNode(node);
//l.searchNode(node, 4);
//node = l.insertNode(node, 3, "g");
//l.printNode(node);
node = l.deleteNode(node, "d");
l.printNode(node);
}
}
class MyListNode {
public String data;
public MyListNode nextNode;
}
class MyList {
public MyListNode createList() {
MyListNode node = new MyListNode();
MyListNode q ,p;
q = new MyListNode();
q = node;
while (true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("请输入节点数据:");
s = br.readLine();
if (s.equals("0")) {
break;
} else {
p = new MyListNode();
p.data = s;
p.nextNode = null;
q.nextNode = p;
q = p;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
public void printNode(MyListNode node) {
MyListNode p = node.nextNode;
while (p!= null) {
System.out.print(" "+p.data);
p = p.nextNode;
}
}
public void searchNode(MyListNode node, int i){
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji) {
p = p.nextNode;
j++;
}
if( p == null || ji) {
System.out.println("error");
}
System.out.println(" --"+p.data+"--");
}
public MyListNode insertNode(MyListNode node, int i ,String s) {
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji-1) {
p = p.nextNode;
j++;
}
if( p == null || ji-1) {
System.out.println("error");
}
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = p.nextNode;
p.nextNode = n;
return node;
}
public MyListNode deleteNode(MyListNode node ,String s) {
MyListNode p = node;
while(p.nextNode != null !p.nextNode.data.equals(s)) {
p = p.nextNode;
}
p.nextNode = p.nextNode.nextNode;
return node;
}
}
/*逆位序创建
public MyListNode createList() {
MyListNode node = new MyListNode();
node.nextNode = null;
while(true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("请输入节点数据:");
s = br.readLine();
if(s.equals("0")) {
break;
}else {
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = node.nextNode;
node.nextNode = n;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
*/