Home > Software engineering >  Problem with printing own Linked List and implementing 2 methods
Problem with printing own Linked List and implementing 2 methods

Time:10-04

I created my own Linked List using nodes, and I have few methods to do.

Methods in main:

//List<Person> females = peopleFromWarsaw.getWithFilter(p -> p.getName().endsWith("a"));
// Dont know how to implements this methods.

//        ObjectContainer.removeIf(p -> p.getAge() > 50);
//        peopleFromWarsaw.storeToFile("youngPeopleFromWarsaw.txt",
//                p -> p.getAge() < 30, p -> p.getName()   ";"   p.getAge()   ";"   p.getCity());

At the begging i made a linked list and tried to print elements from list but doesnt work. At first it looks like it doesn't have a method toString but class Person has toString. I guess the program takes the node but I want to print object. Maybe u can see what is the problem with code, basically nodes and methods in Object Container are fine.

Secondly i have a problem with implementing methods in main (getWithFilter,removeIf and storeToFile) No matter how I write method in ObjectContainer, always intelij tells me that cannot use lambda and underline p.getName (p -> p.getName().endsWith("a")); In class Person i have getters and setters with fields name,age.

Maybe someone could explain how properly write this methods ?

public class ObjectContainer<T> {

    private Node head;
    private Node tail;
    private final Predicate<T> predicate;

    public ObjectContainer(Predicate<T> predicate) {
        this.predicate = predicate;
    }

    static class Node {

        private Object object;
        private Node next;

        Node(Object object) {
            this.object = object;
        }
    }

    public void add(T object) {
        if (!predicate.test(object)) {
            throw new IllegalArgumentException("Element can not be added");
        }
        Node newNode = new Node(object);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
    }

    public int size() {
        int count = 0;
        Node current = head;

        while (current != null) {
            count  ;
            current = current.next;
        }
        return count;
    }

    public void push(T new_data) {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }

    public void insertAfter(Node prev_node, T new_data) {
        if (prev_node == null) {
            System.out.println("The given previous node cannot be null");
            return;
        }
        Node new_node = new Node(new_data);
        new_node.next = prev_node.next;
        prev_node.next = new_node;
    }

    public void append(T new_data) {
        Node new_node = new Node(new_data);
        if (head == null) {
            head = new Node(new_data);
            return;
        }
        new_node.next = null;

        Node last = head;
        while (last.next != null)
            last = last.next;

        last.next = new_node;
        return;
    }

    public void printList() {
        Node tnode = head;
        while (tnode != null) {
            tnode = tnode.next;
            System.out.println(tnode);
        }
    }
}

CodePudding user response:

Probably you don't want to print the node, but the object contained within. Also, you want to print the first object, too. That is

public void printList() {
    Node tnode = head;
    while (tnode != null) {
        System.out.println(tnode.object);
        tnode = tnode.next;
    }
}
  • Related