怎么把一个单链表倒序输出 就是12345变成54321不能重建一个链表或者堆栈什么的求代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这种东西很无聊诶不知道写这个有什么用,给你写一个吧。Java的package link;/** * 节点对象不用我写注释了吧 * @author * 创建时间:2011-4-1 上午10:15:18 * 类说明: */public class Node { private int value = -1; private Node next = null; public Node(int value){ this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } @Override public String toString() { return "Node [value=" + value + "]"; } }package link;/** * 链表类,注释我都加了 * @author * 创建时间:2011-4-1 上午10:16:46 * 类说明: */public class NodeList { private Node head = null; public Node getHead(){ return this.head; } /** * 对外公布的添加节点的方法 * @param node */ public void addNode(Node node){ if(this.head == null){ /** * 如果头节点是空,就把要加的节点给头节点 */ this.head = node; }else{ /** * 否则把这个节点加到头节点后面的节点上 */ this.addSubNode(this.head, node); } } /** * 反向输出节点元素 */ public void reversePrint(){ int size = this.getSize(0, this.head); for(int index = size; index > 0; index--){ System.out.println(this.getNode(index)); } } /** * 给一个节点加子节点的方法 * @param destNode * @param node */ private void addSubNode(Node destNode, Node node){ if(destNode.getNext() == null){ /** * 如果这个节点没有子节点则把新节点设置为这个节点的子节点 */ destNode.setNext(node); }else{ /** * 否则把新节点加到这个节点的子节点的后面 */ this.addSubNode(destNode.getNext(), node); } } /** * 得到下标对应的节点对象 * @param index * @return */ private Node getNode(int index){ int start = 1; Node currentNode = this.head; while(start < index){ currentNode = currentNode.getNext(); start++; } return currentNode; } /** * 计算链表里节点个数的方法 * @param size * @param currentNode * @return */ private int getSize(int size, Node currentNode){ if(currentNode.getNext() == null){ return ++size; }else{ return getSize(++size, currentNode.getNext()); } }}package link;/** * 测试类,自己看吧。 * @author * 创建时间:2011-4-1 上午10:15:59 * 类说明: */public class NodeListConstructor { public static void main(String[] args){ NodeList list = new NodeList(); list.addNode(new Node(3)); list.addNode(new Node(1)); list.addNode(new Node(2)); list.addNode(new Node(4)); list.addNode(new Node(5)); list.addNode(new Node(6)); list.reversePrint(); }}不借助其他存储区域的话,你做什么程序都做不了的。最少你做for循环要申请循环变量吧? public void turnList() throws Exception { if(head==null) throw new Exception("null list"); else if(head==tail) ; else { Node new_head=head; while(head.next.next!=null) { Node temp=head.next; head.next=temp.next; temp.next=new_head; new_head=temp; } tail.next=new_head; Node temp=tail; tail=head; tail.next=null; head=temp; } } Node *Reverse(Node *&head){ Node *p1,*p2,*p3; p1=head; if (NULL==head||NULL==head->next) { return head; } p2=p1->next; while (p2) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } head->next=NULL; head=p1; return head;} typedef int datatype;typedef struct node{datatype data;struct node *next;}linkqueue,*link;void Ln1_1n(link S){link p,q;p=S->next;S->next=NULL;while(p){q=p;q->next=S->next;S->next=q;p=p->next;}} javase 写小项目问题 如何理解枚举成员生成的class文件 初学者求教,为什莫说找不到类名 关于java日期的小结 JAVA通过II取ORACLE数据库表中的图片BLOB 急问:如何下载www.188.com上面的网页代码 请教 不包含字串的正则写法 如何控制下拉框输入长度和输入字符 我是菜鸟,请大虾指教关于quoted-printable的问题!!! 请问怎样使我的主frame位于其他窗口之上! class [B 类型 再问个问题!关于ping的!!球代码!
package link;
/**
* 节点对象不用我写注释了吧
* @author
* 创建时间:2011-4-1 上午10:15:18
* 类说明:
*/
public class Node { private int value = -1;
private Node next = null;
public Node(int value){
this.value = value;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public Node getNext() {
return next;
} public void setNext(Node next) {
this.next = next;
} @Override
public String toString() {
return "Node [value=" + value + "]";
}
}
package link;
/**
* 链表类,注释我都加了
* @author
* 创建时间:2011-4-1 上午10:16:46
* 类说明:
*/
public class NodeList { private Node head = null;
public Node getHead(){
return this.head;
}
/**
* 对外公布的添加节点的方法
* @param node
*/
public void addNode(Node node){
if(this.head == null){
/**
* 如果头节点是空,就把要加的节点给头节点
*/
this.head = node;
}else{
/**
* 否则把这个节点加到头节点后面的节点上
*/
this.addSubNode(this.head, node);
}
}
/**
* 反向输出节点元素
*/
public void reversePrint(){
int size = this.getSize(0, this.head);
for(int index = size; index > 0; index--){
System.out.println(this.getNode(index));
}
}
/**
* 给一个节点加子节点的方法
* @param destNode
* @param node
*/
private void addSubNode(Node destNode, Node node){
if(destNode.getNext() == null){
/**
* 如果这个节点没有子节点则把新节点设置为这个节点的子节点
*/
destNode.setNext(node);
}else{
/**
* 否则把新节点加到这个节点的子节点的后面
*/
this.addSubNode(destNode.getNext(), node);
}
}
/**
* 得到下标对应的节点对象
* @param index
* @return
*/
private Node getNode(int index){
int start = 1;
Node currentNode = this.head;
while(start < index){
currentNode = currentNode.getNext();
start++;
}
return currentNode;
}
/**
* 计算链表里节点个数的方法
* @param size
* @param currentNode
* @return
*/
private int getSize(int size, Node currentNode){
if(currentNode.getNext() == null){
return ++size;
}else{
return getSize(++size, currentNode.getNext());
}
}
}package link;
/**
* 测试类,自己看吧。
* @author
* 创建时间:2011-4-1 上午10:15:59
* 类说明:
*/
public class NodeListConstructor {
public static void main(String[] args){
NodeList list = new NodeList();
list.addNode(new Node(3));
list.addNode(new Node(1));
list.addNode(new Node(2));
list.addNode(new Node(4));
list.addNode(new Node(5));
list.addNode(new Node(6));
list.reversePrint();
}
}
不借助其他存储区域的话,你做什么程序都做不了的。最少你做for循环要申请循环变量吧?
{
if(head==null)
throw new Exception("null list");
else
if(head==tail)
;
else
{
Node new_head=head;
while(head.next.next!=null)
{
Node temp=head.next;
head.next=temp.next;
temp.next=new_head;
new_head=temp;
}
tail.next=new_head;
Node temp=tail;
tail=head;
tail.next=null;
head=temp;
}
}
Node *Reverse(Node *&head)
{
Node *p1,*p2,*p3;
p1=head;
if (NULL==head||NULL==head->next)
{
return head;
}
p2=p1->next;
while (p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
typedef struct node
{
datatype data;
struct node *next;
}linkqueue,*link;
void Ln1_1n(link S)
{
link p,q;
p=S->next;
S->next=NULL;
while(p)
{
q=p;
q->next=S->next;
S->next=q;
p=p->next;
}
}