如果我们使用Linkedlist部署Stack,stack.push()和stack.addFirst()有区别吗?
If we use Linkedlist to deploy the Stack, is there are differernce between stack.push() and stack.addFirst()?
现在我使用 LinkedList 将 Stack 部署为:
Deque<Integer> stack = new LinkedList();
如果我想模拟 Push 操作我会做:
stack.addFirst(a);
由于我用的是链表,元素4是栈顶,1是栈底
现在,在这种情况下,stack.push 和 stack.addFirst() 之间有什么区别?
他们会有相同的行为吗??
根据 documentation:
Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack
class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent to Deque
methods as indicated in the table below:
Stack Method
Equivalent Deque Method
push(e)
addFirst(e)
pop()
removeFirst()
peek()
peekFirst()
现在我使用 LinkedList 将 Stack 部署为:
Deque<Integer> stack = new LinkedList();
如果我想模拟 Push 操作我会做:
stack.addFirst(a);
由于我用的是链表,元素4是栈顶,1是栈底
现在,在这种情况下,stack.push 和 stack.addFirst() 之间有什么区别?
他们会有相同的行为吗??
根据 documentation:
Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy
Stack
class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent toDeque
methods as indicated in the table below:
Stack Method Equivalent Deque Method push(e) addFirst(e) pop() removeFirst() peek() peekFirst()