类 LinkedStack<Item>
- java.lang.Object
-
- edu.princeton.cs.algs4.LinkedStack<Item>
-
- 所有已实现的接口:
java.lang.Iterable<Item>
public class LinkedStack<Item> extends java.lang.Object implements java.lang.Iterable<Item>TheLinkedStackclass represents a last-in-first-out (LIFO) stack of generic items. It supports the usual push and pop operations, along with methods for peeking at the top item, testing if the stack is empty, and iterating through the items in LIFO order.This implementation uses a singly linked list with a non-static nested class for linked-list nodes. See
Stackfor a version that uses a static nested class. The push, pop, peek, size, and is-empty operations all take constant time in the worst case.For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
构造器概要
构造器 构造器 说明 LinkedStack()Initializes an empty stack.
-
方法概要
修饰符和类型 方法 说明 booleanisEmpty()Is this stack empty?java.util.Iterator<Item>iterator()Returns an iterator to this stack that iterates through the items in LIFO order.static voidmain(java.lang.String[] args)Unit tests theLinkedStackdata type.Itempeek()Returns (but does not remove) the item most recently added to this stack.Itempop()Removes and returns the item most recently added to this stack.voidpush(Item item)Adds the item to this stack.intsize()Returns the number of items in the stack.java.lang.StringtoString()Returns a string representation of this stack.
-
-
-
方法详细资料
-
isEmpty
public boolean isEmpty()
Is this stack empty?- 返回:
- true if this stack is empty; false otherwise
-
size
public int size()
Returns the number of items in the stack.- 返回:
- the number of items in the stack
-
push
public void push(Item item)
Adds the item to this stack.- 参数:
item- the item to add
-
pop
public Item pop()
Removes and returns the item most recently added to this stack.- 返回:
- the item most recently added
- 抛出:
java.util.NoSuchElementException- if this stack is empty
-
peek
public Item peek()
Returns (but does not remove) the item most recently added to this stack.- 返回:
- the item most recently added to this stack
- 抛出:
java.util.NoSuchElementException- if this stack is empty
-
toString
public java.lang.String toString()
Returns a string representation of this stack.- 覆盖:
toString在类中java.lang.Object- 返回:
- the sequence of items in the stack in LIFO order, separated by spaces
-
iterator
public java.util.Iterator<Item> iterator()
Returns an iterator to this stack that iterates through the items in LIFO order.- 指定者:
iterator在接口中java.lang.Iterable<Item>- 返回:
- an iterator to this stack that iterates through the items in LIFO order.
-
main
public static void main(java.lang.String[] args)
Unit tests theLinkedStackdata type.- 参数:
args- the command-line arguments
-
-