[关闭]
@nalan90 2018-05-04T11:37:00.000000Z 字数 995 阅读 613

数据结构之栈

数据结构


特性

接口列表

示例代码
  1. public interface Stack<E> {
  2. public int getSize();
  3. public boolean isEmpty();
  4. public void push(E e);
  5. public E pop();
  6. public E peek();
  7. }
  1. public class ArrayStack<E> implements Stack<E> {
  2. Array<E> array;
  3. public ArrayStack(int capacity) {
  4. this.array = new Array<>(capacity);
  5. }
  6. public ArrayStack() {
  7. this.array = new Array<>();
  8. }
  9. @Override
  10. public int getSize() {
  11. return this.array.getSize();
  12. }
  13. @Override
  14. public boolean isEmpty() {
  15. return this.array.isEmpty();
  16. }
  17. public int getCapacity() {
  18. return this.array.getCapacity();
  19. }
  20. @Override
  21. public void push(E e) {
  22. this.array.addLast(e);
  23. }
  24. @Override
  25. public E pop() {
  26. return this.array.removeLast();
  27. }
  28. @Override
  29. public E peek() {
  30. return this.array.getLast();
  31. }
  32. @Override
  33. public String toString() {
  34. StringBuilder res = new StringBuilder();
  35. res.append("Stack: ");
  36. res.append('[');
  37. for (int i = 0; i < this.array.getSize(); i++) {
  38. res.append(array.get(i));
  39. if (i != this.array.getSize() - 1) {
  40. res.append(", ");
  41. }
  42. }
  43. res.append("] top");
  44. return res.toString();
  45. }
  46. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注