实战之数据结构篇(线性表,堆栈与队列)

1:定义

    线性表(Linear List)是具有相同特性的数据元素的一个有限序列。该序列中所含元素的个数称作线性表的长度。

    堆实际上指的就是(满足堆性质的)优先队列的一种数据结构,第1个元素有最高的优先权

    栈实际上就是满足先进后出的性质的数学或数据结构。

    (虽然堆栈,堆栈的说法是连起来叫,但是他们还是有很大区别的,连着叫只是由于历史的原因。)

    队列 是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。(队列具有先进先出(FIFO)的特点)

2:实现 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1ContractedBlock.gifExpandedBlockStart.gif    定义结点#region 定义结点
  2    public class ListNode
  3ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  4        public ListNode(int NewValue)
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        {
  6            Value = NewValue;
  7        }

  8
  9        // 前一个
 10        public ListNode Previous;
 11
 12        // 后一个
 13        public ListNode Next;
 14
 15        // 值
 16        public int Value;
 17    }

 18   #endregion

 19
 20ContractedBlock.gifExpandedBlockStart.gif    定义链表类#region 定义链表类
 21    //定义结点之后,开始类线性表的操作编程了.在LIST 类中,采用了,Head ,Tail,  Current,三个指针,使用Append ,MoveFrist,MovePrevious,MoveNext,MoveLast ,Delete,InsertAscending,InsertUnAscending ,Clear 实现移动,添加,删除,升序插入,降序插入,清空链表操作,GetCurrentValue() 方法取得当前的值。
 22    public class Clist
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 24        // 头指针
 25        private ListNode Head;
 26        // 尾指针
 27        private ListNode Tail;
 28        // 当前指针
 29        private ListNode Current;
 30        // 链表数据的个数
 31        private int ListCountValue;
 32
 33        public Clist()
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 35            ListCountValue = 0;
 36            Head = null;
 37            Tail = null;
 38        }

 39
 40        // 尾部添加数据
 41        public void Append(int DataValue)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 43            ListNode NewNode = new ListNode(DataValue);
 44            if (IsNull())
 45            //如果头指针为空
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 47                Head = NewNode;
 48                Tail = NewNode;
 49            }

 50            else
 51ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 52                Tail.Next = NewNode;
 53                NewNode.Previous = Tail;
 54                Tail = NewNode;
 55            }

 56            Current = NewNode;
 57            //链表数据个数加一
 58            ListCountValue += 1;
 59        }

 60
 61        // 删除当前的数据
 62        public void Delete()
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 64            //若为空链表
 65            if (!IsNull())
 66ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 67                //若删除头
 68                if (IsBof())
 69ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 70                    Head = Current.Next;
 71                    Current = Head;
 72                    ListCountValue -= 1;
 73                    return;
 74                }

 75                //若删除尾
 76                if (IsEof())
 77ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 78                    Tail = Current.Previous;
 79                    Current = Tail;
 80                    ListCountValue -= 1;
 81                    return;
 82                }

 83                //若删除中间数据
 84                Current.Previous.Next = Current.Next;
 85                Current = Current.Previous;
 86                ListCountValue -= 1;
 87                return;
 88            }

 89
 90        }

 91        // 向后移动一个数据
 92        public void MoveNext()
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 94            if (!IsEof()) Current = Current.Next;
 95        }

 96        // 向前移动一个数据
 97        public void MovePrevious()
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 99            if (!IsBof()) Current = Current.Previous;
100        }

101        // 移动到第一个数据
102        public void MoveFrist()
103ExpandedSubBlockStart.gifContractedSubBlock.gif        {
104            Current = Head;
105        }

106        // 移动到最后一个数据
107        public void MoveLast()
108ExpandedSubBlockStart.gifContractedSubBlock.gif        {
109            Current = Tail;
110        }

111        // 判断是否为空链表
112        public bool IsNull()
113ExpandedSubBlockStart.gifContractedSubBlock.gif        {
114            if (ListCountValue == 0)
115                return true;
116            return false;
117        }

118        // 判断是否为到达尾部
119        public bool IsEof()
120ExpandedSubBlockStart.gifContractedSubBlock.gif        {
121            if (Current == Tail)
122                return true;
123            return false;
124        }

125        // 判断是否为到达头部
126        public bool IsBof()
127ExpandedSubBlockStart.gifContractedSubBlock.gif        {
128            if (Current == Head)
129                return true;
130            return false;
131        }

132        public int GetCurrentValue()
133ExpandedSubBlockStart.gifContractedSubBlock.gif        {
134            return Current.Value;
135        }

