This how you would add an item:
public void insert (Object item)
{
Link add = new Link();
add.data = item;
add.next = head;
_head = add;
++_l
You need a temporary variable that start from the head, traverse each node until the desired position or the end of the list, then insert the new node.
Since it is a homework exercise, I will only post pseudo code:
if pos < 0
//define what to do here...
return
end if
if pos == 0
//inserting at the beginning
insert(item)
return
end if
Link temp <- head
int index <- 0
while index < pos and temp->next != null
temp <- temp->next
index <- index + 1
end while
//now you're at your desired location or at the end of the list
Link newLink <- new Link
newLink->data <- item
newLink->next <- temp->next
temp->next <- newLink
It is definetly possible. But what would matter most is deciding at which position to insert new element because after each insertion the list would change and position of new element coming will have to be decided appropriately. You can try this
insertat=head;
for(i=0;i<pos;i++){
insertat=insertat.next;
}
add.next=insertat.next;
insertat.next=add;
listsize++;
You should do something like this:
public void insert (Object item, int pos)
{
Link add = new Link();
int ix = pos - 1;
Link cur = _head;
for (int i = 0; i < _list_size; i++) {
if(i == ix) {
add.next = cur.next;
cur.next = add;
}
cur = cur.next;
}
++_listsize;
}
After attempting alone the implementation of the concept, you can consider the open-source research. One of the best things you can do with open source is learn from it, study the implementation of java.util.LinkedList,
following the logic of the method add (int index, E element) { http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#360 }, you can divide in;
1) Get where the element will be added, in your case "link" http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#380
2) and you can examine the code that links the elements following the logic "before adding" in the code snippet http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#794
So, you will understand the logic behind the algorithm and will be able to perform your own implementation
It seems you have not correctly inserted the new Link
into the list. When you do that, you need to find the Link
at the given position as well as the Link
at the previous position. Then only you can set the previous.next = add
and add.next = position
.
Below is the updated method that does the task.
public void insert (Object item)
{
Link add = new Link();
add.data = item;
add.next = _head;
_head = add;
++_listsize;
}
public void insert (Object item, int pos)
{
Link add = new Link();
add.data = item;
int ix = pos - 1;
add.next = _head;
Link previous = _head;
for (int i = _listsize - 1; i > ix; --i) {
previous = previous.next;
}
Link position = previous.next;
previous.next = add;
add.next = position;
++_listsize;
}
Use add( int index, E element)
, which insert the element at the specified index : http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#add(int, E)
EDIT : if you're using LinkedList, of course ; with your own class, you have to store prev/next pointers and simply update them (previous node next's pointer should point to the new element, and next node previous's pointer should point to the new element too)