java循环单链表实现约瑟夫环
importjava.util.*;publicclassMainJose{publicstaticvoidmain(String[]args)throwsExcepti...
import java.util.*;public class MainJose{ public static void main (String[] args)throws Exception { int m,n; Scanner reader=new Scanner(System.in); System.out.print("请输入参与游戏的人数:"); n=reader.nextInt(); System.out.print("请输入出列密码:"); m=reader.nextInt(); LinkList li=new LinkList(); for(int i=0;i<n;i++) { li.createlist(i+1); } System.out.println("输出检查循环链表:"); li.display(); System.out.print("出列顺序:"); li.out(m); }}package shugou;public class Node { private Object data; private Node next; public Node() { this(null,null); } public Node(Object data) { this(data,null); } public Node(Object data,Node next) { this.data=data; this.next=next; } public Object getData() { return data; } public Node getNext() { return next; } public void setData(Object data) { this.data=data; } public void setNext(Node next) { this.next=next; }}package shugou;public class LinkList{ private Node head,tail; int curlen=0,position=-1; //创建链表 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; } public void out(int m)throws Exception { Node p=head; int count=1; while(curlen>0) { if(count==m) { System.out.print(p.getData()+" "); p=p.getNext(); remove(m); count=1; } else { p=p.getNext(); count++; } } } 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(); }}
我的出列顺序对了几个,其他都不对,求解啊大神们 展开
我的出列顺序对了几个,其他都不对,求解啊大神们 展开
1个回答
展开全部
看了你的代码,不是很明白,给你提几个建议吧:
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询