数据结构 -- 链式栈

栈:先进后出的数据结构。

C#版本:

  1 namespace LinkStack
  2 {
  3     /// <summary></summary>
  4     interface IStack<T>
  5     {
  6         /// <summary>推入元素操作</summary>
  7         bool Push(T _data);
  8         /// <summary>弹出元素操作</summary>
  9         bool Pop(ref T _data);
 10         /// <summary>取栈顶元素</summary>
 11         bool GetTop(ref T _data);
 12         /// <summary>获取队列长度</summary>
 13         int StackLenght();
 14         /// <summary>是否为空</summary>
 15         bool IsEmpty();
 16         /// <summary>清空队列</summary>
 17         void Clear();
 18         /// <summary>是否满</summary>
 19         bool IsFull();
 20     }
 21 
 22     /// <summary>栈节点类</summary>
 23     class StackNode<T>
 24     {
 25         #region 私有属性
 26         private T data; //数据项
 27         private StackNode<T> next; //指针项
 28         #endregion
 29         #region 公有属性
 30         public T Data
 31         {
 32             get
 33             {
 34                 return data;
 35             }
 36             set
 37             {
 38                 data = value;
 39             }
 40         }
 41         public StackNode<T> Next
 42         {
 43             get
 44             {
 45                 return next;
 46             }
 47             set
 48             {
 49                 next = value;
 50             }
 51         }
 52         #endregion
 53 
 54         #region 构造函数
 55         public StackNode()
 56         {
 57             Data = default(T);
 58             Next = null;
 59         }
 60         public StackNode(T _data = default(T), StackNode<T> _next = null)
 61         {
 62             Data = _data;
 63             Next = _next;
 64         }
 65         #endregion
 66     }
 67 
 68     /// <summary>链式栈类</summary>
 69     class LinkStack<T>: IStack<T>
 70     {
 71         #region 成员变量
 72         private StackNode<T> top; //栈顶指示器
 73         private int nodeNum;
 74         #endregion
 75 
 76         #region 访问函数
 77         public LinkStack()
 78         {
 79             top = null;
 80             nodeNum = 0;
 81         }
 82 
 83         /// <summary>在栈顶推入一个新的元素</summary>
 84         public bool Push(T _data)
 85         {
 86             if (IsFull())
 87                 return false;
 88             StackNode<T> q = new StackNode<T>(_data);
 89             if (top == null)
 90             {
 91                 top = q;
 92             }
 93             else
 94             {
 95                 q.Next = top;
 96                 top = q;
 97             }
 98             ++nodeNum;
 99             return true;
100         }
101 
102         /// <summary>弹出栈顶元素值!值保存在_data中</summary>
103         public bool Pop(ref T _data)
104         {
105             if (IsEmpty())
106                 return false;
107 
108             _data = top.Data;
109             top = top.Next;
110             --nodeNum;
111 
112             return true;
113         }
114 
115         /// <summary>取栈顶元素值!值保存在_data中</summary>
116         public bool GetTop(ref T _data)
117         {
118             if (IsEmpty())
119                 return false;
120             _data = top.Data;
121 
122             return true;
123         }
124 
125         /// <summary>清空栈</summary>
126         public void Clear()
127         {
128             top = null;
129             nodeNum = 0;
130         }
131 
132         /// <summary>栈是否为空</summary>
133         public bool IsEmpty()
134         {
135             if (top == null && nodeNum == 0)
136                 return true;
137             return false;
138         }
139 
140         /// <summary>求栈元素个数操作</summary>
141         public int StackLenght()
142         {
143             return nodeNum;
144         }
145 
146         /// <summary>链式栈不会满的</summary>
147         public bool IsFull()
148         {
149             return false;
150         }
151 
152         #endregion
153 
154     }
155 
156 }

 

posted @ 2018-01-15 14:51  欧晨曦  阅读(174)  评论(0编辑  收藏  举报