递归反转链表的问题。
我使用了这种方法,但是当我尝试在家运行此方法时,即使函数看起来不错,我也无法打印链表的反面它继续以与之前相同的方式打印链表。
有人可以帮助我了解这里出了什么问题吗?
class link {
int data;
public link nextlink;
link(int d1) {
data = d1;
}
}
class List{
link head;
link revhead;
List(){
head = null;
}
boolean isEmpty(link head) {
return head==null;
}
void insert(int d1) {
link templink = new link(d1);
templink.nextlink = head;
head = templink;
}
void printlist(){
link head1 = head;
while(!isEmpty(head1)) {
System.out.print(head1.data + " ");
head1 = head1.nextlink;
}
System.out.println();
}
void reverse() {
link previous=null,temp=null;
while(isEmpty(head)) {
temp = head.nextlink;
head.nextlink = previous;
previous = head;
head = temp;
}
}
}
public class LinkedList {
public static void main(String[] args) {
List list1 = new List();
list1.insert(10);
list1.insert(20);
list1.insert(30);
list1.insert(40);
list1.insert(50);
list1.printlist();
list1.reverse();
list1.printlist();
}
}
你的代码中有两个问题。一:你检查isEmpty(head),你应该检查!isEmpty(head)。第二:当你修复第一个问题时,当循环终止时,"head"变为空。
修复上述两个问题的正确代码:
void reverse() {
link previous = null, temp = null;
while (!isEmpty(head)) {
temp = head.nextlink;
head.nextlink = previous;
previous = head;
if (temp == null) {
break;
}
head = temp;
}
}
检查您的状况: 而 (是空(头))
您忘了添加"! 你的意思是虽然不是空的...做
考虑递归方法在打印链接时的作用。 它实际上不会做任何事情,直到它拥有所有这些元素(即,它将每个调用放在堆栈上,然后当达到基本情况时,它会从堆栈中弹出元素)。
您有哪些现成的非堆栈结构可用于存储列表的反面,然后让您将其打印出来?
更正的代码,用于反转链表而不递归。
class Link {
int data;
public Link nextLink;
Link(int d1) {
data = d1;
}
}
class List {
Link head;
Link revhead;
List() {
head = null;
}
boolean isEmpty(Link head) {
return head == null;
}
void insert(int d1) {
Link tempLink = new Link(d1);
tempLink.nextLink = head;
head = tempLink;
}
void printlist() {
Link head1 = head;
while (!isEmpty(head1)) {
System.out.print(head1.data + " ");
head1 = head1.nextLink;
}
System.out.println();
}
void reverse() {
Link previous = null, temp = null;
while (!isEmpty(head)) {
temp = head.nextLink;
head.nextLink = previous;
previous = head;
head = temp;
}
head = previous;
}
}
public class Main {
public static void main(String[] args) {
List list1 = new List();
list1.insert(10);
list1.insert(20);
list1.insert(30);
list1.insert(40);
list1.insert(50);
list1.printlist();
list1.reverse();
list1.printlist();
}
}
class Node {
Node next;
int value;
public Node() {
}
public Node(Node next, int value) {
super();
this.next = next;
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class Linkedlist {
private Node head = null;
public Linkedlist(Node head) {
this.head = head;
}
public void iterate() {
Node current = head;
while (current != null) {
System.out.println(current.getValue());
current = current.getNext();
}
}
public void reverse() {
Node current = head;
Node prev = null;
while (current != null) {
Node temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
head = prev;
}
public static void main(String[] args) {
Node n = new Node(null, 10);
Node n1 = new Node(n, 20);
Node n2 = new Node(n1, 30);
Node n3 = new Node(n2, 40);
Node n4 = new Node(n3, 50);
Node n5 = new Node(n4, 60);
Linkedlist linkedlist = new Linkedlist(n5);
linkedlist.iterate();
linkedlist.reverse();
System.out.println("------------------REVERSED---------------------");
linkedlist.iterate();
}
}