Java 在LinkedList中最后插入的概念

Java 在LinkedList中最后插入的概念,java,algorithm,data-structures,linked-list,Java,Algorithm,Data Structures,Linked List,我有Link1类来创建链接并显示链接数据, LinkedList 1创建列表并在LinkedList中插入元素。和singleLinkedList类调用addFirst和addLast的方法。 实现细节如下。唯一的问题是指针,在addlast方法的最后一个元素插入数据后,应该为最后一个链接设置指针。 Link1类实现 class Link1{ public String str; public Link1 forward; public Link1(String st

我有
Link1类
来创建链接并显示链接数据,
LinkedList 1
创建列表并在LinkedList中插入元素。和
singleLinkedList
类调用
addFirst
addLast的方法。
实现细节如下。唯一的问题是指针,在
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);
    }
}
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;
        }
    }
}
LinkList1类实现

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);
    }
}
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;
        }
    }
}
单链接列表实现

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);
    }
}
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;
        }
    }
}
公共类单链接列表{

    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
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
第二条注释行为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
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
预期结果 “最后插入”应至少插入元素任意次数。

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

Biggy:你能解释一下这将如何让我到达我的LinkedList的末尾吗?我正在尝试的是到达末尾并在last and break中存储该值。有什么帮助吗!!!问题是否没有正确发布?信息不够。请让我知道。我希望解决这个问题。