顺序队列
#include <iostream>
using namespace std;
//顺序队列
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int front, rear;
}SqQueue;
//初始化队列
void Init_Queue(SqQueue *&q)
{
q = (SqQueue*)malloc(sizeof(SqQueue));
q->front = q->rear = -1;
}
//销毁队列
void Destory(SqQueue*& q)
{
free(q);
}
//判断队列是否为空
bool Queue_Empty(SqQueue*& q)
{
return (q->front ==q->rear );
}
//进队列
bool EnQueue(SqQueue*&q, int e)
{
if (q->rear == MaxSize - 1)
return false;
q->rear++;
q->data[q->rear] = e;
return true;
}
//出队列
int DeQueue(SqQueue*&q,int &e)
{
if (q->front == q->rear)
return false;
q->front++;
e = q->data[q->front];
return e;
}
int main()
{
SqQueue* q;
int e;
int a[5] = { 1,2,3,4,5 };
int b[5] = { 0 };
Init_Queue(q);
for (int i = 0; i < 5; i++)
{
EnQueue(q, a[i]);
}
for (int j = 0; j < 5; j++)
{
b[j] = e = DeQueue(q, e);
}
for (int k = 0; k < 5; k++)
{
cout << b[k];
}
return 0;
}
出队后,剩余的空间就用不到了,所以为了高效,需要改进一下
环形队列
#include <iostream>
using namespace std;
//顺序队列
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int front; //队头指针
int count; //队列中元素个数
} QuType;
//初始化队列
void InitQueue(QuType*& qu) //初始化队运算算法
{
qu = (QuType*)malloc(sizeof(QuType));
qu->front = 0;
qu->count = 0;
}
//判断队列是否为空
//进队列
bool EnQueue(QuType*& qu,int x) //进队运算算法
{
int rear; //临时队尾指针
if (qu->count == MaxSize) //队满上溢出
return false;
else
{
rear = (qu->front + qu->count) % MaxSize; //求队尾位置
rear = (rear + 1) % MaxSize; //队尾循环增1
qu->data[rear] = x;
qu->count++; //元素个数增1
return true;
}
}
//出队列
int DeQueue(QuType*& qu,int & x) //出队运算算法
{
if (qu->count == 0) //队空下溢出
return false;
else
{
qu->front = (qu->front + 1) % MaxSize; //队头循环增1
x = qu->data[qu->front];
qu->count--; //元素个数减1
return x;
}
}
bool QueueEmpty(QuType* qu) //判队空运算算法
{
return(qu->count == 0);
}
int main()
{
QuType* q;
int e;
int a[5] = { 1,2,3,4,5 };
int b[5] = { 0 };
InitQueue(q);
for (int i = 0; i < 5; i++)
{
EnQueue(q, a[i]);
}
for (int j = 0; j < 5; j++)
{
b[j] = e = DeQueue(q, e);
}
for (int k = 0; k < 5; k++)
{
cout << b[k];
}
return 0;
}
#include <iostream>
using namespace std;
//顺序队列
#define MaxSize 10
typedef struct qnode
{
int data; //数据元素
struct qnode* next;
} DataNode;
typedef struct
{
DataNode* front; //指向单链表队头结点
DataNode* rear; //指向单链表队尾结点
} LinkQuNode;
void InitQueue ( LinkQuNode*& q)
{
q = (LinkQuNode*)malloc(sizeof(LinkQuNode));
q->front = q->rear = NULL;
}
void DestroyQueue(LinkQuNode*& q)
{
DataNode* p = q->front, * r; //p指向队头数据结点
if (p != NULL) //释放数据结点占用空间
{
r = p->next;
while (r != NULL)
{
free(p);
p = r; r = p->next;
}
}
free(p); free(q); //释放链队结点占用空间
}
bool QueueEmpty(LinkQuNode* q)
{
return(q->rear == NULL);
}
void enQueue(LinkQuNode*& q,int e)
{
DataNode* p;
p = (DataNode*)malloc(sizeof(DataNode));
p->data = e;
p->next = NULL;
if (q->rear == NULL) //若链队为空,新结点是队首结点又是队尾结点
q->front = q->rear = p;
else
{
q->rear->next = p; //将p结点链到队尾,并将rear指向它
q->rear = p;
}
}
int deQueue(LinkQuNode*& q,int& e)
{
DataNode* t;
if (q->rear == NULL) return false; //队列为空
t = q->front; //t指向第一个数据结点
if (q->front == q->rear) //队列中只有一个结点时
q->front = q->rear = NULL;
else //队列中有多个结点时
q->front = q->front->next;
e = t->data;
free(t);
return e;
}
int main()
{
LinkQuNode* q;
int e;
int a[5] = { 1,2,3,4,5 };
int b[5] = { 0 };
InitQueue(q);
for (int i = 0; i < 5; i++)
{
enQueue(q, a[i]);
}
for (int j = 0; j < 5; j++)
{
b[j] = e = deQueue(q, e);
}
for (int k = 0; k < 5; k++)
{
cout << b[k];
}
return 0;
}