Java建循环链表代码 java怎么做循环

java循环单链表实现约瑟夫环看了你的代码,不是很明白 , 给你提几个建议吧:
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 != nullji - 1) {
p = p.getNext();
j++;
}
if (ji || p == null) {
throw new Exception("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
//tail = s;
//tail.setNext(head);
curlen = curlen + 1;
【Java建循环链表代码 java怎么做循环】 }
public void remove(int i) throws Exception {
Node p = head, q = null;
int j = 0;
i = i - 1;
while (ji) {
q = p;
p = p.getNext();
j++;
}
if (ji || 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 (curlen0) {
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; i2 * curlen; i++) {
System.out.print(node.getData() + " ");
node = node.getNext();
}
System.out.println();
}
}
. java怎么创建链表java中创建链表的例子:
package zx;
class Link{
private Node root;
class Node{
private String name;
private Node Next;
public Node(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void addNode(Node newNode){
if(this.Next==null){
this.Next = newNode;
}else{
this.Next.addNode(newNode);
}
}
public void printNode(){
System.out.print(this.name + "--");
if(this.Next!=null){
this.Next.printNode();
}
}
};
public void add(String name){
Node newNode = new Node(name);
if(this.root==null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
};
public class LinkDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Link link = new Link();
link.add("根节点");
link.add("第一节点");
link.add("第二节点");
link.add("第三节点");
link.add("第四节点");
link.print();
System.out.println("null");
}
}
帮忙写个java代码 , 双向循环链表操作的实现可选中1个或多个下面的关键词,搜索相关资料 。也可直接点“搜索资料”搜索整个问题 。

推荐阅读