简体   繁体   中英

Need some clarification of a Queue and a LinkedList implementation

This is my code:

/*
private Queue<Card> cards = new LinkedList<Card>();

*/

I am using a Queue and a LinkedList to process a deck of Black Jack playing cards.

Now, my understanding is that the queue holds the cards, and the LinkedList grants access to the Queue , correct?

If not, can someone give me a break-down of what the above code does?

You don't have a linked list and a queue. The class LinkedList<E> implements the interface Queue<E> . You are creating a linked list, but then using it as a queue.

Now, my understanding is that the queue holds the cards, and the linkedlist grants access to the queue, correct?

No. Queue is an interface (an abstraction, a way to define contract) while LinkedList is an implementation of that interface. In other words it works hard to fulfill that contract.

In this particular case Queue is a very restrictive interface that exposes only few possible operations of a LinkedList .

Real life example: bank is an institution with employees, treasury and offices. Internet is a way you access the bank, providing some interface and capabilities. When using a mobile application or speaking with a bank consultant via phone you are still using the same bank, but with a different interface.

Queue is interface that gives you possibility to interact with underlying object of LinkedList as with queue. So you cannot call method to get object by index and so on. So Queue cuts functionality of LinkedList that you don't require when want to use queue mechanism.

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