Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

addFirst and removeFirst methods in java not working correctly (linked LIst)

I have a problem in my java code the problem is when I try to use methods multiple times here's an example

 /* works */
PureListString t = L.addFirst("abc"); 
System.out.println(t.get(0)); 

/* don't work */
  
L.addFirst("abc"); 
System.out.println(L.get(0))

These are my functions

    public PureListString(String elt) {
        this.first = elt;
        this.tail = EMPTY_LIST;
        this.size = 1;
    }

    public PureListString addFirst(String elt) {


        PureListString newCell = new PureListString(elt);
        PureListString tmp = this;
        tmp.size++;
        newCell.tail = tmp;
        tmp = newCell;
        return tmp;
    }

/* same as addFirst() */ 
    public PureListString removeFirst() {
        if (this.isEmpty()){
            return EMPTY_LIST;
        }
        PureListString tmp = this;
        tmp.size--;
        tmp = this.tail;
        return tmp;
    }

addLast method works perfectly

    public PureListString addLast(String elt) {
        PureListString newCell = new PureListString(elt);
        PureListString tmp = this;
        tmp.size++;
        while (tmp.tail != EMPTY_LIST)
            tmp = tmp.tail;
        tmp.tail = newCell;
        return tmp;
    }


    public static void main(String[] args){
        List<String> list1 = Stream.of("January", "February", "March", "April", "May") 
                                  .collect(Collectors.toList());
        PureListString L = new PureListString(list1);
        //PureListString t = L.addFirst("abc");
        //PureListString h = L.addFirst("a");
        //PureListString t = L.addFirst("abc");
        L.addLast("x");
        L.addLast("y");
        L.addLast("z");
        System.out.println(L.contains("y"));
        System.out.println(L.contains("z"));
        System.out.println(L.contains("May"));
        System.out.println(L.indexOf("z"));
        System.out.println(L.get(7));
        PureListString t = L.removeFirst();
        System.out.println(t.get(0));
        
        System.out.println(L.size);
        
    }

The problem is addFirst() and removeFirst() methods the same problem occurs in both of these methods I have always to do PureListString t = L.method().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this for your addFirst()

public void addFirst(Type e) {
  PureListString newCell = new PureListString(e); 
  newCell.next = first;
  first = newCell; 
  size++; 
  if (tail == null) 
  tail = first;
}

Then this for your removeFirst()

public PureListString  removeFirst(){
    if(size == 0){ return null;}
    else{
        PureListString temp = first;
        first = first.next;
        size--;
         
            if(first == null){tail = null;}
            return temp.element;
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...