Minchae
Minchae

Reputation: 83

Storing more than one information into one Node in a singly linked list

I'm trying to add several information into one Node in a singly linked list... How do I do that?

After asking the user for several vehicle information: plateNo(String), vehicleType(String), serviceType(String) I will have to store this information for each vehicle. I have to use a singly linked list to store all the vehicle entering and leaving the wash.

Then, my program should display all the vehicles entering and leaving the vehicle wash with their service order.

How do I do this?

This is my singly LinkedList:

public class LinkedList<T>
{
    private Node<T> head; // first node in the linked list
    private int count;

    public int getCount() {
        return count;
    }

    public Node getHead() {
        return head;
    }

    public LinkedList() {
        head = null;    // creates an empty linked list
        count = 0;
    }

    public void displayList(){
        Node<T> current = head;     // start at beginning
        while(current != null)      // until end of list,
        {
            System.out.print(current.getData() + " ");
            current = current.getNext();
            //move to next link
        }
        System.out.println("");
    }

    public Node deleteFront()
    {
        Node<T> temp = head;
        if(head.getNext() == null)  // if only one item
            return null;           // return null
        head = head.getNext();      // first --> old next
        count--;
        return temp;
    }

    public void removeValue(T value)
    {
        Node<T> current = head, prev = null;
        while (current != null)
        {   //if current node contains value
            if (value == current.getData())
            {
                //handle front removal (case 1)
                if( prev == null)
                    head = current.getNext();
                else //handle mid removal (case 2)
                    prev.setNext(current.getNext());
                    // prev node now points to                             maxNode's (a.k.a current)                           successor, removing max node.

                break; // remove first occurence only
            }
            // update prev to next position (curr)
            prev = current;
            // move curr to the next node
            current = current.getNext();
        }
    }

    public void addFront(T n)
    {
        Node<T> newNode = new Node<T>(n);
        newNode.setNext(head);
        head = newNode;
        count++;
    }
}

My Node

public class Node<T> {
    private T data;
    private Node next;
    public T getData() {
        return data;
    }
    public void setData(T data) {
       this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public Node(T data) {
        this.data = data;
        this.next = null;
    }
}

Upvotes: 1

Views: 905

Answers (2)

Raj Maheshwari
Raj Maheshwari

Reputation: 9

Your code seems fine. You just need to define a new class that contains all the information that you want to store. As you have already made the Node class for a generic data type T, you can then insert the new class that you will make here.

class Details{
    String plateNo;
    String vehicleType;
    String serviceType;

    public Details(){
        this.plateNo = "";
        this.vehicleType = "";
        this.serviceType = "";
    }
} 

Then in your code for the linked list:

public class LinkedList<T>
{
    private Node<Details> head = new Details();
    //rest of the class

}

Upvotes: 0

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

I'm trying to add several information into one Node in a singly linked list... How do I do that?

... by thinking object-oriented! Create a class that models a vehicle:

class Vehicle {
    String plateNo;
    String vehicleType;
    String serviceType;
    // constructors, getters, setters, other methods ...
}

You have already a generic Node<T>, so use it:

Vehicle vehicle = callAwesomeMethodThatCreatesVehicleInstance();
Node<Vehicle> node = new Node(vehicle);

Now you can use such a node in your linked list.

Upvotes: 3

Related Questions