Beispiel #1
0
        // 压栈
        public void Push(T data)
        {
            LinkStackNode <T> node = new LinkStackNode <T>(data);

            node.next = top;
            top       = node;

            length++;
        }
Beispiel #2
0
        // 清空
        public void Clear()
        {
            while (top != null)
            {
                LinkStackNode <T> node = top;
                top = top.next;
                node.Reset();
            }

            length = 0;
        }
Beispiel #3
0
        // 出栈
        public T Pop()
        {
            if (length <= 0)
            {
                LogError("Out of the range!");
                return(default(T));
            }

            LinkStackNode <T> node = top;

            top = top.next;

            T data = node.data;

            node.Reset();
            length--;

            return(data);
        }
Beispiel #4
0
        // 打印
        public void Print()
        {
            StringBuilder builder = new StringBuilder("[");

            LinkStackNode <T> node = top;

            while (node != null)
            {
                builder.Append(node.data);

                if (node.next != null)
                {
                    builder.Append(", ");
                }

                node = node.next;
            }

            builder.Append("]");

            Log(builder.ToString());
        }
Beispiel #5
0
 public LinkStack()
 {
     top    = null;
     length = 0;
 }
Beispiel #6
0
 public void Reset()
 {
     data = default(T);
     next = null;
 }