用数组实现顺序队列
队列遵循先进先出原则,所以入队位置固定,出队位置也固定。
#include<iostream>
#define maxSize 5//队列最大长度
using namespace std;
int tag = 0;//0代表出队,1代表入队
struct qtQueue {
int front, rear;//代表队头队尾的数组下标
int value[maxSize];
};
void initQueue(qtQueue &s)
{
s.rear = s.front = 0;
}
bool enQueue(qtQueue& s,int e)//入队
{
if (tag == 1 && s.rear == s.front)//判断队列是否已经满了
{
cout << "队列已经满了"<<endl;
return false;
}
s.value[s.rear] = e;
s.rear++;
if (s.rear == maxSize)
{
s.rear = 0;
}
tag = 1;
return true;
}
bool ouQueue(qtQueue& s, int& e)//出队
{
if (tag == 0 && s.rear == s.front)
{
cout << "队列为空!"<<endl;
return false;
}
e = s.value[s.front];
s.front++;
if (s.front == maxSize)
{
s.front = 0;
}
tag = 0;
return true;
}
void main()
{
qtQueue S;
initQueue(S);
int e[] = {1,2,3,4,5,6,7,0};
int i = 0;
bool b;
while (e[i])//记得用0来结束循环bool b;
while (e[i])
{
b=enQueue(S, e[i]);
if (!b)//满了就跳出循环
{
break;
}
i++;
}
{
b=ebool b;
while (e[i])
{
b=enQueue(S, e[i]);
if (!b)//满了就跳出循环
{
break;
}
i++;
}nQueue(S, e[i]);
if (!b)//如果返回false则代表队列满了,跳出循环
{
break;
}
i++;
}
int a;
while (1)
{
b = ouQueue(S, a);
if (b)
{
cout << a;
}
else
{
break;
}
}
}
用指针实现顺序队列
#include<iostream>
#define maxSize 5//队列最大长度
using namespace std;
int tag = 0;//0代表出队,1代表入队
int in = 0, out = 0;//为了实现环状,用来记录队头队尾指针的位置
struct qtQueue {
int* front, * rear;
};
void initQueue(qtQueue& s)
{
s.front = new int[maxSize];
if (!s.front)
exit(-1);
s.rear = s.front;
}
bool enQueue(qtQueue& s, int e)//入队
{
if (tag == 1 && s.rear == s.front)//判断队列是否已经满了
{
cout << "队列已经满了" << endl;
return false;
}
*s.rear = e;
s.rear++;
in++;
if (in == maxSize)//判断指针是否已经达到申请的最大的节点位置
{
s.rear = s.rear - maxSize;//把尾节点的地址替换成申请地址的第一块,形成环
in = 0;
}
tag = 1;
return true;
}
bool ouQueue(qtQueue& s, int& e)//出队
{
if (tag == 0 && s.rear == s.front)
{
cout << "队列为空!" << endl;
return false;
}
e = *s.front;//获取数据并传递给e
s.front++;//指向下一块地址
out++;
if (out == maxSize)//判断指针是否已经达到申请的最大的节点位置
{
s.front = s.front - maxSize;//把尾节点的地址替换成申请地址的第一块,形成环
out = 0;
}
tag = 0;
return true;
}
void main()
{
qtQueue S;
initQueue(S);
int e[] = { 1,2,3,4,5,6,7,0 };
int i = 0;
bool b;
while (e[i])
{
b=enQueue(S, e[i]);
if (!b)
{
break;
}
i++;
}
int a;
while (1)
{
b = ouQueue(S, a);
if (b)
{
cout << a;
}
else
{
break;
}
}
delete S.front;
}
用链表实现链队
#include<iostream>
using namespace std;
struct qtQueue {
int value;
qtQueue* next;//第一个节点不存放东西
qtQueue* front, * rear;
};
void initQueue(qtQueue& s)
{
s.front = new qtQueue;
if (!s.front)
exit(-1);
s.rear = s.front;
s.front->next = NULL;
}
bool enQueue(qtQueue& s, int e)//入队
{
qtQueue* p = new qtQueue;
p->value = e;
p->next = NULL;
s.rear->next = p;
s.rear = p;
return true;
}
bool ouQueue(qtQueue& s, int& e)//出队
{
if (s.front->next==NULL)
{
cout << "队列为空!" << endl;
return false;
}
e = s.front->next->value;//获取数据并传递给e,头节点不存放数据
qtQueue* p;
p = s.front->next;
s.front->next = s.front->next->next;//指向下一块地址
if (s.front->next == NULL)//如果链队空了,则队尾指针指向队头指针
{
s.rear = s.front;
}
delete p;
return true;
}
void main()
{
qtQueue S;
initQueue(S);
int e[] = { 1,2,3,4,5,6,7,8,0 };
int i = 0;
bool b;
while (e[i])
{
b=enQueue(S, e[i]);
if (!b)//满了就跳出循环
{
break;
}
i++;
}
int a;
while (1)
{
b = ouQueue(S, a);
if (b)
{
cout << a;
}
else
{
break;
}
}
delete S.front;
}