The LinkedList bottom is realized by bidirectional linked list, which can be used to realize stack, queue, bidirectional queue
1. Structure of each node
private Static Class Node {
E item;
Node Next;
Node prev;
Node (node<e> prev, E element, node<e> next) {
This.item = element;
This.next = Next;
This.prev = prev;
}
-Note that we can add null to the LinkedList, when only the item equals NULL, and Next and Prev are still non-empty and do not affect the use
The 2.LinkedList class maintains the node of the both-A and the last, easy to operate
-first's prev is Null,last's next is null
3. With the one and last we can easily implement the stack, queue, two-way queue function
Add: public void AddFirst (e e);
public void AddLast (e e);
Delete: Public E removefirst ();
Public E removelast ();
Query: Public E peekfirst ();
Public E peeklast ();
4. Implement stack operation:
public void push (E e) {
AddFirst (e);
}
Public E pop () {
return Removefirst ();
}
Public E Peek () {
Final Node f = A;
return (f = null)? Null:f.item;
}
Note: size () will change after each pop, so you cannot traverse for (int i=0;i