java中链表的操作 本人初学java有个问题想问一下,希望各位大虾帮下忙,先谢谢了! 在java中不是没有了指针吗,那么java中是如何对链表进行操作的呢??请附上例子讲一下,再次谢谢! 解决方案 » 免费领取超大流量手机卡,每月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;}}}上面是我自己写的链表~其实java自带有LinkedList~ 如二楼所述,LinkedList可以制作队列和栈. 楼主你不研究代码怎么可能明白啊.把代码拿回去自己好好查阅JDK就能明白. 这道android程序为什么出错? DB2中如何实现静态SQL中对查询条件运行动态选择(已实现oracle)?有答案即结贴!! 蝴蝶文形成 帮我解释一下g.drawRect(0, 0, getSize().width - 1,getSize().height - 1);中getSize()为什么就可以直接用? 欢迎参与,编程思想大讨论!! 问牛人们一个问题 jsp显示Excel文件的问题 Java中如何实现对文本文件的插入文本,删除文本等操作 想做点东西!?!? 已过时,什么才是较新的API。 (我的分数不多) 组件用不了,不知道是怎么回事 关于光驱的问题
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;
}
}
}上面是我自己写的链表~其实java自带有LinkedList~
楼主你不研究代码怎么可能明白啊.把代码拿回去自己好好查阅JDK就能明白.