标题
示例
由于这次写文章距离AC时间较长,当时没有趁热打铁写完,希望现在还能回忆起来。
注意题目要求是循环右移,这个示例给的有点迷惑性,左移三位和右移三位结果一样,当时我就给左移了【可能眼神不太好】,所以我的方法就在左移的基础上逆着输出了一下(我记得是这样的doge),还是使用循环队列,
还是看代码吧
#include <stdio.h>
#include <stdlib.h>
//节点
typedef struct Node
{
int val;/* data */
struct Node* next;
struct Node* pre;
}Node,*ListNode;
//队列
typedef struct queue
{
ListNode front;
ListNode rear;
}LinkQueue;
//初始化队列
void InitQueue(LinkQueue *s)
{
ListNode p=(ListNode)malloc(sizeof(Node));
p->val=0;
p->next=NULL;
p->pre=NULL;
s->front=p;
s->rear=p;
}
//入队
void AddQueue(LinkQueue *s,int n)
{
ListNode p=(ListNode)malloc(sizeof(Node));
p->val=n;
p->next=s->front;
p->pre=s->rear;
s->rear->next=p;
s->rear=p;
s->front->pre=s->rear;
}
//出队
int DeQueue(LinkQueue *s)
{
int temp;
ListNode p=s->front->next;
temp=p->val;
p=p->next;
p->pre=s->front;
s->front->next=p;
return temp;
}
//输出
void PrintQueue(LinkQueue *s)
{
ListNode p=s->rear;
while (p!=s->front)
{
/* code */
printf("%d ",p->val);
p=p->pre;
}
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
int A[n];
LinkQueue a;
InitQueue(&a);
//正着输入
for (int i = 0; i < n; i++)
{
scanf("%d",&A[i]);
}
//倒着插入到队列里面
for (int i = n-1; i >= 0; i--)
{
/* code */
AddQueue(&a,A[i]);
}
//出队再入队,即可实现移动
for (int i = 0; i < k; i++)
{
/* code */
int temp;
temp=DeQueue(&a);
AddQueue(&a,temp);
}
//最后打印
PrintQueue(&a);
return 0;
}
如果有大佬觉得方法复杂了或者有什么错误的地方,欢迎批评指正!
怕什么真理无穷,进一寸有一寸的欢喜。