SDUTOJ-3331 数据结构实验之链表八:Farey序列

链表节点插入练习题 题目链接

#include <stdio.h>
#include <cstdlib>
using namespace std;


typedef int ElementType;

typedef struct node
{
    ElementType x, y;
    struct node * next;
} Lnode;

typedef Lnode * List;

void PrintL(List head)
{
    List p = head;
    int n = 0;
    if(p)
    {
        while(p->next)
        {
            if(++n % 10 == 0)
            printf("%d/%d\n", p->x, p->y);
            else
            printf("%d/%d\t", p->x, p->y);
            p = p->next;
        }
        printf("%d/%d\n", p->x, p->y);
    }
    else
    {
        printf("This list is empty!\n");
    }
}

int main()
{
    int n;
    scanf("%d", &n);

    List head = (List) malloc( sizeof(Lnode) );
    head->x = 0;
    head->y = 1;
    List rear = (List) malloc( sizeof(Lnode) );
    head->next = rear;
    rear->next = NULL;
    rear->x = rear->y = 1;

    int w = n-1;
    while(w--)
    {
        List p = head;
        while(p->next)
        {
            if(p->y + p->next->y <= n)
            {
                List q = (List) malloc( sizeof(Lnode) );
                q->next = p->next;
                q->x = p->x + p->next->x;
                q->y = p->y + p->next->y;
                p->next = q;
                p = q->next;
                continue;
            }
            p = p->next;
        }
    }

    PrintL(head);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/wz_e18/article/details/82970405