0

I am practicing with LinkedList in java. I have bought a book where one of the exercises is to create a student directory using a singly linkedlist. The directory can store the students full name and gpa.

I need to implement and write the code for the methods removeFirst, removeLast, insertFirst, insertLast, getStudent etc(which are usually built in iterators of the linkedlist). I don't know where to create these custom iterators. The goal is to be able to make a linkedlist of type StudentDirectory in my main and be able to use my custom methods to remove, add, get, and generate list of students in the linked list. This leads me to believe that all of these methods need to live in my StudentDirectory class but unfortunately I do not know how to begin.

I have posted my code but I would appreciate a nudge in the right direction. I dont want any total solutions for I want to be able to do this on my own rather than copying someone else's code. I understand I need to iterate through the linkedlist via traversing through the nodes in the linkedlist but I dont know how/where to declare the nodes head

public class studentDirectory {
    private String fullName;
    private double gpa;




    public studentDirectory(String name, double gpa){
        fullName = name;
        this.gpa = gpa;
    }

    public void setGpa(double grade){
        gpa = grade;
    }

    public void setName(String name){
        fullName = name;
    }

    public double getGpa(){
        return gpa;
    }

    public String getName(){
        return fullName;
    }

    public void insertAtEnd(studentDirectory lastItem){


    }

    public void insertAtStart(studentDirectory firstItem){

    }

    public void getStudent(studentDirectory student){

    }

    public void updateGpa(studentDirectory update){

    }

    public void removeFirst(studentDirectory first){

    }

    public void removeLast(studentDirectory last){

    }

    public void generateStudentList(){

    }
1
  • 1
    I would probably start by creating a Node<T>, it might have a T (or Optional<T>) value and a Node<T> (or Optional<Node<T>>) next. But that's literally the minimum you need for a "LinkedList". Commented Sep 29, 2016 at 1:18

1 Answer 1

1

I think you are confusing some concepts here. A list should be generic, i.e. it should not know about the data type it contains. So you should separate the student data from the list itself, ideally by making the list generic:

public class LinkedList<T>{
    // your LinkedList needs nodes:
    static class Node<T>{
        Node<T> next;
        T value;
    }
    Node<T> rootNode;
}

public class Student{}

Now your student directory can be modeled as a LinkedList<Student>

Some of the methods you list make sense in the LinkedList class (insertAtStart, insertAtEnd), while anything student-specific should be moved to the Student class.

And your LinkedList class will also need a Node class

Sign up to request clarification or add additional context in comments.

1 Comment

So should the removeLast, removeFirst etc be declared in the LinkedList<T> class. Regardless of the class I am still confused on how to be able to search through the linkedlists nodes. I understand the concept ie Node<E> node--> if(node.next == null) do something but I guess I am confused on what the 'next' is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.