1.数据结构之队列
数据结构中的队列所遵循的原则是先进先出的排队原则,即先存放的先先取出。同时队列分为普通队列和环形队列,其中普通队列是非闭合队列,效率和利用率低;相反,环形队列为首尾闭合的队列,可以实现不断取出又不断放进,效率和利用率高。
队列有列首、列尾、列容量、列长度等名词,在此不解释了。
接下来放代码
Customer.h中的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;
class Customer
{
public:
Customer(string name = "", int age = 0);
void printInfo() const;
private:
string m_strName;
int m_iAge;
};
Customer.cpp中的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
Customer::Customer(string name, int age)
{
m_strName = name;
m_iAge = age;
}
void Customer::printInfo() const
{
cout << "姓名" << m_strName << endl;
cout << "年龄" << m_iAge << endl;
}
MyQueue.cpp中的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using namespace std;
MyQueue::MyQueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new Customer[m_iQueueCapacity];
ClearQueue();
}
MyQueue::~MyQueue()
{
delete []m_pQueue;
m_pQueue = NULL;
}
void MyQueue::ClearQueue()
{
m_iHead = 0;
m_iTail = 0;
m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
if (m_iQueueLen == 0)
{
return true;
}
else
{
return false;
}
}
int MyQueue::QueueLength() const
{
return m_iQueueLen;
}
bool MyQueue::QueueFull() const
{
if (m_iQueueLen == m_iQueueCapacity)
{
return true;
}
return false;
}
bool MyQueue::EnQueue(Customer element)
{
if (QueueFull())
{
return false;
}
else
{
m_pQueue[m_iTail] = element;
m_iTail++;
m_iTail = m_iTail % m_iQueueCapacity;
m_iQueueLen++;
return true;
}
}
bool MyQueue::DeQueue(Customer &element)
{
if (QueueEmpty())
{
return false;
}
else
{
element = m_pQueue[m_iHead];
m_iHead++;
m_iHead = m_iHead % m_iQueueCapacity;
m_iQueueLen--;
return true;
}
}
void MyQueue::QueueTraverse()
{
for (int i = m_iHead; i < m_iQueueLen + m_iHead; i++)
{
m_pQueue[i%m_iQueueCapacity].printInfo();
cout << "前面还有" << (i-m_iHead) << "人" << endl;
cout << endl;
}
cout << endl;
}
MyQueue.h中的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyQueue
{
public:
MyQueue(int queueCapacity); //创建队列
virtual ~MyQueue(); //销毁队列
void ClearQueue(); //清空队列
bool QueueEmpty() const; //判空队列
bool QueueFull() const; //判满队列
int QueueLength() const; //队列长度
bool EnQueue(Customer element); //新元素入队
bool DeQueue(Customer &element); //首元素出队
void QueueTraverse(); //遍历队列
private:
Customer *m_pQueue; //队列数组指针
int m_iQueueLen; //队列元素个数
int m_iQueueCapacity; //队列数组容量
int m_iHead;
int m_iTail;
};
demo.cpp中的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace std;
int main(void)
{
MyQueue *p = new MyQueue(4);
Customer c1("张三",20);
Customer c2("李四",30);
Customer c3("王五",25);
p->EnQueue(c1);
p->EnQueue(c2);
p->EnQueue(c3);
p->QueueTraverse();
Customer c4("",0);
p->DeQueue(c4);
c4.printInfo();
system("pause");
return 0;
}
2.数据结构之栈
讲真,假期学习真的好难,我好不容易静下心来再学,其实之前已经学了,只是博客没做记录,今天补上。栈是很简单的模型,就是相当于一个瓶子,放东西进去,先放的在下面,后放的在上面,所以遵循后进先出原则,栈的栈底始终不变,栈顶随放入数量增加而上升。差不多就这些,今天的代码很简单,遵循我写代码不麻烦,理解容易原则
接下来放代码
MyStack.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyStack
{
public:
MyStack(int size); //分配内存初始化栈空间,设定栈容量,栈顶
~MyStack(); //回收栈空间内存
bool stackEmpty(); //判断栈是否为空,为空返回true,非空返回false
bool stackFull(); //判断栈是否为满,为满返回true,非满返回false
void clearStack(); //清空栈
int stackLength(); //已有元素个数
bool push(char elem); //元素入栈,栈顶上升
bool pop(char &elem); //元素出栈,栈顶下降
void stackTraverse(bool isFromButtorm); //遍历栈中所有元素
private:
char *m_pBuffer; //栈空间指针
int m_iSize; //栈容量
int m_iTop; //栈顶,栈中元素个数
};
MyStack.cpp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using namespace std;
MyStack::MyStack(int size)
{
m_iSize = size;
m_pBuffer = new char[size];
m_iTop = 0;
}
MyStack::~MyStack()
{
delete []m_pBuffer;
}
bool MyStack::stackEmpty()
{
if (0 == m_iTop) //此处为了优秀代码,也可写成 m_iTop == 0;
{
return true;
}
return false;
}
bool MyStack::stackFull()
{
if (m_iTop == m_iSize)
{
return true;
}
return false;
}
void MyStack::clearStack()
{
m_iTop = 0;
}
int MyStack::stackLength()
{
return m_iTop;
}
bool MyStack::push(char elem)
{
if (stackFull())
{
return false;
}
m_pBuffer[m_iTop] = elem;
m_iTop++;
return true;
}
bool MyStack::pop(char &elem)
{
if (stackEmpty())
{
return false;
}
m_iTop--;
elem = m_pBuffer[m_iTop];
return true;
}
void MyStack::stackTraverse(bool isFromButtorm)
{
if (isFromButtorm)
{
for (int i = 0; i < m_iTop; i++)
{
cout << m_pBuffer[i] << ",";
}
}
else
{
for (int i = m_iTop-1; i >= 0; i--)
{
cout << m_pBuffer[i] << ",";
}
}
}
demo.cpp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using namespace std;
int main(void)
{
MyStack *pStack = new MyStack(5);
pStack->push('h');
pStack->push('e');
pStack->push('l');
pStack->push('l');
pStack->push('o');
pStack->stackTraverse(true);
char elem = 0;
pStack->pop(elem);
pStack->stackTraverse(true);
//pStack->clearStack();
cout << pStack->stackLength() << endl;
if (pStack->stackEmpty())
{
cout << "栈为空" << endl;
}
if (pStack->stackFull())
{
cout << "栈为满" << endl;
}
delete pStack;
pStack = NULL;
system("pause");
return 0;
}