C++ 使用数组实现简单队列
我对数组、队列和堆栈了解不多。我知道如何实现一个简单的队列C++ 使用数组实现简单队列,c++,data-structures,queue,C++,Data Structures,Queue,我对数组、队列和堆栈了解不多。我知道如何实现一个简单的队列 #include <iostream> #include <queue> using namespace std; void main() { queue<char> queue1; queue1.push('a'); queue1.push('b'); queue1.push('c'); queue1.push('d'); while(!queu
#include <iostream>
#include <queue>
using namespace std;
void main()
{
queue<char> queue1;
queue1.push('a');
queue1.push('b');
queue1.push('c');
queue1.push('d');
while(!queue1.empty())
{
cout << queue1.front();
queue1.pop();
cout << endl;
}
system("pause");
}
#包括
#包括
使用名称空间std;
void main()
{
队列1;
队列1.推送('a');
队列1.push('b');
队列1.推送('c');
队列1.推送('d');
而(!queue1.empty())
{
cout如果您的队列基于数组,那么为了提高效率,我建议创建一个有界或“循环”队列,其中队列的最大大小是固定的,您基本上有一个指向队列数组中“第一个”和“最后一个”位置的头和尾指针,当尾指针(或索引值)移动到“过去”位置在数组的末尾,它实际上会移回数组的开头。基于数组的无边界队列效率极低,因为每次填充数组的最大大小时,您都需要不断重新分配内存,并且/或者在删除队列的第一个元素时尝试重新洗牌数组中的元素
使用头
和尾
的整型数组索引,而不是实际的指针类型,以及用于确定队列中项目总数的计数器,您的入队和出队函数看起来可能非常简单:
template<typename T>
bool queue<T>::enqueue(const T& item)
{
if (count == array_size)
return false;
array[tail] = item;
tail = (tail + 1) % array_size;
count++;
return true;
}
template<typename T>
bool queue<T>::dequeue(T& item)
{
if (!count)
return false;
item = array[head];
head = (head + 1) % array_size;
count--;
return true;
}
模板
bool队列::排队(常量和项目)
{
if(计数==数组大小)
返回false;
数组[尾]=项;
tail=(tail+1)%array\u size;
计数++;
返回true;
}
模板
bool队列::出列(T和项目)
{
如果(!计数)
返回false;
项目=数组[头];
头=(头+1)%array\u size;
计数--;
返回true;
}
您可以将此概念扩展到您想要的任何其他函数,即,如果您希望使用单独的函数,如STL用于访问队列头并实际“删除”队列中的元素。注意:模拟阵列时(线性数据存储)作为一个循环数据存储和维护队列的属性,一个单元将永远不被使用。因此,对于6个单元的数组,数组的最大容量将是5。下面的C++代码是自解释的。
/*Implementation of queue with basic operation using arrays */
#include<iostream>
using namespace std;
#define MAX 6 //to accomodate a maximum of 05 elements as 1 cell pointed by tail will always be vacant
void ENQUE(int key); // ~insertion
int DEQUEUE(); // ~deletion
void TRAVERSE();
bool isEmpty();
bool isFull ();
int Q[MAX], head=0, tail=0; /* Note: head is the side facing cashier and new person joins the queue at tail. So, from cashier point of view tail~rear and head~front.
Q -> [h ][][][][][][][][][][t]
Q -> [h,t][][][][][][][][][][] : initial configuration*/
int main(){
int choice,val,i;
char ch='y';
do{
cout<<"1. For Enqueue \n";
cout<<"2. For Dequeue \n";
cout<<"3. For Traverse \nYour Option : ";
cin>>choice;
switch(choice)
{
case 1 : // insertion
if( isFull() ){
cout<<"\nQueue Full !!!\n";
break;
}
cin>>val;
ENQUE(val);
TRAVERSE();
break;
case 2 : //deletion
if( isEmpty() ){
cout<<"\nQueue Empty !!!\n";
break;
}
cout<<"\nDeleted element from Queue : "<<DEQUEUE()<<endl;
TRAVERSE();
break;
case 3 : //traversal
if( isEmpty() ){
cout<<"\nQueue Empty !!!\n";
break;
}
TRAVERSE();
break;
default :
cout<<"Please choose 1/2/3 !!! \n";
}
cout<<"\nDo you want to continue(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y'); //end of do loop
return 0;
}
void ENQUE(int x){
Q[tail] = x;
tail =(tail+1)%MAX ; //OR tail = (tail==MAX) ? 0 : tail+1 ; */
}
int DEQUEUE(){
int temp =Q[head];
head =(head+1)%MAX ; //OR head = (head==MAX) ? 0 : head+1 ; */
return temp;
}
void TRAVERSE(){
int i; //simple case: Q -> [ ][ ][h7][8][9][5t][ ][ ][ ][ ][ ]
for(i=head; i!=tail; i=(i+1)% MAX) //complex case: Q -> [16][t][ ][ ][ ][h5][11][12][13][14][15]
cout<<Q[i]<<" ";
cout<<endl;
}
bool isEmpty(){
if(head == tail)
return true;
else
return false;
}
bool isFull(){
if( (tail == MAX-1 && head == 0) || (head == tail + 1) )
return true;
else
return false;
}
/*使用数组实现具有基本操作的队列*/
#包括
使用名称空间std;
#定义最大6//以容纳最多05个元素,因为一个由尾部指向的单元格将始终为空
void ENQUE(int键);//~插入
int DEQUEUE();//~删除
无效遍历();
bool是空的();
bool是full();
int Q[MAX],head=0,tail=0;/*注意:head是面向出纳的侧面,新人从尾部加入队列。因此,从出纳的角度来看,tail~后部和head~前部。
Q->[h][[]t]
Q->[h,t][]:初始配置*/
int main(){
int选择,val,i;
char ch='y';
做{
如果您还不了解如何从头开始实现它,请继续使用您演示的std
版本。如果这是家庭作业,请记住队列是先进先出的。我不同意您关于“知道如何实现简单队列”的说法。到目前为止,您只演示了可以使用名为“队列”的库类。您可以使用循环缓冲区/循环数组实现队列。查看此链接了解我可以在堆栈上应用相同的内容吗?是的,当然,尽管堆栈不会像队列那样“环绕”,因为您只添加和删除“顶部”元素。可能会由于“tail”溢出而导致问题。如果使用无符号long
类型,则溢出tail
将需要一段时间。为了避免该问题,还可以使用其他方法,为了简单起见,我使用了此示例。使用head和count实现可以避免许多问题。Th您的MAX
定义中的e字面值应该是6
。前导的0
使其成为八进制文字。在这种特定情况下,这不是问题,但可能是错误的潜在来源。