linked list iterator inner class-java - java

I am writing an iterator inner class that iterates through a list. Besides the remove method, I believe I have implemented all the methods of iterator correctly but I get an error saying "Bound mismatch: The type E is not a valid substitute for the bounded parameter > of the type List.Node". I believe this has to with having Node> implements Iterable at the top of my code but I do not want to change that if unneeded. Any possible suggestions on what I should do?
public class List<T extends Comparable<L>> implements Iterable<L> {
private class Node<N extends Comparable<N>> {
private N data;
private Node<N> next;
}
protected Node<L> head;
public Iterator<L> iterator() {
return new ListIterator<L>();
}
public class ListIterator<E extends Comparable<E>> implements Iterator<E> {
private Node<E> N = (Node<E>) head; //"Bound mismatch: The type E is not a valid substitute for the bounded parameter <D extends Comparable<D>> of the type List<T>.Node<D>"
public boolean hasNext() {
return (N.next != null);
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}

You should reduce the number of generic types. Because the inner classes know the generic type of their parent class, you should simplify the Node and ListIterator class:
public class List<L extends Comparable<L>> implements Iterable<L> {
private class Node {
private L data;
private Node next;
}
protected Node head;
public Iterator<L> iterator() {
return new ListIterator();
}
public class ListIterator implements Iterator<L> {
private Node N = head;
public boolean hasNext() {
return (N.next != null);
}
public L next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}

The type parameter N is declared as
N extends Comparable<N>
ie. it has bounds. It must be Comparable to itself.
The type parameter E is declared as
E
ie. it has no bounds. It can be any type, but not necessarily a type that is Comparable to itself.
Therefore, you can't use E where an N is expected. Consider adding the same bounds as N to E.

Related

How do I implement abstraction and generics in java?

I currently have a project that requires the use of abstraction and generics, but I don't even know where I should start for this. The abstract class is as follows.
public abstract class Links<AType> {
abstract AType getElem(); //returns the head of the list
abstract Links<AType> getNext(); //return the next link
}
This is the class that extends the abstract class
public class Cons<AType> extends Links<AType> {
AType elem;
Links<AType> next;
Cons(AType elem, Links<AType> next) {
this.elem = elem;
this.next = next;
}
#Override
AType getElem() {
return elem;
}
#Override
Links<AType> getNext() {
return next;
}
}
Here's another class that extends the abstract class
public class Nil<AType> extends Links<AType> {
Nil(){}
#Override
AType getElem() {
return null;
}
#Override
Links<AType> getNext() {
return null;
}
}
And here is the class that is supposed to implement everything
public class LList<AType> {
Links<AType> list;
LList() {
list = new Cons<>();
}
Links<AType> getList() {
return list;
}
AType get(int n, AType a) {
Cons<AType> aTypeCons = new Cons<>(a, list);
return null;
}
void add(AType elem) {
//add to head of list
}
void remove(int i) {
//remove ith element
//do nothing if i is invalid
}
void print() {
//prints the list
}
}
I just need some help figuring out where to actually start in making the LList class. I can't figure out the constructor because Links is abstract, so I can't make that an object, and I can't make a new Cons<> because there are no elements that are passed into the constructor. However, the constructor is supposed to instantiate a new list. I also can't figure out how I'm supposed to be able to access an individual element of that list. If I can just have a bit of understanding of what needs to happen in the constructor, I should be able to figure out how to implement the rest of the methods.
Your LList is a singly-linked list, where each element has a value and a link to the list that follows. The final element of the list will always be a Nil object, which represents an empty list. When you first initialize an empty list, you can just assign list = new Nil<>();. When you add an element to the list, you can reassign it as list = new Cons<>(elem, list);.
To access an an element in the list by index, just use a while loop that calls getNext() until it's either reached the desired index or found the end of the list.
AType get(int n) {
Links<AType> current = list;
while (n > 0 && current instanceof Cons) {
current = current.getNext();
n--;
}
return current.getElem();
}

How do I implement a list of lists which extends Comparable?

I try to make a generic linked list.
The node of this linked list using <T extends Comparable <T>>. But when I use
LList<LList<Integer>> linkedlist = new LList<LList<Integer>>();
to create a new instance, there is an error:
Multiple markers at this line
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type LList<T>
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type
How do I fix this?
Node class:
public class Node <T extends Comparable <T>> {
// Members:
public T data;
public Node <T> next;
// Methods:
public Node () {
data =null;
next = null;
}
public Node (T data) {
this.data = data;
next = null;
}
}
LList class:
public class LList <T extends Comparable <T>> {
// Members:
public Node <T> head;
// Methods:
public LList () {
head = null;
}
// Add node.
public void addNode (T data) {
if (head == null) {
head = new Node <T> (data);
return;
}
Node <T> newNode = new Node <T> (data);
Node <T> tempNode = head;
while (tempNode.next != null) tempNode = tempNode.next;
tempNode.next = newNode;
}
// Show linked list.
public void showLLForInteger () {
if (head == null) return;
Node <T> tempNode = head;
while (tempNode != null) {
System.out.print(String.format("%-6d", tempNode.data));
tempNode = tempNode.next;
}
System.out.println();
}
}
Why are you requiring that T extends Comparable<T>? You do not seem to be using that anywhere.
Since you require that T extends Comparable<T>, that means the parameter of the list must be comparable to itself. LList<LList<Integer>> doesn't work because LList<Integer> is not comparable to itself (it does not extend Comparable<LList<Integer>>). Are you sure you don't just want LList<Integer>?
LList<T extends Comparable>
so LList accepts only classes that extends Comparable as type parameter.
LList <LList<Integer>> subArrsList = new LList <LList<Integer>>();
in this statement you are giving LList<Integer> class as type parameter. LList<Integer> does not extend Comparable.
Integer extends comparable but you are not using the Integer class as type parameter, You are using LList which does not extends Comparable.
So you are getting an error.
Change your LList class as follows:
public class LList <T extends Comparable <T>> implements Comparable<LList<T>>{
#Override public int compareTo(LList<T> list) {
//to do
}
...// your code
}

comparable implementation in Java

I am new to Java and trying to implement MyLinkedList which extends the GeneralList interface, I want to use the comparable interface for my Node to keep my list sorted,
When I try to create a head node it is giving me errors
Please find the error message below the following code
//List interface
public interface GeneralList<T>
{
public boolean addNode(T elem);
public boolean deleteNode(T elem);
public T containsNode(T elem);
public void printSll();
}
//ListImplementation
public class SLL2<T> implements GeneralList<T>
{
private static class Node<T extends Comparable<T>>
{
public T data;
public Node<T> next;
public Node()
{
data = null;
next = null;
}
}
public SLL2()
{
head = null;
}
/* 1. Error while creating a head referance*/
private Node<T> head;
#Override
public boolean addNode(T elem)
{
Node<T> tmp = new Node<T>();
tmp.data = elem;
if(head == null)
{
head = temp;
return true;
}
else
{
for(Node<T> cur = head; cur.next != null ; cur= cur.next)
{
/* iterate and add the node */
if(temp.elem.comparTo(cur.elem))
{
}
}
}
}
1. I am not able to create the head node with the declaration private Node<T> head;
It is giving error "Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type SLL2<T>.Node<T>"
Please help me to resolve this error...
Your class SLL2<T> should also have a constraint about comparability of T. Like that:
public class SLL2<T extends Comparable<T>> implements GeneralList<T> {
// ...

error: method does not override or implement a method from a supertype

Building queue implementation based on linked list. Cannot run application because of the two errors:
public class Queue<Integer> implements Iterable<Integer> {
...
public Iterator<Integer> iterator() {
return new ListIterator(first);
}
private class ListIterator<Integer> implements Iterator<Integer> {// error #1
private Node<Integer> current;
public ListIterator(Node<Integer> first) {
current = first;
}
public boolean hasNext(){ return current != null; }
public void remove() { throw new UnsupportedOperationException();}
public int next() { // error #2
if (!hasNext()) throw new NoSuchElementException();
int item = current.item;
current = current.next;
return item;
}
}
}
error #1: error: Queue.ListIterator is not abstract and does not override abstract method next() in Iterator
where Integer is a type-variable:
Integer extends Object declared in class Queue.ListIterator
error #2: error: next() in Queue.ListIterator cannot implement next() in Iterator
return type int is not compatible with Integer
where E,Integer are type-variables:
E extends Object declared in interface Iterator
Integer extends Object declared in class Queue.ListIterator
How to get it working?
Boxing and unboxing in Java simplify code in many places, but method return types is not one of them. The next method must return an Integer, not an int. It must match the generic type parameter exactly.
public Integer next()
Second, you've declared a generic type parameter Integer in your Queue and ListIterator classes that has nothing to do with java.lang.Integer. Remove it:
// here
public class Queue implements Iterable<Integer> {
and
// here
private class ListIterator implements Iterator<Integer> {

How to compare generic nodes in a linked list using Comparable?

I am implementing a sorted list using linked lists. My node class looks like this
public class Node<E>{
E elem;
Node<E> next, previous;
}
In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error
"The method compareTo(E) is undefined for type E". I have tried implemnting the compareTo method in Node, but then I can't call any of object's methods, because E is generic type.
Here is the non-finished body of add(E elem) method.
public void add(E elem)
{
Node<E> temp = new Node<E>();
temp.elem = elem;
if( isEmpty() ) {
temp.next = head;
head.previous = temp;
head = temp;
counter++;
}else{
for(Node<E> cur = head; cur.next != null ; cur= cur.next) {
**if(temp.elem.comparTo(cur.elem)) {**
//do the sort;
}/*else{
cur.previous = temp;
}*/
}
//else insert at the end
}
}
Here is one of the object implemnting compareTo method
public class Patient implements Comparable<Patient>{
public int compareTo(Patient that)
{
return (this.getPriority() <= that.getPriority() ? 1 : 0 );
}
}
Bound E to Comparable:
public class Node<E extends Comparable<E>>{
E elem;
Node<E> next, previous;
}
It will compile now.
If you want the elements stored in your nodes to be comparable, you can state this using generics:
public class Node<E extends Comparable<E>> {
E elem;
Node<E> next, previous;
}
this way it is sure, that every E implements the Comparable interface, so you can safely call the compareTo method.
It seems that your generic E must be E extends Comparable<E>. This way you will get the access to the compareTo(E other) method. However, you will be unable to add elements that are not implementing this interface.
Try
public class Node<E extends Comparable<E>>{
E elem;
Node<E> next, previous;
}

Categories