添加到链表前面

发布于 2024-10-15 15:26:14 字数 486 浏览 1 评论 0原文

我对如何添加到链接列表的前面感到困惑。

/**
* data is added to the front of the list
* @modifies this
* @ffects 2-->4-->6 becomes data-->2-->4-->6
*/
public void insert(E data) {
    if (front == null) 
        front = new Node(data, null);
    else {
        Node temp = new Node(data, front);
        front = temp;
    }
}

这就形成了一个循环。我该如何避免这种情况?

我有一个 LinkedList 类,它在一个名为 front 的变量中保存前端节点。 我在这个 LinkedList 类中有一个 Node 类。

任何帮助将不胜感激。 谢谢。

I am confused as to how to add to the front of the linked list.

/**
* data is added to the front of the list
* @modifies this
* @ffects 2-->4-->6 becomes data-->2-->4-->6
*/
public void insert(E data) {
    if (front == null) 
        front = new Node(data, null);
    else {
        Node temp = new Node(data, front);
        front = temp;
    }
}

This creates a cycle. How do I avoid that?

I have a LinkedList class which holds the front Node, in a variable called front.
I have a Node class within this LinkedList class.

Any help would be appreciated.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

呢古 2024-10-22 15:26:14

您无权访问“下一个”节点吗?

在这种情况下

public void insert(E data) {
    if (front == null) { 
        front = new Node(data, null);
    } else {
        Node temp = new Node(data, null);
        temp.next = front;
        front = temp;
    }
}

——

 class LinkedList {
    Node front;

    LinkedList() { 
        front = null; 
    }

    public void AddToFront(String v) {
        if (front == null) {
            front = new Node(v);
        } else {
            Node n = new Node(v);
            n.next = front;
            front = n;
        }
    }   
}

class Node {
    public Node next;
    private String _val;

    public Node(String val) {
        _val = val;
    }
}

Don't you have access to "Next" node ?

In that case

public void insert(E data) {
    if (front == null) { 
        front = new Node(data, null);
    } else {
        Node temp = new Node(data, null);
        temp.next = front;
        front = temp;
    }
}

--

 class LinkedList {
    Node front;

    LinkedList() { 
        front = null; 
    }

    public void AddToFront(String v) {
        if (front == null) {
            front = new Node(v);
        } else {
            Node n = new Node(v);
            n.next = front;
            front = n;
        }
    }   
}

class Node {
    public Node next;
    private String _val;

    public Node(String val) {
        _val = val;
    }
}
南街女流氓 2024-10-22 15:26:14

我假设 Node 构造函数将 next 指针作为其第二个参数,在这种情况下,我没有看到这段代码有任何明显的错误。这听起来确实像是一个家庭作业问题。如果是,您应该这样标记它。

I'm assuming that the Node constructor takes a next pointer as its 2nd argument, in which case I don't see anything obvious wrong with this code. This really sounds like a homework question. If it is, you should tag it as such.

等往事风中吹 2024-10-22 15:26:14

以我有限的链表知识,我冒昧地说:

Node temp = new Node(data);
temp.next = front;
front = temp;

您可能想等待有人确认。

With my limited linked list knowledge, I would venture this:

Node temp = new Node(data);
temp.next = front;
front = temp;

You might want to wait around for somebody to confirm though.

泅人 2024-10-22 15:26:14

这会创建一个循环。我该如何避免这种情况?

如果没有链表实现的其余代码,就不可能确定,但​​您提供的代码看起来根本不会创建循环。

如果正在创建一个循环,那么它很可能是在其他地方创建的。或者,您/您的测试将某些其他故障误诊为由循环引起。

如果您需要更多帮助,请发布更多代码/证据......特别是 Node 构造函数,以及让您认为自己有循环的代码。

This creates a cycle. How do I avoid that?

It is not possible to know for sure without the rest of the code for your linked list implementation, but the code that you have supplied doesn't look like it creates a cycle at all.

If a cycle is being created, it is most likely being created elsewhere. Alternatively, you / your tests are misdiagnosing some other failure as being caused by a cycle.

If you need more help, post more code / evidence ... particularly the Node constructor, and the code that makes you think you have a cycle.

独守阴晴ぅ圆缺 2024-10-22 15:26:14

添加一个新节点,如果当前头不为空,则将当前头指向新创建的节点作为下一个节点。

Node insert(Node head,int x) {
    Node node = new Node();
    node.data = x;
    if(head != null) {
       node.next = head;}
    return node;
}

Add a new node and if the current head is not null, then point the current head to the newly created node as the next node.

Node insert(Node head,int x) {
    Node node = new Node();
    node.data = x;
    if(head != null) {
       node.next = head;}
    return node;
}
不顾 2024-10-22 15:26:14

这是我在Java中将节点插入到链表的前面或头部的实现。

void insertAtHead(Object data){
    if(head==null) {
        head = new Node(data);
    }
    Node tempNode = new Node(data);
    Node currentNode = head;
    tempNode.setNext(currentNode.getNext());
    head.setNext(tempNode);
    incrementCounter();
}

This is my Implementation of Inserting a node to front or head of the Linked List in Java.

void insertAtHead(Object data){
    if(head==null) {
        head = new Node(data);
    }
    Node tempNode = new Node(data);
    Node currentNode = head;
    tempNode.setNext(currentNode.getNext());
    head.setNext(tempNode);
    incrementCounter();
}
过去的过去 2024-10-22 15:26:14

一个简单快速[可能效率不高]的解决方案是用新元素创建一个临时的新LinkedList,并将两个列表与前面的临时列表合并在一起。
请参阅下面的示例

import java.util.*;
public class Main
{

    public static Queue<Integer> addFirst(Queue<Integer> intQueue, Integer i){
        Queue<Integer> intQueue2 =  new LinkedList<Integer>();
        intQueue2.add(i);
        intQueue2.addAll(intQueue);
        intQueue = intQueue2;
        return intQueue;
    }

    public static void main(String[] args) {
        System.out.println("Hello LinkedList");

        Queue<Integer> intQueue =  new LinkedList<Integer>();
        intQueue.add(3);
        intQueue.add(4);
        intQueue.add(5);

        intQueue = addFirst(intQueue,2);
        intQueue = addFirst(intQueue,1);


        System.out.println(intQueue);
    }
}

A simple and quick [ may not be efficient ] solution is to create a temporary new LinkedList with the new element and merge the two lists together with the temp-list in the front.
see the example below

import java.util.*;
public class Main
{

    public static Queue<Integer> addFirst(Queue<Integer> intQueue, Integer i){
        Queue<Integer> intQueue2 =  new LinkedList<Integer>();
        intQueue2.add(i);
        intQueue2.addAll(intQueue);
        intQueue = intQueue2;
        return intQueue;
    }

    public static void main(String[] args) {
        System.out.println("Hello LinkedList");

        Queue<Integer> intQueue =  new LinkedList<Integer>();
        intQueue.add(3);
        intQueue.add(4);
        intQueue.add(5);

        intQueue = addFirst(intQueue,2);
        intQueue = addFirst(intQueue,1);


        System.out.println(intQueue);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文