Search code examples
javalinked-listcircular-list

Array of LinkedList with multiple values in Java


I'm trying to create an array or ArrayList which holds the 3 separate linked lists.

Let's say I have an Array of Departments (Sales, Media, Crew) Each element is a circular linked link of name, title, id, and pay rate.

Example:

Sales:
Node 1: Name Title       ID    Payrate
        Mary Salesperson 1378 25.46

Node 2: Name Title       ID    Payrate
        Mary Salesperson 1364 20.13

Media:
Node 1: Name Title       ID    Payrate
        John Designer    563   30.50

Node 2: Name  Title       ID    Payrate
        Chris Designer    586   563

I want to be able to retrieve the nodes in that specific linked list like Departments(Sales).next, etc. This is my current code:

public class Circle{
    
    public static class CircularLinkedList {
        private Node head = null;
        private Node tail = null;
        private int length;
    
        class Node{
            private Node next;
            private String deptNameIn;
            private String empNameIn;
            private String title;
            private int id;
            private float payrate;
            
            public Node(String deptNameIn, String empNameIn, String title, int id, float payrate) {
                this.deptNameIn = deptNameIn;
                this.empNameIn = empNameIn;
                this.title = title;
                this.id = id;
                this.payrate = payrate;
            }
        }

        public CircularLinkedList() {
            length = 0;
        }
        
        public void insertNodeRight(String deptNameIn, String empNameIn, String title, int id, float payrate) {
            Node newNode = new Node(deptNameIn, empNameIn, title, id, payrate);
            
            if (head == null) {
                head = newNode;
            } else {
                tail.next = newNode;
            }
            tail = newNode;
            tail.next = head;
            
            length++;
            
        }
        
        public static CircularLinkedList createCircularLinkedList() {
            CircularLinkedList cll = new CircularLinkedList();
        
            cll.insertNodeRight("Sales", "Mary", "Sales_person", 568, (float) 12.50);
            cll.insertNodeRight("Sales", "Mary", "Sales_person", 589, (float) 25);
            cll.insertNodeRight("Sales", "Vallerie", "Manager", 123, (float) 30);
            
            return cll;
        }

   }
    
    public static void main(String[] args) {
        CircularLinkedList cll = CircularLinkedList.createCircularLinkedList();
        
        
    }
    
}

Also two more sidenotes, I can currently insert from the rightmost, but how can I insert from the leftmost. Also, how can I delete a node? Everything I've seen online shows deleting a node with only one value, but I have multiple values.


Solution

  • circular linked link of name, title, id, and pay rate.

    Also, how can I delete a node? Everything I've seen online shows deleting a node with only one value, but I have multiple values.

    No, you don't. You have a circular linked list of Node objects. Your linked list consists of Node objects - not 'multiple values'.

    Once you can wrap your head around that, you'll see all the thousands of blog posts and tutorials you find when you search the web for 'how to write a linked list in java' apply here just the same.

    How do I insert an new node at the start?

    Well, think about it. After you're doing adding a new node 'at the front', head needs to be pointing at this new node, and the new node's next needs to be pointing at what used to be head.

    Also, of course, length must be increased. It's the exact same code as what you already have for insertNodeRight, just, flip head/tail and next/prev around.

    Deleting nodes is, again, the same principle. Think about what the head/tail pointers of your linked list start out as, and what they need to become, and what the next/prev pointers in your list start out as, and what they need to become. Sketch it out on paper if you like.

    Once you've figured it out, just write code that does what you sketched; for exercises like this, it's trivial, and your insertNodeRight code shows the way.