Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package estdados;
- public class List2 {
- static Node first = null;
- static Node last = null;
- public static void add(int value) {
- Node a = new Node();
- a.setInfo(value);
- if(first == null) {
- first = a;
- last = a;
- }else {
- last.setNext(a);
- last = a;
- }
- }
- public void delete() {
- if(first.getNext() != null) {
- Node penultimate = retrieveNextElement(first.getNext());
- penultimate.setNext(null);
- last = penultimate;
- }else {
- first = last = null;
- }
- }
- private Node retrieveNextElement(Node element) {
- if(element.getNext().equals(last)) {
- return element;
- }
- return retrieveNextElement(element.getNext());
- }
- }
- //node
- package estdados;
- public class Node {
- private int info;
- private Node next;
- public int getInfo() {
- return info;
- }
- public void setInfo(int info) {
- this.info = info;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment