6

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.

2
  • 1
    How does this create a cycle? Commented Feb 3, 2011 at 5:58
  • 1
    This doesn't create a cycle. Would you care to provide the error the compiler generates when you try to compile your code? Commented Feb 3, 2011 at 6:14

7 Answers 7

8

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

It would be helpful if you said where the cycle is occurring. What points to what? Does your Node constructor have a bug in it?
2

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.

4 Comments

I agree with @takteek, it does look right if the second argument is to the next node.
Tried that too. It still creates a cycle.
I don't see anything wrong with this implementation of the insert method if the second argument is a pointer to the next node. Are you sure this is causing the cycle and not something else?
You don't need the last argument.
1

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.

Comments

0

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;
}

Comments

0

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

Comments

0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.