简体   繁体   中英

Java implementing Queue by extending LinkedList

I've been trying to research a way to implement a Queue using a LinkedList. I've mostly found examples showing me how to do it by literally using "implements" in the class. BUT What I'd like to do, however, is to extend the LinkedList class. For example I have written something like this:

public class TestQueue extends LinkedList{

    public TestQueue(){

    }

    public void enqueue(ObjectType c){

       add(c);
    }

    public Object dequeue(){
       return (ObjectType ) remove();
    }

    // more code for peek and size ect....


} 

Is this really all I have to do to use a linked-list type queue? How then would I have to set a head(front) and a tail(rear) to use the linked list just like a queue?

Thanks in advance.

From my understanding and looking up the LinkedList class, you should be good to go as the only thing you should need is the Queue class, which LinkedList already includes. I would recommend taking a quick look at these resources however just to make sure your understanding is where you want it to be.

API from Oracle - http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Learning about Queues & Staks (Document) - http://introcs.cs.princeton.edu/java/43stack/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM