Skip to content

Commit ac4bfa3

Browse files
committed
数据结构--队列
队列完整实现
1 parent c8e4e6e commit ac4bfa3

4 files changed

Lines changed: 111 additions & 3 deletions

File tree

DataStruct/Array/DynArray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace AlLib
2323
public:
2424
DynArray();
2525
DynArray(int nInitialSize_, const T& nInitialValue_);
26-
~DynArray();
26+
virtual ~DynArray();
2727

2828
void Add(const T& value_);
2929
void AddRange(const DynArray& arrItems_);

DataStruct/List/DoubleList.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace AlLib
5353
DoubleList(const DoubleList& list_);
5454
// 赋值
5555
DoubleList<T>& operator=(const DoubleList& list_);
56-
~DoubleList();
56+
virtual ~DoubleList();
5757

5858
// 判空
5959
bool IsEmpty() const;

DataStruct/Queue/DynQueue.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Author : XuBenHao
2+
// Version : 1.0.0
3+
// Mail : xbh370970843@163.com
4+
// Copyright : XuBenHao 2020 - 2030
5+
6+
#ifndef AILIB_DATASTRUCT_QUEUE_DYNQUEUE_H
7+
#define AILIB_DATASTRUCT_QUEUE_DYNQUEUE_H
8+
#include "..\..\stdafx.h"
9+
#include "..\List\DoubleList.h"
10+
11+
namespace AlLib
12+
{
13+
namespace DataStruct
14+
{
15+
namespace Queue
16+
{
17+
template <typename T>
18+
class DynQueue
19+
{
20+
public:
21+
DynQueue();
22+
virtual ~DynQueue();
23+
24+
DynQueue(const DynQueue& dqA_);
25+
DynQueue operator=(const DynQueue& dqA_);
26+
27+
void In(const T& nValue_);
28+
T Out();
29+
T Peek() const;
30+
bool IsEmpty() const;
31+
List::DoubleList<T> GetList() const
32+
{
33+
return m_List;
34+
}
35+
private:
36+
List::DoubleList<T> m_List;
37+
};
38+
39+
template <typename T>
40+
DynQueue<T>::DynQueue()
41+
{
42+
}
43+
44+
template <typename T>
45+
DynQueue<T>::~DynQueue()
46+
{
47+
}
48+
49+
template <typename T>
50+
DynQueue<T>::DynQueue(const DynQueue& dqA_)
51+
{
52+
m_List = dqA_.m_List;
53+
}
54+
55+
template <typename T>
56+
typename DynQueue<T> DynQueue<T>::operator=(const DynQueue& dqA_)
57+
{
58+
if (this == &dqA_)
59+
{
60+
return *this;
61+
}
62+
63+
m_List = dqA_.m_List;
64+
return *this;
65+
}
66+
67+
template <typename T>
68+
void DynQueue<T>::In(const T& nValue_)
69+
{
70+
m_List.InsertLast(nValue_);
71+
}
72+
73+
template <typename T>
74+
T DynQueue<T>::Out()
75+
{
76+
if (IsEmpty())
77+
{
78+
throw "queue is empty";
79+
}
80+
81+
List::DoubleList<T>::Node* _pFirst = m_List.GetFirst();
82+
T _nValue = _pFirst->GetValue();
83+
m_List.DeleteFirst();
84+
return _nValue;
85+
}
86+
87+
template <typename T>
88+
T DynQueue<T>::Peek() const
89+
{
90+
if (IsEmpty())
91+
{
92+
throw "queue is empty";
93+
}
94+
95+
List::DoubleList<T>::Node* _pFirst = m_List.GetFirst();
96+
return _pFirst->GetValue();
97+
}
98+
99+
template <typename T>
100+
bool DynQueue<T>::IsEmpty() const
101+
{
102+
return m_List.IsEmpty();
103+
}
104+
}
105+
}
106+
}
107+
#endif
108+

DataStruct/Stack/DynStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace AlLib
2121
DynStack();
2222
DynStack(const DynStack& sA_);
2323
DynStack operator=(const DynStack& sA_);
24-
~DynStack();
24+
virtual ~DynStack();
2525

2626
void Push(const T& nNewValue_);
2727
T Pop();

0 commit comments

Comments
 (0)