How to insert an item at a given position in a linked list?

后端 未结 7 1352
天命终不由人
天命终不由人 2021-01-21 12:50

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         


        
相关标签:
7条回答
  • 2021-01-21 13:18

    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
    
    0 讨论(0)
  • 2021-01-21 13:25

    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++;
    
    0 讨论(0)
  • 2021-01-21 13:28

    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;
     }
    
    0 讨论(0)
  • 2021-01-21 13:30

    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

    0 讨论(0)
  • 2021-01-21 13:35

    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;
    }
    
    0 讨论(0)
  • 2021-01-21 13:38

    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)

    0 讨论(0)
提交回复
热议问题