LL: Constructor
Create a constructor for a LinkedList class that initializes a new linked list with a single node.
Instructions:
Understand the Node Structure:
Before writing the constructor, ensure you're familiar with the Node class, which is a nested class within the LinkedList class. Each Node should hold an integer value and a reference to the next node (Node next). For the initial node, next will be null.
Constructor Signature:
Your constructor should be public and named LinkedList. It will accept a single integer argument, value, which represents the data to store in the list's first node.
Create the First Node:
Inside the constructor, use the passed value to create a new instance of the Node class. This instance will be the first and only node in your linked list upon creation.
Initialize Head and Tail:
Assign the newly created node to both the head and tail of the linked list. This is because, initially, the list contains only one node, making it both the beginning and end of the list.
Set the List Length:
Initialize the length of the linked list to 1, reflecting that the list currently contains a single node.
Deliverable:
Implement the LinkedList constructor following the above guidelines. This constructor will lay the foundation for further methods you might implement in the LinkedList class, such as adding or removing nodes, searching for values, etc.
public class LinkedList {
code
private Node head;
private Node tail;
private int length;
class Node {
int value;
Node next;
Node(int value) {
this.value = value;
}
}
// +===================================================+
// | WRITE YOUR CODE HERE |
// | Description: |
// | - Constructor for the LinkedList class. |
// | - Initializes the linked list with a single node. |
// | |
// | Parameters: |
// | - value: The integer value of the first node in |
// | the newly created linked list. |
// | |
// | Behavior: |
// | - A new Node is created with the given value. |
// | - This node is set as both the head and tail of |
// | the list, indicating it is the only node in the |
// | list at creation. |
// | - The length of the list is initialized to 1. |
// +===================================================+
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public int getLength() {
return length;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.println(temp.value);
temp = temp.next;
}
}
public void printAll() {
if (length == 0) {
System.out.println("Head: null");
System.out.println("Tail: null");
} else {
System.out.println("Head: " + head.value);
System.out.println("Tail: " + tail.value);
}
System.out.println("Length:" + length);
System.out.println("\nLinked List:");
if (length == 0) {
System.out.println("empty");
} else {
printList();
}
}
}
Here is the implementation of the LinkedList constructor based on your detailed specifications. This constructor will initialize the linked list with a single node that contains the provided integer value.
java
public class LinkedList {
private Node head;
private Node tail;
private int length;
class Node {
int value;
Node next;
Node(int value) {
this.value = value;
this.next = null; // Next will be null since it's the only node
}
}
// Constructor for the LinkedList class
public LinkedList(int value) {
Node newNode = new Node(value); // Create a new node with the given value
this.head = newNode; // Set head to this new node
this.tail = newNode; // Set tail to this new node
this.length = 1; // Initialize length to 1
}
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public int getLength() {
return length;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.println(temp.value);
temp = temp.next;
}
}
public void printAll() {
if (length == 0) {
System.out.println("Head: null");
System.out.println("Tail: null");
} else {
System.out.println("Head: " + head.value);
System.out.println("Tail: " + tail.value);
}
System.out.println("Length: " + length);
System.out.println("\nLinked List:");
if (length == 0) {
System.out.println("empty");
} else {
printList();
}
}
// Example usage
public static void main(String[] args) {
LinkedList list = new LinkedList(10); // Creating a linked list with a single node containing value 10
list.printAll(); // Print the state of the linked list
}
}
Explanation of the Constructor
Node Creation: A new instance of Node is created by passing the provided integer value to its constructor. The next reference for this node is automatically set to null due to the implementation of the Node constructor.
Head and Tail Initialization: The head and tail of the LinkedList are both set to this new node since it is the only node currently in the list.
Length Initialization: The length of the linked list is initialized to 1, indicating that there is one node in the list.
Example Usage
The main method demonstrates how to create an instance of the LinkedList class with a single node containing the value 10, and then it prints out the linked list structure using the printAll() method. You can test this in a Java environment to see how the linked list behaves.