【问题标题】:Java linked list implementationJava链表实现
【发布时间】:2016-01-24 07:25:17
【问题描述】:

我现在正在学习链接列表,我有点困惑。我正在处理一项任务,我们正在创建自己的一些方法的实现,以与链表一起使用。

我观看了很多关于链接列表概念的视频,以及节点如何工作,例如获取数据和设置下一个元素,但我对如何制作列表来测试我正在实施的方法感到困惑。我知道这听起来可能很愚蠢,但我开始对方法中的多个参数感到非常困惑,并且涉及到返回语句,而且我之前从未使用过通用数据类型,所以这些基本想法可能会让我感到困惑。

我希望在创建列表方面得到一些帮助,以便我可以在创建方法时对其进行测试,但我在打印列表时遇到了困难。我知道我主要做的是创建新节点并添加到一些列表中,但老实说,我不知道这个所谓的 LinkedList 在哪里或它叫什么。任何帮助是极大的赞赏!!仅供参考,我对 Java 很陌生,这是我的第二堂课,它是在线的,不是一个很好的课程,请多多关照,哈哈

我明白了这个概念,但是这些被添加到的列表(或链接列表)的名称是什么?

    ListNode node4 = new ListNode("Fourth", null);
    ListNode node3 = new ListNode("Third", node4);
    ListNode node2 = new ListNode("Second", node3);
    ListNode node1 = new ListNode("First", node2);

我不知道如何像上面那样单独打印这些,因为我可以使用上面的名字

ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));

这是我的测试类代码,其中我有问题作为 cmets,如果我在上面没有意义,这可能会帮助您理解我对什么感到困惑。我省略了 ListNode 类,因为它是您的基本 getNext() setNext() Node 类,称为 ListNode ,具有泛型类型......

//Testing program for SinglyLinkedList class
public class LinkedListDriver {

public static void main(String[] args) {
    //List<String> list = new LinkedList<String>();                   //comment out this line to test your code
    SinglyLinkedList<String> list = new SinglyLinkedList<String>();  //remove comment to test your code
    SinglyLinkedList SLL = new SinglyLinkedList();

    ListNode node4 = new ListNode("Fourth", null);
    ListNode node3 = new ListNode("Third", node4);
    ListNode node2 = new ListNode("Second", node3);
    ListNode node1 = new ListNode("First", node2);

//ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));

System.out.println(node2.getData());

//Is this the correct way to add a new node to this method?
    SLL.addLast(new ListNode("Fifth", null));
//I doubt my printList is correct as I do not know what parameter I am       supposed to pass to it.    
    SLL.printList();
}

单链表类

//This class implements a very simple singly-linked list of Objects
public class SinglyLinkedList<E> {
     ListNode<E> first; // first element

public SinglyLinkedList()  {
    first = null;
}

public E getFirst() {
   if (first == null) {
      throw new NoSuchElementException();
   }
   else
      return first.getData();
   }

public void addFirst(E value) {
    first = new ListNode<E>(value, first);
}

// Methods below implemented by you. Note: while writing methods, keep in mind
// that you might be able to call other methods in this class to help you - you
// don't always need to start from scratch(but you'll have to recognize when)
public void addLast(E value) {    
    ListNode<E> temp = first;
    //If list is empty make new node the first node.
    if(temp == null) {
        first = new ListNode <E>(value, null);
        first.setNext(null);
    }
    //Otherwise loop to end of list and add new node.
    else {
        while(temp.getNext() != null) {
            temp = temp.getNext();
        }
    temp.setNext(new ListNode<E>(value, null));
    }
}//end addLast

// throws an exception - you decide when and which one
public E getLast() {
    ListNode<E> temp = first;
    if(temp == null) {
        throw new NullPointerException("There are no elements in this list to get.");
    }
    else {
        while(temp.getNext() != null) {
            temp = temp.getNext();
        }
        return temp.getData();
    }
}

// throws an exception - you decide when and which one
public E removeFirst() {
    if(first == null) {
        throw new NullPointerException("There are no elements in this list to remove.");
    }
    ListNode<E> tempRemove = first;  
    return null;//just so it'll compile
  }

  // throws an exception - you decide when and which one
  public E removeLast() {
     return null; //just so it'll compile
  }

  // return the number of elements in the list
  public int size() {
      return 0;//just so it'll compile
  }

  // return true if o is in this list, otherwise false
  public boolean contains(E obj) {
      return true;//just so it'll compile
  }

 public void printList(java.io.PrintStream out) {
     if(first == null) {
        System.out.println("The list is empty");
     }
     ListNode<E> current = first;
     while(current != null) {
         System.out.println(current.toString());
         current = current.getNext();
     }
  }

  public String toString() {
       String s = "[";
       ListNode<E> current = first;
    //write code to traverse the list, adding each object on its own line
    while(current.getNext() != null) {
        current = current.getNext();
    }

  s += "]";
  return s;
  }

  // OPTIONAL: just for fun...and a challenge
  public void reverse() {
  }
}    

【问题讨论】:

  • 您能否在代码之外发布您的问题?看不懂,很难找到。
  • 我的问题用粗体表示,但我会将这些 cmets 取出并将它们与代码之外的那部分一起添加,以免混淆

标签: java linked-list implementation singly-linked-list


【解决方案1】:

链表是基本的数据结构。

一个基本的链表实现如下:

class LinkedListNode {
  int value;
  LinkedListNode next;
}

基本思想是您可以通过检查列表是否具有next 参数来遍历列表,例如:while (currentNode.next != null) { ... }

打印出链表的值利用了前面提到的while循环,你只需打印当前节点的值。

您可以将链表视为一行人,他们只能看到他们前面的人,而看不到他们后面的人。你知道排在最前面的那个人——也就是那个没有人在他“身后”的人——在哪里。这个人被称为列表的“头”。他知道谁在他的正前方,每个人都知道谁在他的正前方。当你到达没有人在他们面前的地步时,你已经走过了整条线。

以下是在列表末尾添加新元素的示例:

LinkedListNode node = myListHead;
while (node.next != null) {
  node = node.next;
}
node.next = new LinkedListNode(...);

【讨论】:

  • 我完全明白,我明白它是如何工作的我只是对这个任务感到困惑......在我创建的主要节点中,那些“链接列表”是什么也被添加?在编写方法时如何测试它们?我只想有一个基本列表,然后我可以将其传递给我正在编写的方法并在我进行时对其进行测试......我有任何意义吗?大声笑
  • 是的,我明白了,我看了一些视频,其中那个家伙做了一个非常好的工作想法,解释了这个概念以及如何将它们画出来......这是否意味着它们不同于数组或ArrayLists,其中有一个名为 namesArrayList 的 ArrayList,您可以向其中添加项目,LinkedList 是链接在一起但没有“链接列表”整体的实际名称的单个节点?
  • 在 ArrayList 中,您有一个根据需要“增长”的数组,因为您可以向其中添加更多内容。因此,您可以通过索引访问元素。在标准单链表中,您无法访问列表中间或末尾的节点——您只能直接访问根。因此,您必须遍历列表以找到您要查找的值。为了向列表末尾添加新值,您必须迭代到末尾并在末尾添加一个值。我将编辑我的帖子并包含一个示例。
  • 现在我的 printList() 方法只打印出我假设的某种内存地址 (ListNode@7852e922)
  • @sjud9227 你不能只打印出对象。您必须打印出对象的值:System.out.println(node.value);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 2012-10-15
  • 2016-09-27
相关资源
最近更新 更多