【问题标题】:Why my Single Linked List implementation is giving me my first element twice after adding objects?为什么我的单链表实现在添加对象后给了我两次我的第一个元素?
【发布时间】:2017-06-01 14:21:43
【问题描述】:

这里我尝试实现“单链表”的基本操作。 但只有在这里我只面临一个问题,即添加元素后,即

al.add("Ravi");
al.add("Vijay");
al.add("Sanjay");
al.add("Ajay"); 

我得到的输出是:
[]--------->对于空链表
[Ravi,Ravi,Vijay,Sanjay,Ajay]----->添加元素后。

class MyLinkedList {
        private Node first;
        private Node last;
        private int count;

        public void add(Object ele){
            if(first==null){
                first=new Node(ele);
                last=first;
                count++;
            }
            last.next=new Node(ele);
            last=last.next;
            count++;
        }
        public int size(){
            return count;
        }
        public Object get(int index){
            if(index>=size())throw new IndexOutOfBoundsException();
            Node p=first;
            for(int i=0;i<=index;i++){
                p=p.next;
            }
            return p.ele;
        }
        public void remove(int index){
            if(index>=size())throw new IndexOutOfBoundsException();
            if(index==0){
                first=first.next;
                count--;
                return;
            }
        }

        @Override
        public String toString() {
            if(size()==0)return "[]";
            Node p=first;
            String s="[" + p.ele;
            while(p.next!=null){
                p=p.next;
                s+=","+p.ele;
            }
            return s + "]";
        }

        private class Node{
            Object ele;
            Node next;
            Node(Object ele){
                this.ele=ele;
            }
        }

        public static void main(String[] args) {
             MyLinkedList al=new MyLinkedList(); 
             System.out.println(al);
              al.add("Ravi");  
              al.add("Vijay");  
              al.add("Sanjay");  
              al.add("Ajay");
              System.out.println(al);

        }

    }

【问题讨论】:

  • 那么你的问题到底是什么?你期待什么输出?您尝试过什么来解决此问题?您是否使用调试器单步调试过您的代码?
  • 是的,我已经检查过了。预期的输出应该是:[Ravi,Vijay,Sanjay,Ajay] 而不是:[Ravi,Ravi,Vijay,Sanjay,Ajay]

标签: java collections linked-list singly-linked-list


【解决方案1】:

因为你加了两次:

    public void **add**(Object ele){
        if(first==null){
            first=new Node(ele); //First
            last=first;
            count++;
        }
        last.next=new Node(ele); //second.
        last=last.next;
        count++;
    }

添加一个 else 语句:

    public void **add**(Object ele){
        if(first==null){
            first=new Node(ele);
            last=first;
            count++;
        } else {
          last.next=new Node(ele);
          last=last.next;
          count++;
        }
    }

【讨论】:

  • 谢谢,我认为问题在于 toString() 方法的实现。但是,由于 else 我没有得到想要的输出。谢谢好友!!!
【解决方案2】:

在你的方法中:

 public void **add**(Object ele){
    if(first==null){
       first=new Node(ele);
       last=first;
       count++;
    }
    last.next=new Node(ele);
    last=last.next;
    count++;
 }

您必须在 if 子句的末尾添加 return 语句,或使用 else。否则,您将第一个元素添加两次。

【讨论】:

  • @GAJENDRATIWARI 你不能接受两个答案,但你可以对两个答案都投赞成票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 2019-08-07
相关资源
最近更新 更多