Implementing a Generic Double Linked Class with refer to current obeject - java

For an assignment we were asked to implement a fully generic DoubleLinkedList class, and there was a part where I was unsure of that I need help with. I tried to search online, but I was unable to find an answer.
For the addToEnd and addToFront methods, they each have the return type as BasicDoubleLinkedList and should return a "reference to the current object"
When I try to retun a node, it doesn't allow me to because its not a BasicDoubleLinkedList object. I need to use "this" however, I am unsure how to actually go about doing it.
Here is my code so far
*my inner class node
public class Node <T>
{
private T data;
private Node<T> next;
private Node<T> prev;
public Node()
{
}
}
public class BasicDoubleLinkedList<T> {
private Node<T> head; // points to the head node
private Node<T> tail; // points to the last node
int size;
public BasicDoubleLinkedList<T> addToFront(T data)
{
Node<T> newNode = new Node<T>();
newNode.data = data;
if(head == null)
{
head = newNode;
}
else{
head.prev = newNode;
newNode.next = head;
head = newNode;
}
return null; <------------ I am getting a bit confused somewhere, as I am not sure what to put for the return type that is BasicDoubleLinkedList<T>
}
}

Related

How compiler is knowing about head in linkedlist

How here head is automatically got the address of the node when we did not pointed we just created a node with name head
`we are directly using the head as if head is not equal to null do this but how head is getting any value in it thats my question.
public class Main {
public int size ;
Main(){
this.size=0;
}
Node head;
class Node{
String data;
Node next;
Node(String data){
this.data= data;
this.next=null;
size++;
}
}
// add in first
public void addFirst(String data){
Node Newnode = new Node(data);
if (head == null){
head = Newnode;
return;
}
Newnode.next=head;
head=Newnode;
}
}

Learning LinkedList and Nodes, but I keep getting a "cannot be resolved or is not in field" error on newNode in buildList method

I've been trying to figure this out for a few days, and everything I do is wrong.
I keep getting a "cannot be resolved or is not in field" error for the .next on newNode.next in buildList method.
I am also getting this same error for the .next and the .data on the current.next/ current.data in the printList method.
What I have is what is in the book, but it does not want to work in Eclipse.
Please help...
package linkedList;
import java.util.*;
import org.w3c.dom.Node;
public class ListOne {
//This part needs various options:
//Build list
//clear list
//check if the list is sorted
//insert at head
static Scanner input = new Scanner(System.in);
public static Node head;
public int linkedListCount = 0;
//public static LinkedList<Integer> intList = new LinkedList<Integer>();
private class MyNode{
private int data;
private Node next;
public MyNode(int data){
this.data = data;
this.next = null;
}
}
//BUILD LIST
public void buildList(int value){
Node newNode = (Node) new MyNode(value);
newNode.next = head;
head = newNode;
}
//Clear the list
public void clearList(){
head = null;
}
public void printList () {
if(head == null){
return;
}
Node current = head;
while (current != null) {
// visit
System.out.println(current.data);
current = current.next;
} // traversal
} // printList
public boolean isEmpty(){
return head == null;
}
}
Here is the errors I am receiving.
In method buildList, on newNode.next = "next cannot be resolved or is not a field." / In method printList, on current.data = "data cannot be resolved or is not a field." / In method printList, on current.next = "next cannot be resolved or is not a field."
I don't see any point in using the Node interface at all. Just use MyNode throughout:
package linkedList;
import java.util.*;
//import org.w3c.dom.Node; No need for this
public class ListOne {
// .....
public static MyNode head;
private class MyNode{
private int data;
private MyNode next;
public MyNode(int data){
this.data = data;
this.next = null;
}
}
//BUILD LIST
public void buildList(int value){
MyNode newNode = new MyNode(value);
newNode.next = head;
head = newNode;
}
// etc....
}

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;
}
}
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.
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
}

Java Searching LinkedList for data return true/false?

I need write a method to loop through a linked list searching to see if Object data is in linked list. Any help?
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
for(somethinggoeshere..){
if(head==data){
return true;
}else{
return false;
}
}
Any help?
Edit: My LinkedListNode class:
public class LinkedListNode {
private Object data;
private LinkedListNode next;
public LinkedListNode(Object data, LinkedListNode next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
Edit: Final solution for those who are interested:
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
LinkedListNode temp = head;
while(temp!= null) // check if you have reached the tail
{
if(data.equals(temp.getData()))
{
return true;
}
temp = temp.getNext(); // move to the next node
} // end of while loop
return false;
} // end of find method
Assuming that you wrote the code for LinkedListNode, you should know wether or not is Iterable, and thus be able to loop through it with a for-each loop like that.
As it stands, you should traverse the nodes either recursively or in an iterative manner, by using some form of "next" pointers that is held within each node, and in essence do a linear search through the links until you find the data you are looking for, or return null.
Here's a link for some help on implementing a linked list:
http://www.danielacton.com/Data-Structures/Linked-List/Java/
You need to loop through your LinkedList and search for the data. If you reach tail of the list and still unable to find the data this implies that data is not in the LinkedList.
I am assuming that LinkedListNode has a member variable data to store the data in each node. Below is the corrected code :
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data)
{
LinkedListNode temp = head;
while(temp!= null) // check if you have reached the tail
{
if(data.equals(temp.getData()))
{
return true;
}
temp = temp.getNext(); // move to the next node
} // end of while loop
return false;
} // end of find method
}

Generics collection implementation java compilation error

EDIT :
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
public synchronized E get() {
if (current!=null) return current.data;
return null;
}
}
}
the problem is that i get the following compilation Error on the emphasized lines :
> Type mismatch: cannot convert from LinkedList<E>.Node<E> to
> LinkedList<E>.Node<E>
what does it mean? and how do i fix this?
btw, the code is only part of the implementation so dont try to logicly figure it out.
--- Edited as the question changes slightly ---
The question is now becoming, how do I have two inner classes coordinate generic types? In short, they don't have to if they are both inner classes of an outer class where the generic type is bound. So even with the public synchronized E get() in the non-generic LinkedListIterator you are returning an E (and it is type safe).
However, if you then reach out to implement java.util.Iterator<E> things fall apart, because that E is based on a different class (interface) so the E has different scoping. How do you fix this? You need to parameterize your Node classes to Node<E> to satisfy that E bindings exist on the implementation of Iterator even when that implementation is being used outside of the scope of it's originating class. This forces Node<E> to be defined statically.
The reason it forces the static definition of Node<E> has to do with garbage collection. An Iterator might still be holding references to Nodes even though the LinkedList is scheduled for garbage collection. Sure, you might be able to keep such a thing from happening with a specific implementation, but the JVM has to allow any implementation (even an errant one).
Perhaps it is easier to explain with code
public LinkedList<E> {
public Iterator<E> iterator() {
return new LinkedIterator(head);
}
// private because we don't want instances created outside of this LinkedList
private class LinkedIterator implements Iterator<E> {
// Right here, needing a parameterized next node will force Node to be static
// static inner classes can exist outside of the scope of their parent
// Since it can exist outside of the parent's scope, it needs it's own generic parameter
private Node<E> next;
LinkedIterator(Node start) {
next = start;
}
public boolean hasNext() {
return next != null;
}
public E next() {
Node<E> retValue = next;
if (retValue != null) {
next = retValue.next;
}
return retValue;
}
}
// must be static because LinkedList might be garbage collected when
// an Iterator still holds the node.
// This E is not the same E as in LinkedList, because it is a E declaration (hiding the above E)
private static Node<E> {
Node<E> next;
Node<E> prev;
E data;
}
}
If you are not careful, you can now wind up back where you started; however, the key is to construct new Node<E> objects when needed in the parent scope. Since that is the same scope where you construct LinkedIterator types, the generic type safety will be ensured.
--- Original post follows ----
By specifying that your node class definition is a Node<E>, you basically create a second, independently scoped generic type E which will hide the outer generic type E in the LinkedList class.
Since none of your classes are static, they will only exist within context of a LinkedList class, which will provide the generics binding. That means you can simplify Node<E> to Node yet still put E class types within the Node class. Same goes for the LinkedListIterator, except that if you want it to implement Iterator you should indicate it implements Iterator<E>.
Due to request, what follows is the code that compiles on my machine, (java 1.6.0_20)
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
You overdid it a bit by parametrising the embedded classes. I removed all unnecessary ones.
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
Alternatively with a static class Node.
public class LinkedList<E> {
private static class Node<E2> {
protected Node next, prev;
protected E2 data;
protected Node(E2 dat) {
data = dat;
next = prev = null;
}
}
private Node<E> head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node<E> current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
It doesn't understand that the <E> in LinkedListIterator is the same <E> as the parent class. Just remove from the inner class:
public class LinkedListIterator {
private Node<E> current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}

Categories