Ejemplo n.º 1
0
        // 压栈
        public void Push(T data)
        {
            LinkStackNode <T> node = new LinkStackNode <T>(data);

            node.next = top;
            top       = node;

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

            length = 0;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 5
0
 public LinkStack()
 {
     top    = null;
     length = 0;
 }
Ejemplo n.º 6
0
 public void Reset()
 {
     data = default(T);
     next = null;
 }