The stack is a linear table that is advanced, allowing elements to be added at the top of the table and deleted at the top of the table.
How to construct the stack:
Public stack () {}
Push (): Adds an element at the top of the table and AddElement () is the same.
Pop (): Get and remove the first element at the top of a table
Peek (): Gets the first element at the top of a table but does not remove
A queue is a special linear table that only allows elements to be added at the back end of the table, deleting elements at the front of the table.
The LinkedList class implements the queue, so you can use LinkedList as a queue.
Offer (): Adds an element to the back end of the table.
Poll (): Gets and deletes the first element at the front of the table.
Peek (): Gets the first element at the front of the table.
Element (): Gets the first element at the front of the table.
Add (): Inserts the specified element at the tail of this queue. As the queue is unbounded, this method would never throw IllegalStateException or return false .
Offer (): Inserts the specified element in the tail of this queue. As the queue is unbounded, this method would never return false .
Difference: Both are inserting elements into the tail of the queue, different times, when the queue bounds are exceeded, the Add () method throws an exception for you to handle, and the offer () method returns false directly
The Add () and remove () methods throw exceptions when the operation fails, so it is not recommended.
Initial learning of the Java queue