Разобраться со строкой Linkedlist для удаления узла из текстового файла - PullRequest
0 голосов
/ 22 марта 2020

Я пытаюсь удалить узел, читая из текстового файла. Мне удалось создать узел, отсортировать узел и распечатать узел, но я застрял, чтобы удалить узел из распечатанного списка. До того, как я поработал с числами, все было хорошо, но у меня возникают проблемы со списком строк. Не могли бы вы помочь мне удалить узел. Меня просят сделать «Пользователь должен иметь возможность удалить одно имя из списка». Вот мой код.

public class Node {
    String name;
    Node next;

    public Node(String name) {
        this.name = name;
    }
}

public class LinkedList {
    public Node front;

    public LinkedList startWith(String str) {
        LinkedList tempList = new LinkedList();
        Node pointer = front;
        while (pointer != null) {
            if (pointer.name.charAt(0) == str.charAt(0)) {
                tempList.add(pointer.name);
            }
            pointer = pointer.next;
        }

        return tempList;
    }

    public void add(String name) {
        if (front == null) {
            front = new Node(name);
        } else {
            Node at = sort(name);
            insertBefore(at, name);
        }

    }

    public void insertAfter(Node n, String name) {
        if (n.next == null) {
            n.next = new Node(name);
        } else {
            Node temp = n.next;

            Node toInsert = new Node(name);
            toInsert.next = temp;

            n.next = toInsert;
        }
    }

    public void deleteNode(Node n) {
        if (n == front) {
            front = front.next;
        } else {
            Node prev = getPrev(n);
            Node next = n.next;
            prev.next = next;
        }
    }

    public void insertBefore(Node n, String name) {
        if (n == front) {
            Node temp = new Node(name);
            temp.next = this.front;
            this.front = temp;
        } else {
            Node temp = getPrev(n);
            insertAfter(temp, name);
        }
    }

    public Node getPrev(Node n) {
        Node temp = front;
        if (front == n) {
            // Can't go back
            System.out.println("Can't go previous to front");
            return null;
        }
        while (temp.next != null) {
            if (temp.next.equals(n)) {
                return temp;
            }
            temp = temp.next;
        }
        return temp;
    }

    public Node sort(String name) {

        if (front == null) {
            return front;
        } else {
            Node temp = front;
            while (temp != null) {
                if (name.compareTo(temp.name) < 1) {
                    return temp;
                } else {
                    temp = temp.next;
                }
            } // while(temp.next != null);

            return null;
        }
    }

    public void addAtFront(String name) {
        if (front == null) {
            front = new Node(name);
        } else {
            Node temp = new Node(name);
            temp.next = front;
            this.front = temp;
        }
    }

    public void addAtLast(String name) {
        if (front == null) {
            front = new Node(name);
        } else {
            Node last = findLastnode();
            last.next = new Node(name);
        }
    }

    public Node deleteFront() {
        if (front == null) {
            return null;
        } else {
            return front = front.next;
        }
    }

    public void print() {
        Node temp = front;
        System.out.println("");
        if (temp == null) {
            System.out.println("List Is Empty. ");
        } else {
            while (temp != null) {
                System.out.println(temp.name + " ");
                temp = temp.next;
            }
        }
    }

    public Node findLastnode() {
        if (front == null) {
            return front;
        } else {
            Node temp = front;
            while (temp.next != null) {
                temp = temp.next;
            }
            return temp;
        }
    }

    public void empty() {
        this.front = null;
    }

}

класс, чтобы прочитать файл и попросить пользователя выполнить задачу.

import java.io.File;
import java.util.Scanner;

public class FileIn {

    public static void main(String[] args) {

        System.out.println("Enter the filename: ");
        Scanner sc = new Scanner(System.in);
        String filename = sc.next();

        String[] names = readFile(filename);
        LinkedList list = new LinkedList();

        System.out.println("Enter the length of data:");
        int length = sc.nextInt();

        for (int i = 0; i < names.length && i < length; i++) {
            list.add(names[i]);
        }
        list.print();

        System.out.println("\nIndex number that you want to delete: ");
        int num = sc.nextInt();
        LinkedList.deleteNode(num);
        list.print();

        System.out.println("\nList Text beginning with letter : ");
        String begin = sc.next();

        LinkedList l = list.startWith(begin);
        l.print();

        sc.close();

    }

    public static String[] readFile(String file) {
        String[] temp = null;
        try {
            Scanner sc = new Scanner(new File(file));
            int count = 0;
            while (sc.hasNextLine()) {
                sc.nextLine();
                count++;
            }
            // defining size for names/hashed
            temp = new String[count];

            sc = new Scanner(new File(file));
            count = 0;
            while (sc.hasNextLine()) {
                temp[count] = sc.nextLine();
                count++;
            }

            sc.close();

        } catch (Exception e) {
            System.out.println("No file found.");
        }

        return temp;
    }

}
...