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

后端 未结 7 1471
天命终不由人
天命终不由人 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: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

提交回复
热议问题