关于循环链表的问题 可否只通过一个链接点引用而建立循环链表?例如:class RecList{ private Link current; public RecList{ }}如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class LinkedList {private Node header;/** * @return the header */public Node getHeader() {return header;}/** * @param header the header to set */public void setHeader(Node header) {this.header = header;}public void printListContext(){if (this.header == null){System.out.println("Null");return;}Node temp = this.header;while(temp != null){System.out.print(temp.getContext() + "-->");temp = temp.getNext();}System.out.print("null");System.out.println();}public void resverce(){if (this.header == null || this.header.getNext() == null){return;}Node current = this.header,tempNext = this.header.getNext();int cnt = 0;while(tempNext != null){cnt ++;Node temp = current;current = tempNext;tempNext = tempNext.getNext();if (cnt == 1){temp.setNext(null);}current.setNext(temp);}this.header = current;}public void buildLinkedList(){Node n4 = new Node("4",null);Node n3 = new Node("3",n4);Node n2 = new Node("2",n3);Node n1 = new Node("1",n2);Node n0 = new Node("0",n1);this.header = n0;}/** * @param args */public static void main(String[] args) {LinkedList ll = new LinkedList();ll.buildLinkedList();ll.printListContext();ll.resverce();ll.printListContext();}class Node {private String context;private Node next;public Node(String context,Node n){this.context = context;this.next = n;}/** * @return the context */public String getContext() {return context;}/** * @return the next */public Node getNext() {return next;}/** * @param next the next to set */public void setNext(Node next) {this.next = next;}}}这是我在别的帖子里给写的LinkedList,你可以参考一下,希望有帮助~ public class Check { static class CycleList{ private int seq=0; private CycleList next=null; private static int currcapacity=0; CycleList(int seq,CycleList next) { this.seq=seq; this.next=next; currcapacity++; } private void DeleteNode(CycleList node,CycleList head) { if(currcapacity>=2){ while(head.next!=node) head=head.next; head.next=node.next; node.next=node.next.next; currcapacity--; } } private boolean IsEmpty(){ return currcapacity<=1; } } static int Josephu(int m,int n) { CycleList head=new CycleList(1,null); CycleList last=head; CycleList next=null; for(int i=2;i<=n;i++) { next=new CycleList(i,null); head.next=next; head=next; } head.next=last; head=last; while (head.IsEmpty()==false) { for(int i=1; i<m;i++) head=head.next; System.out.println(head.seq); head.DeleteNode(head, head); } System.out.println(head.next.seq); return head.next.seq; } public static void main(String[] args) { int b=Josephu(3,5); System.out.println("b="+b); }} 我有一个建议,我在外面上网,没有编写代码,大概是这样的一个意思,因为JAVA没有指针所以在你链表后面加一个对象,这个对象是这个链表的第一个对象,这样最后一个节点指向了首节点,不知道这样是否是可以的.另外在JAVA.UTIL包中(小写的)有LINKLIST类其实就是一个双向的链表,你可以去查看一下 求java类库中文版 请教html Parser的两个应用 Java I/O操作问题 使用synth遇到的问题 连接池得例子得小问题 请教一个jmf的问题,是高手的请进 JAVA中Class类中的getSimpleName()意思 为什么空指针异常不能正常工作 请问如何判断B接收的是类对象还是字符串??? 急用!! 求助,应该不难的问题~~~ 一个关于文件读写的问题 又是==和equals(100分)
private Node header;/**
* @return the header
*/
public Node getHeader() {
return header;
}/**
* @param header the header to set
*/
public void setHeader(Node header) {
this.header = header;
}public void printListContext(){
if (this.header == null){
System.out.println("Null");
return;
}
Node temp = this.header;
while(temp != null){
System.out.print(temp.getContext() + "-->");
temp = temp.getNext();
}
System.out.print("null");
System.out.println();
}public void resverce(){
if (this.header == null || this.header.getNext() == null){
return;
}Node current = this.header,tempNext = this.header.getNext();
int cnt = 0;
while(tempNext != null){
cnt ++;
Node temp = current;
current = tempNext;
tempNext = tempNext.getNext();
if (cnt == 1){
temp.setNext(null);
}
current.setNext(temp);
}
this.header = current;
}public void buildLinkedList(){
Node n4 = new Node("4",null);
Node n3 = new Node("3",n4);
Node n2 = new Node("2",n3);
Node n1 = new Node("1",n2);
Node n0 = new Node("0",n1);this.header = n0;
}/**
* @param args
*/
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.buildLinkedList();ll.printListContext();
ll.resverce();
ll.printListContext();}class Node {
private String context;
private Node next;
public Node(String context,Node n){
this.context = context;
this.next = n;
}
/**
* @return the context
*/
public String getContext() {
return context;
}
/**
* @return the next
*/
public Node getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(Node next) {
this.next = next;
}
}
}这是我在别的帖子里给写的LinkedList,你可以参考一下,希望有帮助~
public class Check {
static class CycleList{
private int seq=0;
private CycleList next=null;
private static int currcapacity=0;
CycleList(int seq,CycleList next)
{
this.seq=seq;
this.next=next;
currcapacity++;
}
private void DeleteNode(CycleList node,CycleList head)
{
if(currcapacity>=2){
while(head.next!=node) head=head.next;
head.next=node.next;
node.next=node.next.next;
currcapacity--;
}
}
private boolean IsEmpty(){
return currcapacity<=1;
}
}
static int Josephu(int m,int n)
{
CycleList head=new CycleList(1,null);
CycleList last=head;
CycleList next=null;
for(int i=2;i<=n;i++)
{
next=new CycleList(i,null);
head.next=next;
head=next;
}
head.next=last;
head=last;
while (head.IsEmpty()==false)
{
for(int i=1; i<m;i++)
head=head.next;
System.out.println(head.seq);
head.DeleteNode(head, head);
}
System.out.println(head.next.seq);
return head.next.seq;
}
public static void main(String[] args) {
int b=Josephu(3,5);
System.out.println("b="+b);
}}
另外在JAVA.UTIL包中(小写的)有LINKLIST类其实就是一个双向的链表,你可以去查看一下