Java链表实现

时间:2015-10-25 00:05:02

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

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

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

我喜欢创建列表的一些帮助,所以我可以在创建它们的时候测试这些方法,但是我很难打印出列表。我得到的是我正在做的主要是创建新节点并添加到一些列表但我老实说不知道这个假设的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) ));

这是我的测试类的代码,我在那里提出问题作为评论,可以帮助你理解我在上面没有意义的时候会感到困惑。我省略了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();
}

SinglyLinkedList类

//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() {
  }
}    

1 个答案:

答案 0 :(得分:0)

链接列表是基本数据结构。

基本链表实现如下:

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(...);