队列



  
  
c++实现环形队列记录。  
  
  
  

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
//环形队列C++实现

#ifndef MYQUEUE_H
#define MYQUEUE_H

class MyQueue
{
public:
MyQueue(int queueCapacity); //InitQueue(&Q) 创建队列
virtual ~MyQueue(); //DestroyQueue(&Q) 销毁队列
void ClearQueue(); //ClearQueue(&Q) 清空队列
bool QueueEmpty() const; //QueueEmpty(Q) 判空队列
bool QueueFull() const; //QueueFull(Q) 判满队列
int QueueLength() const; //QueueLength(Q) 队列长度
bool EnQueue(int element); //EnQueue(&Q, element) 新元素入队
bool DeQueue(int &element); //DeQueue(&Q, &element) 首元素出队
void QueueTraverse(); //QueueTraverse(Q, visit()) 遍历队列
private:
int *m_pQueue; //队列数组指针
int m_iQueueLen; //队列元素个数
int m_iQueueCapacity; //队列数组容量
int m_iHead;
int m_iTail;
};

#endif

注释为C语言版。

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
93
#include "MyQueue.h"
#include <iostream>

using namespace std;

MyQueue::MyQueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new int[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;
}
// return m_iQueueLen == 0 ? true : false;
}

int MyQueue::QueueLength() const
{
return m_iQueueLen;
}

bool MyQueue::QueueFull() const
{
if (m_iQueueLen==m_iQueueCapacity)
{
return true;
}
return false;
}

bool MyQueue::EnQueue(int 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(int &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()
{
cout << endl;
for (int i = m_iHead; i < m_iQueueLen + m_iHead; i++)
{
cout << m_pQueue[i%m_iQueueCapacity] << endl;
}
cout << endl;
}

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//实现环形队列

#include <iostream>
#include <stdlib.h>
#include "MyQueue.h"
using namespace std;

int main(void)
{
MyQueue *p = new MyQueue(4);

p->EnQueue(1);
p->EnQueue(2);
p->EnQueue(3);
p->EnQueue(4);
p->EnQueue(5);//判满
p->QueueTraverse();

int e = 0;
p->DeQueue(e);
cout << e << endl;

p->DeQueue(e);
cout << endl;
cout << e << endl;

p->QueueTraverse();

p->ClearQueue();
p->QueueTraverse();

p->EnQueue(10);
p->EnQueue(20);
p->QueueTraverse();

delete p;
p = NULL;

system("pause");
return 0;
}

运行结果

demo

若对象成员较复杂:比如为Custeomer类

Customer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <string>
using namespace std;

class Customer
{
public:
Customer(string name = "", int age = 0);
void printInfo() const;
private:
string m_strName;
int m_iAge;
};

#endif

Customer.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include "Customer.h"
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;
cout << endl;
}

MyQueue.h修改

1
2
3
4
#include "Customer.h"
bool EnQueue(Customer element); //EnQueue(&Q, element) 新元素入队
bool DeQueue(Customer &element); //DeQueue(&Q, &element) 首元素出队
Customer *m_pQueue; //队列数组指针

MyQueue.cpp修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool MyQueue::EnQueue(Customer element)
bool MyQueue::DeQueue(Customer &element)

MyQueue::MyQueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
m_pQueue = new Customer[m_iQueueCapacity];
ClearQueue();
}

void MyQueue::QueueTraverse()
{
cout << endl;
for (int i = m_iHead; i < m_iQueueLen + m_iHead; i++)
{
m_pQueue[i%m_iQueueCapacity].printInfo();
cout << "前面还有" << (i - m_iHead) << "人" << endl;
}
cout << endl;
}

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
27
28
29
30
31
32
33
//实现环形队列

#include <iostream>
#include <stdlib.h>
#include "MyQueue.h"
#include "Customer.h"
using namespace std;

int main(void)
{
MyQueue *p = new MyQueue(4);
Customer c1("zhangsan", 20);
Customer c2("lisi", 30);
Customer c3("wangwu", 10);

p->EnQueue(c1);
p->EnQueue(c2);
p->EnQueue(c3);

p->QueueTraverse();

Customer c4("", 0);
p->DeQueue(c4);
c4.printInfo();

p->QueueTraverse();

delete p;
p = NULL;

system("pause");
return 0;
}

运行结果

result

文章目录
  1. 1. MyQueue.h
  2. 2. MyQueue.cpp
  3. 3. demo.cpp
  4. 4. 运行结果
  5. 5. Customer.h
  6. 6. Customer.cpp
  7. 7. MyQueue.h修改
  8. 8. MyQueue.cpp修改
  9. 9. demo.cpp
  10. 10. 运行结果
|