I'm supposed to implement a queue using a singly linked circular linked list (it also has to be generic). It keeps saying there are incompatible types at lines 44 and 45. I've honestly been trying to work this out for hours, but I don't understand what i'm doing wrong. Any help would greatly be appreciated.

import java.util.NoSuchElementException;
public class Queue<T>
{

   private int manyItems; //the number of items in the queue
   private ListNode<T> rear;
 
   public Queue()
   {	
   		rear=null;
    	manyItems=0;
    	
   }

   public T getFront() //used to remove the front item and return the item which was deleted
   {
      if(manyItems==0)
      {
      	throw new NoSuchElementException("Queue underflow");
      }
     else
     {	
      	T temp;
      	ListNode<T>q=rear.link;
      	temp=q.data;
      	rear.link=rear.link.link;
      	--manyItems;
      	return temp;
     }
      
   }
   public <T> void insert(T item)
   {
   		if(manyItems==0)
   		{	
   		   ListNode <T> rear=new ListNode<T>();
   		   rear.data=item;
   		   rear.link=rear;
       		++manyItems;
   		}
   		else
   		{
   			ListNode <T>q=new ListNode<T>();
   			q.link=rear.link;
   			rear.link=q;
   			q.data=item;
   			rear=rear.link;
       		++manyItems;
   		}
       
   }
              
   public boolean isEmpty( )
   {
  		if(manyItems==0)
  		{
  			return true;
  		}
  		else
  		{
  			return false;
  		}
  
   }

  /* private int nextIndex(int i)
   // Precondition: 0 <= i and i < data.length
   // Postcondition: If i+1 is data.length, 
   // then the return value is zero; otherwise
   // the return value is i+1.
   {
		if(i<data.length-1)
		{
			return i+1;
		}
		else
		{
			return 0;
		}
      
   }
*/
   public int size( )   
   {
   		return manyItems;
    
   }
}
public class ListNode <T>
{
	public T data;
	public ListNode<T> link;
}

One potential problem that I'm seeing - I don't know that this is related to your error - is that you've got rear declared twice. This may be causing some confusion - the rear that exists between 36-39 doesn't, I think, exist anywhere else. (it expires at the curly brace, and you haven't attached it anywhere... to the bin with it!)

Well, the problem is because those are two different T's :) One is the class T, and the other one is the insert method's T. Let me explain - when you used the syntax

public <T> void insert(T item)

You actually declared a generic method that uses the parameter T - but not the same T as the class. Any class, generic or not, can have generic methods, declared by using the same syntax

public <T> returnType methodName(<parameters that can use T>)

You wanted to use the same T as the class, you needed to write

public void insert(T item)

thank you guys so much! sorry for making such dumb errors, I'm just sick of looking at this. It builds now, so hopefully all is well :)

You are very welcome, and it's not a dumb error at all - generics can be extremely confusing.

Please mark the thread as solved.

Edited 6 Years Ago by apines: n/a

If you're not confused at some point, you're probably not learning anything. If you don't make "dumb mistakes", likewise. They're only "dumb mistakes" because you know more now than you did a little while ago.

This article has been dead for over six months. Start a new discussion instead.