noj11循环右移

标题
在这里插入图片描述
示例
在这里插入图片描述
由于这次写文章距离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;
}

如果有大佬觉得方法复杂了或者有什么错误的地方,欢迎批评指正!
怕什么真理无穷,进一寸有一寸的欢喜。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值