Queue (FIFO)
Array implementations
#include <stdio.h>#define NintMain () {intI, 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;}
Loop queue
Array implementations
#include <stdio.h>#define NintMain () {intI, 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 implementations
#include <cstdio>#include <queue>using namespace STD;intMain () { Queue<int>QintI, 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Queue (FIFO) loop queue