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

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.