0

I quickly wrote a linked list class in Java. I want to write another queue class which uses the linked list. How would I achieve this in Java? I don't fully understand the implements / extends keywords... this is what my queue looks like ( for example):

public class Queue<T> implements LinkedList
{
    protected LinkedList<T> list;

    public Queue() {
        list = new LinkedList<T>();
    }

    public void add( T element) {
        list.add( element);
    }

    public T removeLast() {
        return list.removeLast();
    }   
}

Also note that the linked list class is also generic. I know there are already built in classes to achieve this functionality, but I wanted to learn ( which is why I am trying to do this manually)

EDIT: Additionally, in the end, I would like to be able to say something like this:

Queue<String> aQueue = new LinkedList<String>();
Tim
  • 73
  • 1
  • 2
  • 11
  • 2
    you know about generics but not about `implements/extends` ? Seriously ? – Radu Murzea May 06 '12 at 14:09
  • @SoboLAN I guess he's in learning phase: "I quickly wrote a linked list class in Java", not using the generic LinkedList. – Luiggi Mendoza May 06 '12 at 14:17
  • yes - see my post "I know there are already built in classes to achieve this functionality, but I wanted to learn ( which is why I am trying to do this manually)" – Tim May 06 '12 at 14:34

3 Answers3

6

If you want a behavior like Queue<String> aQueue = new LinkedList<String>(); then your LinkedList must extend/implement the Queue class/interface. Remember that a super class can be the object reference instance of a sub class, not viceversa.

public class LinkedList<T> implements Queue<T> {

    class Node<T> {
        T data;
        Node<T> next;
    }

    //all your behavior here
}

Also, as the Java documentation states, Queue is an interface and LinkedList implements it.

Note: If you want to implement a Queue using your LinkedList, you should see the code sample posted by @Tudor.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • hmm i think this is exactly what i am looking for.. going to test it out quickly, thanks. – Tim May 06 '12 at 14:07
  • so if i declare the queue as an interface. Eg: `public interface Queue{ // Methods}` those methods must not have a function body but must simply have the same name as the relevant method in the LinkedList class? – Tim May 06 '12 at 14:12
  • @Tim yes you're right. More info [here](http://stackoverflow.com/questions/2866987/what-is-the-definition-of-interface-in-object-oriented-programming) and [here](http://docstore.mik.ua/orelly/java-ent/jnut/ch03_07.htm) – Luiggi Mendoza May 06 '12 at 14:14
2

Two mistakes in your code:

  1. You are both implementing LinkedList (did you mean extend?) and using composition by having a LinkedList inside your class.

  2. This piece of code will not work: Queue<String> aQueue = new LinkedList<String>(); because according to point 1, Queue is either a subclass of LinkedList or contains a LinkedList, which makes your code incorrect.

In fact, the last code snippet makes very little sense. I assume that what you want to do is create a Queue that internally uses a linked list. In that case just use:

public class Queue<T>
{
    protected LinkedList<T> list;

    public Queue() {
        list = new LinkedList<T>();
    }

    public void add( T element) {
        list.add( element);
    }

    public T removeLast() {
        return list.removeLast();
    }   
}

And then:

Queue<String> queue = new Queue<String>();
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • ok, so what can i do to make `Queue aQueue = new LinkedList();` work? If I remove the `implements linkedList` from the queue class will this correct it? I am going to test it in the meanwhile... Ok i just tried it out... it says "incompatible types" ( which is why i put `implements..` in in the first place) – Tim May 06 '12 at 14:01
  • cool that solves the one problem... thanks :)... the reason i wanted to have that last snippet was because in my one program i create some linked lists with objects of another class... and i want to be able to copy that linked list over to a queue – Tim May 06 '12 at 14:05
  • @Tim: In the current form it's not possible. If you use the code I posted then there is no relationship between `LinkedList` and `Queue` so you cannot assign a `LinkedList` to a `Queue`. Furthermore, if you extend `LinkedList` it's still not correct because then `Queue` is a specialized linked list and so cannot appear to the left of the assignment. – Tudor May 06 '12 at 14:07
-1

Since Linkedlist implements queue interface we can use poll, peek methods directly....no need of extra code

Nagappa L M
  • 1,452
  • 4
  • 20
  • 33