136        // 取得链表的数据个数
137        public int ListCount
138ExpandedSubBlockStart.gifContractedSubBlock.gif        {
139ExpandedSubBlockStart.gifContractedSubBlock.gif            get return ListCountValue; }
140        }

141
142        // 清空链表
143        public void Clear()
144ExpandedSubBlockStart.gifContractedSubBlock.gif        {
145            MoveFrist();
146            while (!IsNull())
147ExpandedSubBlockStart.gifContractedSubBlock.gif            {
148                //若不为空链表,从尾部删除
149                Delete();
150            }

151        }

152        // 在当前位置前插入数据
153        public void Insert(int DataValue)
154ExpandedSubBlockStart.gifContractedSubBlock.gif        {
155            ListNode NewNode = new ListNode(DataValue);
156            if (IsNull())
157ExpandedSubBlockStart.gifContractedSubBlock.gif            {
158                //为空表,则添加
159                Append(DataValue);
160                return;
161            }

162            if (IsBof())
163ExpandedSubBlockStart.gifContractedSubBlock.gif            {
164                //为头部插入
165                NewNode.Next = Head;
166                Head.Previous = NewNode;
167                Head = NewNode;
168                Current = Head;
169                ListCountValue += 1;
170                return;
171            }

172            //中间插入
173            NewNode.Next = Current;
174            NewNode.Previous = Current.Previous;
175            Current.Previous.Next = NewNode;
176            Current.Previous = NewNode;
177            Current = NewNode;
178            ListCountValue += 1;
179        }

180
181        // 进行升序插入
182        public void InsertAscending(int InsertValue)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        {
184            //参数:InsertValue 插入的数据
185            //为空链表
186            if (IsNull())
187ExpandedSubBlockStart.gifContractedSubBlock.gif            {
188                //添加
189                Append(InsertValue);
190                return;
191            }

192            //移动到头
193            MoveFrist();
194            if ((InsertValue < GetCurrentValue()))
195ExpandedSubBlockStart.gifContractedSubBlock.gif            {
196                //满足条件,则插入,退出
197                Insert(InsertValue);
198                return;
199            }

200            while (true)
201ExpandedSubBlockStart.gifContractedSubBlock.gif            {
202                if (InsertValue < GetCurrentValue())
203ExpandedSubBlockStart.gifContractedSubBlock.gif                {
204                    //满族条件,则插入,退出
205                    Insert(InsertValue);
206                    break;
207                }

208                if (IsEof())
209ExpandedSubBlockStart.gifContractedSubBlock.gif                {
210                    //尾部添加
211                    Append(InsertValue);
212                    break;
213                }

214                //移动到下一个指针
215                MoveNext();
216            }

217        }

218        // 进行降序插入
219        public void InsertUnAscending(int InsertValue)
220ExpandedSubBlockStart.gifContractedSubBlock.gif        {
221            //参数:InsertValue 插入的数据
222            //为空链表
223            if (IsNull())
224ExpandedSubBlockStart.gifContractedSubBlock.gif            {
225                //添加
226                Append(InsertValue);
227                return;
228            }

229            //移动到头
230            MoveFrist();
231            if (InsertValue > GetCurrentValue())
232ExpandedSubBlockStart.gifContractedSubBlock.gif            {
233                //满足条件,则插入,退出
234                Insert(InsertValue);
235                return;
236            }

237            while (true)
238ExpandedSubBlockStart.gifContractedSubBlock.gif            {
239                if (InsertValue > GetCurrentValue())
240ExpandedSubBlockStart.gifContractedSubBlock.gif                {
241                    //满族条件,则插入,退出
242                    Insert(InsertValue);
243                    break;
244                }

245                if (IsEof())
246ExpandedSubBlockStart.gifContractedSubBlock.gif                {
247                    //尾部添加
248                    Append(InsertValue);
249                    break;
250                }

251                //移动到下一个指针
252                MoveNext();
253            }

254        }

255    }

256    #endregion

