最后在LinkedList中插入的概念

时间:2014-05-07 09:52:41

标签: java algorithm data-structures linked-list

我有Link1 class来创建链接并显示链接数据, LinkedList1创建列表并在LinkedList.And singleLinkedList类中插入元素以调用addFirstaddLast.的方法 实现细节如下。唯一的问题是在addlast方法中的最后一个元素插入数据后应该为最后一个链接设置的指针。

Link1类实现

class Link1{

    public String str;
    public Link1 forward;

    public Link1(String str){
        this.str=str;
        this.forward=null;
    }
    public void displayLink1(){
        System.out.println("Link DATA::"+str);
    }
}

LinkList1类实现

class LinkedList1{
    public Link1 first;
    public LinkedList1(){
        first=null;
    }
    public boolean isEmplty(){
        return first==null;
    }
    public void addFirst(String str){
        Link1 newLink=new Link1(str);
        newLink.forward=first;
        first=newLink;
    }
    public Link1 deleteFirst(){
        Link1 temp=first;
        first=first.forward;
        return temp;
    }
    public void insertLast(String str){
        Link1 current=first;
        Link1 last=first;
        Link1 newLinkLast=new Link1(str);
        while(current!=null){   
        current=current.forward;
            if(current==null);
            {
            last=current.forward;            //trying to reach to end of the linkedlist     
            break;
            }
        }
          newLinkLast.forward=last;                      //the last.displayLink is having the correct data evertime this is method getting called.But this is not going to my LinkedList
    //COMMENTED****last.forward=newLinkLast              //SOMETHING IS MESSED HERE,if i add one works perfect but the whole list gets messed up,including the elements added by insertFirst            
           last=newLinkLast;               
        last.displayLink1();                      //Just for testing i am printing the value of this link,which is coming correct everytime i call this method.
    }
    public void displayOB(){
        Link1 current=first;
        while(current!=null){
            current.displayLink1();
            current=current.forward;
        }
    }
}

singleLinkedList实施

public class singleLinkedList {

    public static void main(String args[]){
        LinkedList1 ll=new LinkedList1();

        ll.addFirst("A");
        ll.addFirst("B");
        ll.addFirst("C");

        ll.addFirst("d");
        ll.addFirst("e");
        ll.addFirst("f");

        ll.addFirst("x");
        ll.addFirst("y");
        ll.addFirst("z");

    ll.insertLast("Insert me at End 1");
    ll.insertLast("Insert me at End 2");
    ll.insertLast("Insert Again");

        ll.displayOB();
    }
}

输出  第一行的是newLinkLast.forward = last

Link DATA::Insert me at End 1     //this is the displayLink method called in insertLast

Link DATA::Insert me at End 2     //this is the displayLink method called in insertLast

Link DATA::Insert Again           //this is the displayLink method called in insertLast

Link DATA::z
Link DATA::y
Link DATA::x
Link DATA::f
Link DATA::e
Link DATA::d
Link DATA::C
Link DATA::B
Link DATA::A
第二个COMMENTED LINE专线的

是newLinkLast.forward = last

Link DATA::Insert me at End 1            //this is the displayLink method called in insertLast
Link DATA::Insert me at End 2            //this is the displayLink method called in insertLast

Link DATA::Insert Again                  //this is the displayLink method called in insertLast

Link DATA::z
Link DATA::y
Link DATA::x
Link DATA::Insert Again

预期结果 insert last应该最后插入元素任意次。

1 个答案:

答案 0 :(得分:0)

应该是

       if(current!=null) //removed comma, changed == to !=
       {
        last=current.forward;             
        break;
       }