兵工厂 发表于 2022-12-4 18:48:09

算法入门C语言

求助2.2 2.5 2.6三个问题
好久没敲代码了
刚学算法
对各位大佬应该很简单吧
求求了

焦糖橙子 发表于 2022-12-18 12:32:44

2.2的

#include<stdio.h>
#include<stdlib.h>

typedef struct node//定义结点
{
        int data;
        struct node *next;
}node;

node *creat(int n)//创建循环链表
{
        int i;
        node *p,*temp,*head;
        p = (node*)malloc(sizeof(node));
        head = p;
        for(i=1;i<=n;i++)
        {
                p->data=i;
                p->next = (node*)malloc(sizeof(node));
                temp = p;
                p = p->next;
               
        }
        temp->next = head;
        return head;
       
}

int main()
{
        int n,m,i;
        node *p,*temp;
       
        printf("请输入数组大小n,位移距离m:");
        scanf("%d%d",&n,&m);
        p = creat(n);
        printf("\n位移前数组:");
        temp = p;
        for(i=1;i<=n;i++)//输出结果
        {
                printf("%d",temp->data);
                temp = temp->next;
               
        }
        printf("\n位移后数组:");
        temp = p;
        for(i=1;i<=m;i++)//先进行位移
        {
                temp = temp->next;
        }
        for(i=1;i<=n;i++)//输出结果
        {
                printf("%d",temp->data);
                temp = temp->next;
        }
        printf("\n");
        return 0;
}
页: [1]
查看完整版本: 算法入门C语言