Hi everyone,

how do i push item with a givin index in a linkedlist.


e.g. linkedlist.add(element e, int index);

this is my function

public void push(Student s, int index){
        if(isEmpty()){
            start = s;
        }else{
            last.next = s;
        }
        last = s;
        size++;
    }

Dani AI

Generated

A quick, practical pattern to insert at a given index.

, you can keep your Student as the payload and wrap it in a simple Node type so the list internals stay separate from your data. is right that you must locate the insertion point; is right to flag index semantics and out‑of‑range behavior. The code below uses a temporary dummy head to avoid special cases for inserting at index 0, and it updates head, tail and size correctly.

private static class Node<E> {
    E value;
    Node<E> next;
    Node(E v) { value = v; }
}

public class SimpleLinkedList<E> {
    private Node<E> head;
    private Node<E> tail;
    private int size;

    public void add(int index, E elem) {
        if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: " + index);
        Node<E> dummy = new Node<>(null);
        dummy.next = head;
        Node<E> prev = dummy;
        for (int i = 0; i < index; i++) prev = prev.next;
        Node<E> n = new Node<>(elem);
        n.next = prev.next;
        prev.next = n;
        if (n.next == null) tail = n;
        head = dummy.next;
        size++;
    }
}

Notes and troubleshooting tips:

  • This follows standard Java List.add(index, element) semantics (throw when index > size); see the Java docs for reference (List.add).
  • Complexity is O(n) to find the spot; head insertion is O(1).
  • Keep size accurate and update tail when you append (when n.next == null). Forgetting either is the most common bug.
  • If the Student class already exposes next, consider splitting data and links to avoid coupling. For a refresher on insertion approaches, see a concise reference (insert at specific position).

Recommended Answers

All 5 Replies

In a linked list, you do not push anything. What you do is to hold on an object reference (for Java) which is pointing to your desired location. In this case, your 'last' variable is holding the last object reference. Though, I do not know what your 'index' value for? What is your Student's object variable? Look at the Student class definition.

In a linked list, you do not push anything. What you do is to hold on an object reference (for Java) which is pointing to your desired location. In this case, your 'last' variable is holding the last object reference. Though, I do not know what your 'index' value for? What is your Student's object variable? Look at the Student class definition.

Thank for the post Taywin,

Yes i do know that you don't push in a linkelist, but i like to customize my function so i can add at a desired index.

what i can do know is get the first object i inserted and the last object i inserted. But i want to insert objects in between like you do in an array.

i just edit my code because it was just a raw example of what i was trying to do
this is what it is now

public void push(Student s, int index){
        if(isEmpty()){
            last = s;
        }
        s.next = start;
        start = s;
        size++;
    }

I didn't use the index yet. but i like to implement it in the function. so i can insert student s, at a desired index.

If you want to insert a node into the middle of a linked list, you need a loop that moves through the list and a counter that increments at each move. When you have moved through the correct number of nodes, then you need to insert the new node in the middle. This is complicated because you need to make sure you "hook up" the new node correctly.

Say you have 10 nodes in your list and you want to insert a new node at index 5. You would start with a pointer to the head of the list (which is also the 1st node), call this current. Then you would make current = current.next 3 times. Now current points to the 4th node in the list. "s" is the node you want to insert, so first you make s.next = current.next so you don't lose the link to the old 5th node. Then you make current.next = s. When this is done, s will be the new 5th node in the list, and the old 5th node will be the 6th.

As kramerd said. Also, you need to identify what your index type is going to be (0 or 1 index). If the given index is greater than the length of the list, you could handle it in 2 ways - insert at the end of the list or throw an error. I would prefer inserting it at the end as if nothing wrong with the index value.

Thanx guys... i'll work on it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.