思路
把要逆的先入队,剩下的在入队,最后全部出队赋值给数组
代码
typedef struct queue {
int data;
struct queue* next;
}Queue;
typedef struct Mqueue {
Queue *front;//头指针
Queue *rear;//尾指针
}Mqueue;
//初始化队列
int init_Queue(Mqueue *mq) {
mq->front = mq->rear =(Queue *) malloc(sizeof(Queue));
if (!mq->front) return 0;return 1;
}
//进队操作
int into_Queue(Mqueue* mq,int a) {
Queue* p= (Queue*)malloc(sizeof(Queue));
mq->rear->data = a;
mq->rear->next = p;
mq->rear = p;
return 1;
}
//出队操作
int out_Queue(Mqueue* mq) {
int a;
if (mq->front == mq->rear) return 0;
Queue* p = mq->front;
mq->front = p->next;
a = p->data;
p->next = NULL;
free(p);
return a;
}
void exchangeI(int a[],int n,int p) {
Mqueue mq;
init_Queue(&mq);
for (int i = p; i < n; i++)
{
into_Queue(&mq, a[i]);
}
for (int i = 0; i < p; i++)
{
into_Queue(&mq, a[i]);
}
for (int i = 0; i < n; i++)
{
a[i]=out_Queue(&mq);
}
}