问题
This how you would add an item:
public void insert (Object item)
{
Link add = new Link();
add.data = item;
add.next = head;
_head = add;
++_listsize;
}
But how do you add an item at a given position. So far this is what I got:
public void insert (Object item, int pos)
{
Link add = new Link();
int ix = pos - 1;
add.next = _head;
for (int i = _listsize - 1; i >= ix; --i)
add = add.next;
add.data = item;
_head = add;
++_listsize;
}
This will insert the item correctly if it is sequential, but let say I am given a position which is in the middle, what it will do it will insert the item but it will completely cut off (or delete the rest). For example:
insert at 1: a
insert at 2: b a
insert at 3: c b a
insert at 2: d a
回答1:
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;
}
回答2:
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;
}
回答3:
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)
回答4:
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++;
回答5:
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
回答6:
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
回答7:
My solution is not as clean as it will be with recursion but if you are testing more than 50,000 elements on the list go with an iterative solution. or you can modify the JVM and change the stack size. Because you can get a stack overflow just because you will pass the capacity of activation records in the stack. Think about the worst case scenario that you will do an insert at the end of the list.
/**
* Inserts the specified element at the specified position in this list.
*
* @param :index the desire position starting from 0 2,3,4,5
* @param :data the content of the new node
* @return Boolean: if the insertion was successfully return true otherwise false
*/
public boolean add(int index, T data) {
/*Add to the end of the list*/
if (index == size()) {
add(data);
return true;
}
/*Empty list and index bigger than 0*/
else if (this.front == null && index != 0) {
return false;
} else {
Node<T> tempNode = this.front;
while (tempNode != null && tempNode.next != null && --index != 0) {
tempNode = tempNode.next;
}
if (index != 0) {
return false;
} else {
Node<T> newNode = new Node<T>(data);
/*Empty list,and index is 0*/
if (tempNode == null) {
this.front = newNode;
} else {
newNode.next = tempNode.next;
tempNode.next = newNode;
}
}
}
return true;
}
来源:https://stackoverflow.com/questions/20076364/how-to-insert-an-item-at-a-given-position-in-a-linked-list