标签:
队列( FIFO )
数组实现
#include <stdio.h>
#define N 64
int main()
{
int i, n, front, rear, q[2*N];
scanf("%d", &n);
for(i = 0; i < n; i++)
q[i] = i + 1;
front = 0;
rear = n;
while(rear – front > 0)
{
printf("%d ", q[front]);
front++;
q[rear++] = q[front++];
}
return 0;
}
循环队列
数组实现
#include <stdio.h>
#define N 64
int main()
{
int i, n, front, rear, q[N];
scanf("%d", &n);
for(i = 0; i < n; i++)
q[i] = i + 1;
front = 0; rear = n;
while((rear - front + N) % N > 0)
{
printf("%d ", q[front]);
front = (front + 1) % N;
q[rear] = q[front];
rear = (rear + 1) % N;
front = (front + 1) % N;
}
return 0;
}
STL实现
#include <cstdio>
#include <queue>
using namespace std;
int main()
{
queue<int> q;
int i, n;
scanf("%d", &n);
for(i = 0; i < n; i++)
q.push(i + 1);
while(!q.empty())
{
printf("%d ", q.front());
q.pop();
q.push(q.front());
q.pop();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_28236309/article/details/47206565