我是编码新手,我正在学习Java中的链接列表。下面的代码显示了两个类。一个类反转名为ReverseASinglyLinkedList
类的单链表,另一个类是LinkedListNode
类。我编写代码来测试我的代码是否真正有用,我遇到了问题。在主要内容中,我想传递以下LinkedList
:[7, 14, 21, 28]
。我希望代码返回[28, 21, 14, 7]
。首先,我如何将[7, 14, 21, 28]
传递给主?我试图编写代码,但它有错误。我觉得我错过了一些简单的东西,但我几乎就在那里。感谢。
public static void main(String[] args) {
LinkedListNode list = new LinkedListNode();
list.head = new LinkedListNode(7);
list.head.next = new LinkedListNode(14);
list.head.next.next = new LinkedListNode(21);
list.head.next.next.next = new LinkedListNode(28);
System.out.println(list.data);
System.out.println("Given Linked list");
list.printList(list.head);
list.head = reverse_iterative(list.head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(list.head);
}
public class LinkedListNode {
public LinkedListNode next, prev, last;
public int data;
public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
data = d;
setNext(n);
setPrevious(p);
}
public LinkedListNode(int d) {
data = d;
}
public LinkedListNode() {
}
public void setNext(LinkedListNode n) {
next = n;
if (this == last) {
last = n;
}
if (n != null && n.prev != this) {
n.setNext(this);
}
}
public void setPrevious(LinkedListNode p) {
prev = p;
if (p != null && p.next != this) {
p.setNext(this);
}
}
public LinkedListNode clone() {
LinkedListNode next2 = null;
if (next != null) {
next2 = next.clone();
}
LinkedListNode head2 = new LinkedListNode(data, next2, null);
return head2;
}
}
public class ReverseASinglyLinkedList {
public static LinkedListNode reverse_iterative(
LinkedListNode head) {
// no need to reverse if head is null
// or there is only 1 node.
if (head == null ||
head.next == null) {
return head;
}
LinkedListNode list_to_do = head.next;
LinkedListNode reversed_list = head;
reversed_list.next = null;
while (list_to_do != null) {
LinkedListNode temp = list_to_do;
list_to_do = list_to_do.next;
temp.next = reversed_list;
reversed_list = temp;
}
return reversed_list;
}
}
答案 0 :(得分:0)
只是一个建议
您可以创建一个包含数据成员Data
,previous
和next
Node
的Node类(通常称为前一个节点和下一个节点的“指针”)。
然后创建一个名为List的类,其中您将数据成员作为Node实例,并具有Insert和reverse等功能,您可以在列表中执行操作。
这是你如何实现它:
class LList{
private class Node{
private int data;
private Node previous;
private Node next;
Node(int data){
this.data = data;
this.next = this.previous = null;
}
}
private Node head;
LList(){
this.head = null;
}
public void insertAtFront(int data){
Node temp = new Node(data);
temp.next = head;
temp.previous = null;
if(this.head!= null)this.head.previous = temp;
this.head = temp;
}
void reverse() {
Node temp = null;
Node current = this.head;
while (current != null) {
temp = current.previous;
current.previous = current.next;
current.next = temp;
current = current.previous;
}
if (temp != null) {
this.head = temp.previous;
}
}
public void printList(Node temp){
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
System.out.println();
}
public Node returnNode(){
return this.head;
}
}
public static void main(String[] args) {
LList var1 = new LList();
int[] a = {10,20,30,40};
for(int k : a)var1.insertAtFront(k); //List will look like 40->30->20->10
var1.printList(var1.returnNode());
var1.reverse();
var1.printList(var1.returnNode());
}
此外,由于您有下一个和之前的“指针”,您的实现看起来像一个双向链表。
这是我写的原始代码:
public class LinkedListSO_1_7_2018 {
static class LList{
private static class Node{
// same thing as that of the above
Node(int data){
//just like before
}
}
private Node head;
LList(){
//same thing
}
public void insertAtFront(int data){
//same thing
}
void reverse() {
//same thing
}
public void printList(Node temp){
//and again
}
public Node returnNode(){
//and again
}
}
public static void main(String[] args) {
LList var1 = new LList();
int[] a = {10,20,30,40};
for(int k : a)var1.insertAtFront(k);
var1.printList(var1.returnNode());
var1.reverse();
var1.printList(var1.returnNode());
}
}