法一:数组
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n,head,tail,a[10000],i;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
head=1;
tail=n+1;
while(head<tail){
printf("%d",a[head]);
head++;
a[tail]=a[head];
tail++;
head++;
}
return 0;
}
法二:队列(数组模拟)
4.队列:出队,入队;head,tail;(First In First Out原则)
用于:广度优先搜索 以及 队列优化的Bellman—Ford最短路算法核心结构;
struct queue
{
int date[1000];//队列主体
int head;//队首
int tail;//队尾
}
#include <iostream>
#include<stdio.h>
using namespace std;
struct queue{
int data[1000];
int head;
int tail;
};
int main()
{
struct queue q;
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&q.data[i]);
}
q.head=1;
q.tail=n+1;
while(q.head<q.tail){
printf("%d",q.data[q.head]);
q.head++;
q.data[q.tail]=q.data[q.head];
q.tail++;
q.head++;
}
return 0;
}