链表,它可以在末尾添加、在开头添加、删除和显示内容 — Java

huangapple go评论105阅读模式
英文:

Linked List that appends, prepends, delete and show contents -- Java

问题

public class LinkedListLogic {
    
    private class Node {
        Node next;
        int data;

        public Node(int data) {
            this.data = data;
        }
    }

    Node head;

    public void addToLastElement(int data) {
        if (head == null) {
            head = new Node(data);
            return;
        }

        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new Node(data);  // Appending a new node at the end
    }

    public void insertAtBeginning(int data) {
        Node newHead = new Node(data);
        newHead.next = head;
        head = newHead;
    }

    public void deleteAtSpecificValue(int data) {
        if (head == null) {
            return;
        }
        if (head.data == data) {
            head = head.next;  // Removing the first node
            return;
        }
        Node current = head;
        while (current.next != null && current.next.data != data) {
            current = current.next;
        }
        if (current.next != null) {
            current.next = current.next.next;  // Deleting the node with specific value
        }
    }

    public void printAll() {
        for (Node current = head; current != null; current = current.next) {
            System.out.println(current.data);
        }
    }

    public static void main(String[] args) {
        LinkedListLogic a = new LinkedListLogic();
        a.addToLastElement(2);
        a.addToLastElement(4);
        a.printAll();
    }
}
英文:

I've just started to learn about linked lists and I am attempting to implement my understanding of it. However, it does not work and program runs without errors.

The program should be able to append, insert at the beginning, delete a node that contains a specific value and print out the contents. The program runs but does not show anything

public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
}
Node current = head;
while (current.next.data != data) {
current = current.next;
}
current.next = current.next.next; 
return;                           
}
public void printAll() {
for (Node current = head; current.next != null; current = current.next) {
System.out.println(current.data);
}
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.printAll();
}
}

答案1

得分: 0

感谢Andy Turner指出的错误

public class LinkedListLogic {

    private class Node {
        Node next;
        int data;

        public Node(int data) {
            this.data = data;
        }
    }

    Node head;

    public void addToLastElement(int data) {
        if (head == null) {
            head = new Node(data);
            return;
        }

        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new Node(data);
    }

    public void insertAtBeginning(int data) {
        Node newHead = new Node(data);
        newHead.next = head;
        head = newHead;
    }

    public void deleteAtSpecificValue(int data) {
        if (head == null) {
            return;
        }
        if (head.data == data) {
            head = head.next;
        }
        Node current = head;
        while (current.next.data != data) {
            current = current.next;
        }
        current.next = current.next.next;
        return;
    }

    public void printAll() {
        Node current;
        for (current = head; current.next != null; current = current.next) {
            System.out.println(current.data);
        }
        System.out.println(current.data);
        return;
    }

    public static void main(String[] args) {
        LinkedListLogic a = new LinkedListLogic();
        a.addToLastElement(2);
        a.addToLastElement(4);
        a.insertAtBeginning(5);
        a.deleteAtSpecificValue(2);
        a.addToLastElement(9);
        a.addToLastElement(9);
        a.addToLastElement(9);
        a.addToLastElement(9);
        a.deleteAtSpecificValue(9);
        a.printAll();
    }
}
英文:

Thanks to Andy Turner for pointing out the errors

public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
}
Node current = head;
while (current.next.data != data) {
current = current.next;
}
current.next = current.next.next;
return;
}
public void printAll() {
Node current;
for (current = head; current.next != null; current = current.next) {
System.out.println(current.data);
}
System.out.println(current.data);
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.insertAtBeginning(5);
a.deleteAtSpecificValue(2);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.deleteAtSpecificValue(9);
a.printAll();
}
}

答案2

得分: 0

public class LinkedListLogic  {

    private class Node {
        Node next;
        int data;

        public Node(int data) {
            this.data = data;
        }
    }

    Node head;

    public void addToLastElement(int data) {
        if (head == null) {
            head = new Node(data);
            return;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new Node(data);
    }

    public void insertAtBeginning(int data) {
        Node newHead = new Node(data);
        newHead.next = head;
        head = newHead;
    }

    public void deleteAtSpecificValue(int data) {
        if (head == null) {
            return;
        }

        Node current = head;
        Node previous = null;
        while (current != null && current.data != data) {
            previous = current;
            current = current.next;
        }
        if (current != null && current.data == data) {
            previous.next = current.next;
        }
        return;
    }

    public void printAll() {
        for (Node current = head; current != null; current = current.next) {
            System.out.print(current.data + " ");
        }
        System.out.println();
        return;
    }

    public static void main(String[] args) {
        LinkedListLogic a = new LinkedListLogic();
        a.addToLastElement(2);
        a.addToLastElement(4);
        a.addToLastElement(6);
        a.addToLastElement(9);
        a.printAll();
        a.insertAtBeginning(1);
        a.printAll();
        a.deleteAtSpecificValue(6);
        a.printAll();
        a.deleteAtSpecificValue(9);
        a.printAll();
    }

}
英文:
public class LinkedListLogic  {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
Node current = head;
Node previous = null;
while ( current != null && current.data != data) {
previous = current;
current = current.next;
}
if(current != null && current.data == data) {
previous.next = current.next;
}
return;                           
}
public void printAll() {
for (Node current = head; current != null; current = current.next) {
System.out.print(current.data+" ");
}
System.out.println();
return;
}
public static void main(String[] args) {
LinkedListLogic  a = new LinkedListLogic ();
a.addToLastElement(2);
a.addToLastElement(4);
a.addToLastElement(6);
a.addToLastElement(9);
a.printAll();
a.insertAtBeginning(1);
a.printAll();
a.deleteAtSpecificValue(6);
a.printAll();
a.deleteAtSpecificValue(9);
a.printAll();
}
}

huangapple
  • 本文由 发表于 2020年9月2日 03:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定