听着天籁,我是个音乐迷。时间充实着,会过得很快。我马上也可以到傍晚的时候去乐室吹我心爱的萨克斯。

 

                    ②泡茶看<数据结构>,喜欢看源码-栈ADT

                    嘟嘟嘟... 我会吹一首简单的歌咯,哈哈我想到了一个神奇的比喻,待会说。

栈ADT模型(又称LIFO表)

  栈(stack)插入和删除只能在一个位置上进行的表。该位置是表的末端但是叫做栈的顶(top)。基本操作:进栈(push相当于插入)和出栈(pop相当于删除)。又称LIFO表,后进先出。

 

      ②泡茶看<数据结构>,喜欢看源码-栈ADT相当于②泡茶看<数据结构>,喜欢看源码-栈ADT

                    就想快速呼吸一样。先吸进来的空气,先呼出去。

                     你是否记住了?

 

栈的源码和数组实现

 

  java.util.Stack

 

   不得不申明下,小朽研究不深,如果大家看到了希望能指点指点我。有些时候,说错了,我马上会改正的。谢谢。先介绍类的结构图

   ②泡茶看<数据结构>,喜欢看源码-栈ADT

 

    下面是源码 java.util.Stack

  1 /*
  2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
  3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4  *
  5  *
  6  *
  7  *
  8  *
  9  *
 10  *
 11  *
 12  *
 13  *
 14  *
 15  *
 16  *
 17  *
 18  *
 19  *
 20  *
 21  *
 22  *
 23  *
 24  */
 25 
 26 package java.util;
 27 
 28 /**
 29  * The <code>Stack</code> class represents a last-in-first-out
 30  * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
 31  * operations that allow a vector to be treated as a stack. The usual
 32  * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
 33  * method to <tt>peek</tt> at the top item on the stack, a method to test
 34  * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
 35  * the stack for an item and discover how far it is from the top.
 36  * <p>
 37  * When a stack is first created, it contains no items.
 38  *
 39  * <p>A more complete and consistent set of LIFO stack operations is
 40  * provided by the {@link Deque} interface and its implementations, which
 41  * should be used in preference to this class.  For example:
 42  * <pre>   {@code
 43  *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
 44  *
 45  * @author  Jonathan Payne
 46  * @since   JDK1.0
 47  */
 48 public
 49 class Stack<E> extends Vector<E> {
 50     /**
 51      * Creates an empty Stack.
 52      */
 53     public Stack() {
 54     }
 55 
 56     /**
 57      * Pushes an item onto the top of this stack. This has exactly
 58      * the same effect as:
 59      * <blockquote><pre>
 60      * addElement(item)</pre></blockquote>
 61      *
 62      * @param   item   the item to be pushed onto this stack.
 63      * @return  the <code>item</code> argument.
 64      * @see     java.util.Vector#addElement
 65      */
 66     public E push(E item) {
 67         addElement(item);
 68 
 69         return item;
 70     }
 71 
 72     /**
 73      * Removes the object at the top of this stack and returns that
 74      * object as the value of this function.
 75      *
 76      * @return  The object at the top of this stack (the last item
 77      *          of the <tt>Vector</tt> object).
 78      * @throws  EmptyStackException  if this stack is empty.
 79      */
 80     public synchronized E pop() {
 81         E       obj;
 82         int     len = size();
 83 
 84         obj = peek();
 85         removeElementAt(len - 1);
 86 
 87         return obj;
 88     }
 89 
 90     /**
 91      * Looks at the object at the top of this stack without removing it
 92      * from the stack.
 93      *
 94      * @return  the object at the top of this stack (the last item
 95      *          of the <tt>Vector</tt> object).
 96      * @throws  EmptyStackException  if this stack is empty.
 97      */
 98     public synchronized E peek() {
 99         int     len = size();
100 
101         if (len == 0)
102             throw new EmptyStackException();
103         return elementAt(len - 1);
104     }
105 
106     /**
107      * Tests if this stack is empty.
108      *
109      * @return  <code>true</code> if and only if this stack contains
110      *          no items; <code>false</code> otherwise.
111      */
112     public boolean empty() {
113         return size() == 0;
114     }
115 
116     /**
117      * Returns the 1-based position where an object is on this stack.
118      * If the object <tt>o</tt> occurs as an item in this stack, this
119      * method returns the distance from the top of the stack of the
120      * occurrence nearest the top of the stack; the topmost item on the
121      * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
122      * method is used to compare <tt>o</tt> to the
123      * items in this stack.
124      *
125      * @param   o   the desired object.
126      * @return  the 1-based position from the top of the stack where
127      *          the object is located; the return value <code>-1</code>
128      *          indicates that the object is not on the stack.
129      */
130     public synchronized int search(Object o) {
131         int i = lastIndexOf(o);
132 
133         if (i >= 0) {
134             return size() - i;
135         }
136         return -1;
137     }
138 
139     /** use serialVersionUID from JDK 1.0.2 for interoperability */
140     private static final long serialVersionUID = 1224463164541339165L;
141 }
java.util.Stack

相关文章:

  • 2022-12-23
  • 2021-09-24
  • 2021-08-21
  • 2021-10-25
  • 2020-04-12
  • 2021-12-04
  • 2022-02-01
  • 2022-02-28
猜你喜欢
  • 2021-06-27
  • 2022-01-26
  • 2021-05-22
  • 2021-09-09
  • 2022-12-23
  • 2021-11-21
  • 2021-05-28
相关资源
相似解决方案