@这篇文档是由C++代码实现的环形队列
数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。队列就是先进先出的一种数据结构,环形队列只是将队尾元素与队头元素连接在了一起,要记住的就是队头指针永远指向的是存放队头的那个数组的下标,而队尾指针指向的是队尾元素的下一个数组的下标。
本文是以Visual Studio中新建的C++win32的控制台应用程序实现的,其中建立了头文件和源文件以及实现main()的demo文件,分别是MyQueue.h和MyQueue.cpp和demo.cpp文件。
Customer.h文件
#ifndef
CUSTOMER_H
#define
CUSTOMER_H
#include
<iostream
>
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文件
#include
"Customer.h"
#include
<iostream
>
#include
<string
>
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.h文件
#ifndef
MYQUEUE_H
#define
MYQUEUE_H
#include
"Customer.h"
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
);
bool
QueueTraverse();
private:
Customer
*m_pQueue
;
int m_iQueueLen
;
int m_iQueueCapacity
;
int m_iHead
;
int m_iTail
;
};
#endif
MyQueue.cpp
#include
"MyQueue.h"
#include
"Customer.h"
#include
<iostream
>
using namespace std
;
MyQueue
::MyQueue(int queueCapacity
)
{
m_iQueueCapacity
= queueCapacity
;
ClearQueue();
m_pQueue
= new Customer[queueCapacity
];
}
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;
}
}
bool MyQueue
::QueueFull() const
{
return m_iQueueLen
==m_iQueueCapacity
?true:false;
}
int MyQueue
::QueueLength() const
{
return m_iQueueLen
;
}
bool MyQueue
::EnQueue(Customer element
)
{
if(QueueFull())
{
cout
<< "队列已满,不能再插入元素!" << endl
;
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())
{
cout
<< "队列为空,不能再取元素!" << endl
;
return false;
}
else
{
element
= m_pQueue
[m_iHead
];
m_iHead
++;
m_iHead
= m_iHead
%m_iQueueCapacity
;
m_iQueueLen
--;
return true;
}
}
bool MyQueue
::QueueTraverse()
{
if(QueueEmpty())
{
cout
<< "队列为空,遍历不出任何元素!" << endl
;
return false;
}
else
{
for(int i
=m_iHead
; i
<m_iHead
+m_iQueueLen
;i
++)
{
m_pQueue
[i
%m_iQueueCapacity
].printInfo();
cout
<< "前面还有" << (i
-m_iHead
) << "人" << endl
;
cout
<< endl
;
}
return true;
}
}
demo.cpp文件
#include
<iostream
>
#include
<stdlib
.h
>
#include
"Customer.h"
#include
"MyQueue.h"
using namespace std
;
int
main(void)
{
MyQueue
*p
= new MyQueue(100);
Customer
c1("张三", 18);
Customer
c2("李四", 19);
Customer
c3("王五", 20);
Customer
c4("赵六", 21);
p
->EnQueue(c1
);
p
->EnQueue(c2
);
p
->EnQueue(c3
);
p
->EnQueue(c4
);
cout
<< endl
;
p
->QueueTraverse();
cout
<< endl
;
Customer
c5("",0);
p
->DeQueue(c5
);
Customer
c6("",0);
p
->DeQueue(c6
);
Customer
c7("郑七", 22);
Customer
c8("黄八", 23);
p
->EnQueue(c7
);
p
->EnQueue(c8
);
cout
<< endl
;
cout
<< "循环遍历队列:" << endl
;
cout
<< endl
;
p
->QueueTraverse();
delete p
;
p
= NULL;
system("pause");
return 0;
}
这个操作让我想到删除硬盘上的东西是不是只是移动了指针,而没有真正的删除已存储的东西。如果再次往里面存东西只是说覆盖了之前存储的东西,所以说如果硬盘上的东西如果不小心删除了,只要再不往里面存东西,是可以有办法恢复之前存储的东西的,当然硬盘存储可能根本不是队列,但原理应该差不多。