在Java中迭代链表?
我一直在努力地观看YouTube视频,以便在秋季课程开始前了解链接列表,我不确定如何继续浏览以下链接列表。“node”类来自同一作者的一系列视频,但“main”方法是我写的。我是否以一种不合逻辑的方式设计了一个链表,当然假设一个人不希望使用预定义的LinkedList类,因为教授希望我们每个人都编写自己的实现在Java中迭代链表?,java,oop,data-structures,Java,Oop,Data Structures,我一直在努力地观看YouTube视频,以便在秋季课程开始前了解链接列表,我不确定如何继续浏览以下链接列表。“node”类来自同一作者的一系列视频,但“main”方法是我写的。我是否以一种不合逻辑的方式设计了一个链表,当然假设一个人不希望使用预定义的LinkedList类,因为教授希望我们每个人都编写自己的实现 class Node { private String data; private Node next; public Node(String data, Nod
class Node
{
private String data;
private Node next;
public Node(String data, Node next)
{
this.data = data;
this.next = next;
}
public String getData()
{
return data;
}
public Node getNext()
{
return next;
}
public void setData(String d)
{
data = d;
}
public void setNext(Node n)
{
next = n;
}
public static String getThird(Node list)
{
return list.getNext().getNext().getData();
}
public static void insertSecond(Node list, String s)
{
Node temp = new Node(s, list.getNext());
list.setNext(temp);
}
public static int size(Node list)
{
int count = 0;
while (list != null)
{
count++;
list = list.getNext();
}
return count;
}
}
public class LL2
{
public static void main(String[] args)
{
Node n4 = new Node("Tom", null);
Node n3 = new Node("Caitlin", n4);
Node n2 = new Node("Bob", n3);
Node n1 = new Node("Janet", n2);
}
}
谢谢你的帮助
Caitlin正如其他一些评论所述,您的链接列表中存在一些缺陷。但是你有一个很好的开始,掌握了链表的概念,并且看起来很实用。要回答如何循环链接列表的这个特定实现的基本问题,您可以这样做
Node currentNode = n1; // start at your first node
while(currentNode != null) {
// do logic, for now lets print the value of the node
System.out.println(currentNode.getData());
// proceed to get the next node in the chain and continue on our loop
currentNode = currentNode.getNext();
}
正如其他一些评论所述,您的链接列表中存在一些缺陷。但是你有一个很好的开始,掌握了链表的概念,并且看起来很实用。要回答如何循环链接列表的这个特定实现的基本问题,您可以这样做
Node currentNode = n1; // start at your first node
while(currentNode != null) {
// do logic, for now lets print the value of the node
System.out.println(currentNode.getData());
// proceed to get the next node in the chain and continue on our loop
currentNode = currentNode.getNext();
}
也许这会有用:
static void iterate(Node head) {
Node current = head;
while (current != null) {
System.out.println(current.getData());
current = current.getNext();
}
}
// or through recursion
static void iterateRecursive(Node head) {
if (head != null) {
System.out.println(head.getData());
iterateRecursive(head.getNext());
}
}
也许这会有用:
static void iterate(Node head) {
Node current = head;
while (current != null) {
System.out.println(current.getData());
current = current.getNext();
}
}
// or through recursion
static void iterateRecursive(Node head) {
if (head != null) {
System.out.println(head.getData());
iterateRecursive(head.getNext());
}
}
祝你好运
祝你好运 您可以让链表DS类实现“Iterable”接口并重写hasNext、next方法,或者创建一个内部类来为您实现它。请看下面的实现:
public class SinglyLinkedList<T>{
private Node<T> head;
public SinglyLinkedList(){
head = null;
}
public void addFirst(T item){
head = new Node<T>(item, head);
}
public void addLast(T item){
if(head == null){
addFirst(item);
}
else{
Node<T> temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = new Node<T>(item, null);
}
}
private static class Node<T>{
private T data;
private Node<T> next;
public Node(T data, Node<T> next){
this.data = data;
this.next = next;
}
}
private class LinkedListIterator implements Iterator<T>{
private Node<T> nextNode;
public LinkedListIterator(){
nextNode = head;
}
@Override
public boolean hasNext() {
return (nextNode.next != null);
}
@Override
public T next() {
if(!hasNext()) throw new NoSuchElementException();
T result = nextNode.data;
nextNode = nextNode.next;
return result;
}
}
}您可以让您的链表DS类实现“Iterable”接口并重写hasNext、next方法,或者创建一个内部类来为您实现它。请看下面的实现:
public class SinglyLinkedList<T>{
private Node<T> head;
public SinglyLinkedList(){
head = null;
}
public void addFirst(T item){
head = new Node<T>(item, head);
}
public void addLast(T item){
if(head == null){
addFirst(item);
}
else{
Node<T> temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = new Node<T>(item, null);
}
}
private static class Node<T>{
private T data;
private Node<T> next;
public Node(T data, Node<T> next){
this.data = data;
this.next = next;
}
}
private class LinkedListIterator implements Iterator<T>{
private Node<T> nextNode;
public LinkedListIterator(){
nextNode = head;
}
@Override
public boolean hasNext() {
return (nextNode.next != null);
}
@Override
public T next() {
if(!hasNext()) throw new NoSuchElementException();
T result = nextNode.data;
nextNode = nextNode.next;
return result;
}
}
}这里只有节点。您必须实现一个列表,其中包含第一个和最后一个节点以及大小。您至少需要以下类:Node;节点列表。这里只有节点类,列表类在哪里??你把它们都混在一起了,我想你还没有完全理解链表。来看看如何用Node和NodeList实现它。谁说一个链表需要的不仅仅是一个节点?在LISP&Co中,列表只是一个节点:单个节点是一个元素列表,具有值并指向另一个节点的节点是一个两元素列表。。。等等从概念上讲,不需要单独的实体。“这是一个很有用的地方,可以缓存诸如长度之类的信息,并放置一些有用的方法,但这不是链表IMHO的固有概念。”JoachimSauer完全同意。这也是一些关于数据结构的不懂语言的书籍中链表的表示方式。这里只有节点。您必须实现一个列表,其中包含第一个和最后一个节点以及大小。您至少需要以下类:Node;节点列表。这里只有节点类,列表类在哪里??你把它们都混在一起了,我想你还没有完全理解链表。来看看如何用Node和NodeList实现它。谁说一个链表需要的不仅仅是一个节点?在LISP&Co中,列表只是一个节点:单个节点是一个元素列表,具有值并指向另一个节点的节点是一个两元素列表。。。等等从概念上讲,不需要单独的实体。“这是一个很有用的地方,可以缓存诸如长度之类的信息,并放置一些有用的方法,但这不是链表IMHO的固有概念。”JoachimSauer完全同意。这也是链表在一些关于数据结构的语言不可知书籍中的表现方式。