257
258ContractedBlock.gifExpandedBlockStart.gif    堆栈类#region 堆栈类
259    public class CStack
260ExpandedSubBlockStart.gifContractedSubBlock.gif    {
261        //调用链表类
262        private Clist m_List;
263
264        public CStack()
265ExpandedSubBlockStart.gifContractedSubBlock.gif        {
266            //构造函数
267            m_List = new Clist();
268        }

269
270        // 压入堆栈
271        public void Push(int PushValue)
272ExpandedSubBlockStart.gifContractedSubBlock.gif        {
273           //参数: int PushValue 压入堆栈的数据
274            m_List.Append(PushValue);
275        }

276
277        // 弹出堆栈数据,如果为空,则取得 2147483647 为 int 的最大值;
278        public int Pop()
279ExpandedSubBlockStart.gifContractedSubBlock.gif        {
280            //功能:弹出堆栈数据 
281            int PopValue;
282            if (!IsNullStack())
283ExpandedSubBlockStart.gifContractedSubBlock.gif            {
284                //不为空堆栈
285                //移动到顶
286                MoveTop();
287                //取得弹出的数据
288                PopValue = GetCurrentValue();
289                //删除
290                Delete();
291                return PopValue;
292            }

293            //  空的时候为 int 类型的最大值
294            return 2147483647;
295        }

296
297        // 判断是否为空的堆栈
298        public bool IsNullStack()
299ExpandedSubBlockStart.gifContractedSubBlock.gif        {
300            if (m_List.IsNull())
301                return true;
302            return false;
303        }

304
305        // 堆栈的个数
306        public int StackListCount
307ExpandedSubBlockStart.gifContractedSubBlock.gif        {
308ExpandedSubBlockStart.gifContractedSubBlock.gif            get{return m_List.ListCount;}
309        }

310        
311        // 移动到堆栈的底部
312        public void MoveBottom()
313ExpandedSubBlockStart.gifContractedSubBlock.gif        {
314            m_List.MoveFrist();
315        }

316        
317        // 移动到堆栈的Top
318        public void MoveTop()
319ExpandedSubBlockStart.gifContractedSubBlock.gif        {
320            m_List.MoveLast();
321        }

322
323        // 向上移动
324        public void MoveUp()
325ExpandedSubBlockStart.gifContractedSubBlock.gif        {
326            m_List.MoveNext();
327        }

328        
329        // 向下移动
330        public void MoveDown()
331ExpandedSubBlockStart.gifContractedSubBlock.gif        {
332            m_List.MovePrevious();
333        }

334        
335        // 取得当前的值
336        public int GetCurrentValue()
337ExpandedSubBlockStart.gifContractedSubBlock.gif        {
338            return m_List.GetCurrentValue();
339        }

340        
341        // 删除取得当前的结点
342        public void Delete()
343ExpandedSubBlockStart.gifContractedSubBlock.gif        {
344            m_List.Delete();
345        }

346        
347        // 清空堆栈
348        public void Clear()
349ExpandedSubBlockStart.gifContractedSubBlock.gif        {
350            m_List.Clear();
351        }

352    }

353    #endregion

354
355ContractedBlock.gifExpandedBlockStart.gif    队列类#region 队列类
356    public class CQueue
357ExpandedSubBlockStart.gifContractedSubBlock.gif    {
358        private Clist m_List;
359        public CQueue()
360ExpandedSubBlockStart.gifContractedSubBlock.gif        {
361            //构造函数
362            //这里使用到前面编写的List 
363            m_List = new Clist();
364        }

365
366        // 入队
367        public void EnQueue(int DataValue)
368ExpandedSubBlockStart.gifContractedSubBlock.gif        {
369            //功能:加入队列,这里使用List 类的Append 方法:
370            //尾部添加数据,数据个数加1
371            m_List.Append(DataValue);
372        }

373
374        // 出队
375        public int DeQueue()
376ExpandedSubBlockStart.gifContractedSubBlock.gif        {
377            //功  能:出队
378            //返回值: 2147483647 表示为空队列无返回
379            int QueValue;
380            if (!IsNull())
381ExpandedSubBlockStart.gifContractedSubBlock.gif            {
382                //不为空的队列
383                //移动到队列的头
384                m_List.MoveFrist();
385                //取得当前的值
386                QueValue = m_List.GetCurrentValue();
387                //删除出队的数据
388                m_List.Delete();
389                return QueValue;
390            }

391            return 2147483647;
392        }

393
394        // 判断队列是否为空
395        public bool IsNull()
396ExpandedSubBlockStart.gifContractedSubBlock.gif        {
397            //功能:判断是否为空的队列
398            return m_List.IsNull();
399        }

400
401        // 清空队列
402        public void Clear()
403ExpandedSubBlockStart.gifContractedSubBlock.gif        {
404            //清空链表
405            m_List.Clear();
406        }

407
408        // 取得队列的数据个数
409        public int QueueCount
410ExpandedSubBlockStart.gifContractedSubBlock.gif        {
411            get
412ExpandedSubBlockStart.gifContractedSubBlock.gif            {
413                //取得队列的个数
414                return m_List.ListCount;
415            }

416        }

417    }

418    #endregion

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值