简体   繁体   中英

deleting elements of a linked list

I have implemented a linked list that put their elements in front of the list everytime I call to a function, for example:

30-->40
|
first

//add 50

30-->40-->50
|
first

the code I implemented is the following:

public class Node {
    public int elem;
    Node next;
    public Node(int e){
        this.elem=e;
    }
}

public class List {
    Nodo list;
    Nodo first;
    public void addFront(int n){
        Nodo temp=new Nodo(n);
        if (list==null){
            first=temp;
        }
        else{
            list.next=temp;
        }
        list=temp;
    }

    public void print(){
        Nodo current;
        current=first;
        while (current!=null){
            System.out.println(current.elem);
            current=current.next;
        }
    }
    public Nodo deletefirst(){
        first=first.next;
        return list;
    }
public int size(){
        Nodo temp;
        temp=first;
        int c=0;
        while (temp!=null){
            c++;
            temp=temp.next;
        }
        return c;
    }

the problem I got is in my main program, when I do something like this:

List list4=new List();
list4.addFront(10);
list4.addFront(20);
list4.addFront(40);
list4.addFront(60);
list4.addFront(80);
for (int i=0;i<list4.size();i++){
    List4.deletefirst();
    System.out.println("List");
    List4.print();
}

the list that is visualized is the following:

list
20
40
60
80
list
40
60
80
list
60
80

how can I change my code so that it prints the missed values of the list, I mean:

list
80
list
null

Any help?

To add in front of the list:

public class List {
    Nodo first;

    public void addFront(int n){
        Nodo temp = new Nodo(n);
        temp.next = first;
        first = temp:
    }

Often first is named head as in "head of the list."

There is only one field needed to point to the first node.

Debugging your code, is best done on paper by mental execution.

Change your for loop to a while loop.

List list4=new List();
list4.addFront(10);
list4.addFront(20);
list4.addFront(40);
list4.addFront(60);
list4.addFront(80);
while (list4.size() > 0){
    list4.deletefirst();
    System.out.println("List");
    List4.print();
}

The for loop you were using increased i while decreasing the size of the list which is ripe for off-by-one errors or in your case off-by-two errors.

Important to note that I used .isEmpty() vs. < 0. This wouldn't matter in this code snippet but it is best practice. @azurefrog raises the point that this is a custom implementation of List without a isEmpty()

This is not properly an answer, but i don't have enough points to comment. You maybe interested in a iterator. You may need to change the Nodo list into a java.util.List , but then you can play with your list really easily declaring a Iterator<Nodo> it = list4.list.iterator() . The loop would become:

while(it.hasNext()){                 
    System.out.println(it.next());
}
System.out.println(it.next());          //outside the loop it.next() point to null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM