实验09 动态数据组织(2021级)

一. 程序题(共7题,100分)

1. (程序题)

题目编号:Exp09-Enhance01

题目名称:单链表倒数第K个结点

题目描述:请填写缺失代码完成程序,实现如下功能:

    首先根据键盘随机输入,以0结束的若干非零整数建立单链表;

    然后根据输入的整数K,输出链表倒数第K个结点的值,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符;

    若不存在则输出NULL。

例如,

    输入:1 2 3 4 5 6 7 8 0 3

    输出:6

    输入:0 2 3 4 5 6 7 8 0 3

    输出:NULL

***注意***:

    提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。


#include <stdio.h>

#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
struct cell * back(struct cell* head, int k) {//求链表倒数第k个结点,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数back的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head,*result;
 int k;
 head = build();
 scanf("%d", &k);
 result = back(head,k);
 if (result != NULL)
        printf("%d",result->x);
    else
        printf("NULL");
 release(head);
}
#include <stdio.h>

#include <malloc.h>

struct cell {//单链表结点结构体定义

    int x;

    struct cell* next;

};

struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回

    struct cell* head, * tmp, * p;

    head = tmp = p = NULL;

    int n;

    head = (cell*)malloc(sizeof(cell));

    tmp = head;

    head->next = NULL;


    while (head != NULL) {

        scanf("%d", &n);

        if (n == 0)break;

        p = (cell*)malloc(sizeof(cell));

        p->x = n;

        p->next = NULL;

        tmp->next = p;

        tmp = p;

    }

    if (head->next == NULL) {

        free(head);

        head = NULL;

    }

    return head;//返回单链表头

}

struct cell* back(struct cell* head, int k) {//求链表倒数第k个结点,head是单链表首结点指针

    struct cell* tmp, * p;

    int length, i;

    if (head == NULL)return NULL;

    tmp = head->next;

    for (length = 1; tmp != NULL; length++, tmp = tmp->next);

    if (k >= length|| k<=0)return NULL;

    for (i = 1, tmp = head; i <= length - k; i++, tmp = tmp->next);

    return tmp;

}

void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针

    struct cell* tmp,*p;

    tmp = head;

    while (tmp != NULL) {

        p = tmp;

        tmp = tmp->next;

        free(p);

    }

}

int main(void) {

    struct cell* head, * result;

    int k;

    head = build();

    scanf("%d", &k);

    result = back(head, k);

    if (result != NULL)

        printf("%d", result->x);

    else

        printf("NULL");

    release(head);

}

2. (程序题)

题目编号 :Exp09-Basic03

题目名称:求单链表中间结点

题目描述:请填写缺失代码完成程序,实现如下功能:

首先根据键盘随机输入,以0结束的若干非零整数建立单链表;

然后寻找处于链表中间位置的结点,若中间结点有两个,则设定前一个为中间位置结点;

最后将从中间结点开始到链表尾各结点值输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符。

若是空链表,则输出NULL。

例如,

输入:5 4 2 1 3 0 

输出:2 1 3

输入: 4 2 1 3 3 2 0 

输出:1 3 3 2

***注意***:

提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。


#include <stdio.h>

#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数mid的功能
       ......
       ......
       ......
   */
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head,*half;
 head = build();
 half = mid(head);
 if(half!=NULL)
        print(half);
    else
        printf("NULL");
 release(head);
}
#include <stdio.h>

#include <malloc.h>

struct cell {//单链表结点结构体定义

    int x;

    struct cell* next;

};

struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回

    struct cell* head, * tmp, * p;

    head = tmp = p = NULL;

    int n;

    

    head = (cell*)malloc(sizeof(cell));

    head->next = NULL;

    tmp = head;


    while (head != NULL) {

        scanf("%d", &n);

        if (n == 0)break;

        p = (cell*)malloc(sizeof(cell));

        p->x = n;

        tmp->next = p;

        p->next = NULL;

        tmp = p;

    }

    if (head->next == NULL) {

        free(head);

        head = NULL;

    }

    return head;//返回单链表头

}

struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针

    

    if (head == NULL)return NULL;

    int length,i;

    struct cell* tmp, * p;

    tmp = head->next;

    for (length = 1; tmp != NULL; length++, tmp = tmp->next);

    tmp = head;

    for (i = 0; i < length / 2; i++) {

        tmp = tmp->next;

    }

    return tmp;

}

void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针

    struct cell* tmp, * p;

    tmp = head;

    while (tmp != NULL) {

        printf("%d", tmp->x);

        if (tmp->next != NULL)printf(" ");

        tmp = tmp->next;

    }

}

void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针

    struct cell* temp,*p;

    temp = head;

    while (temp != NULL) {

        p = temp;

        temp = temp->next;

        free(p);

    }

}

int main(void) {

    struct cell* head, * half;

    head = build();

    half = mid(head);

    if (half != NULL)

        print(half);

    else

        printf("NULL");

    release(head);

}

3. (程序题)

题目编号:Exp09-Basic04

题目名称:单链表交换两结点

题目描述:请填写缺失代码完成程序,实现如下功能:

首先根据键盘随机输入,以0结束的若干非零整数建立单链表;

然后根据输入的两个索引位置交换链表上的两个结点(链表首元素索引为1,且要交换的两个索引位置不相邻);

最后链表各结点值输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符。

若是空链表,则输出NULL。

例如,

输入:1 2 3 4 5 6 0 1 5

输出:5 2 3 4 1 6

输入:0 1 2 3 4 5 6 0 1 5

输出:NULL

***注意***:

提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。

#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
struct cell* swap(struct cell* head,int m,int n) {//交换索引为m和n的两个结点,head是单链表首结点指针
 if(head==NULL) return NULL;
    struct cell* pm=head, * pn=head;
 struct cell* pm0 = NULL, * pn0 = NULL;
 struct cell* tmp;
 int i;
 for (i = 1;i < m && pm != NULL;i++) { 
  pm0 = pm;
  pm = pm->next;
 }
 for (i = 1;i < n && pn != NULL;i++) {
  pn0 = pn;
  pn = pn->next;
 }
 if (pm != NULL && pn != NULL && m != n) {//索引为m,n的结点位于链表中间
        /*请在以下位置补充完整,实现函数swap的功能
       ......
       ......
       ......
    */  
  if (pm0 != NULL && pn0 != NULL) {
   /*请在以下位置补充完整,实现函数swap的功能
       ......
       ......
       ......
     */ 
  }
  if (pm0 == NULL && pn0 != NULL) {
   /*请在以下位置补充完整,实现函数swap的功能
       ......
       ......
       ......
     */
  }
  if (pm0 != NULL && pn0 == NULL) {
   /*请在以下位置补充完整,实现函数swap的功能
       ......
       ......
       ......
     */
  }
 }
 return head;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head;
 int m, n;
 head = build();
 scanf("%d%d", &m, &n);
 head = swap(head,m,n);
 if(head!=NULL)
        print(head);
    else
        printf("NULL");
 release(head);
}
#include <stdio.h>

#include <malloc.h>

struct cell {//单链表结点结构体定义

    int x;

    struct cell* next;

};

struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回

    struct cell* head, * tmp, * p;

    head = tmp = p = NULL;

    int n;


    head = (cell*)malloc(sizeof(cell));

    head->next = NULL;

    tmp = head;


    while (head != NULL) {

        scanf("%d", &n);

        if (n == 0)break;

        p = (cell*)malloc(sizeof(cell));

        p->x = n;

        tmp->next = p;

        p->next = NULL;

        tmp = p;

    }

    if (head->next == NULL) {

        free(head);

        head = NULL;

    }

    return head;//返回单链表头

}

struct cell* swap(struct cell* head, int m, int n) {//交换索引为m和n的两个结点,head是单链表首结点指针

    if (head == NULL) return NULL;

    struct cell* pm = head, * pn = head;

    struct cell* pm0 = NULL, * pn0 = NULL;

    struct cell* tmp;

    int i;


    for (i = 1; i < m && pm != NULL; i++) {

        pm0 = pm;

        pm = pm->next;

    }


    for (i = 1; i < n && pn != NULL; i++) {

        pn0 = pn;

        pn = pn->next;

    }


    if (pm != NULL && pn != NULL && m != n) {//索引为m,n的结点位于链表中间

        int temp;

        if (pm0 != NULL && pn0 != NULL) {//中间

            temp = pm->next->x; 

            pm->next->x = pn->next->x;

            pn->next->x = temp;

        }


        if (pm0 == NULL && pn0 != NULL) {  //pm0为头部 n>m

            temp = head->next->x;

            head->next->x = pn->next->x;

            pn->next->x = temp;

        }


        if (pm0 != NULL && pn0 == NULL) {  //pn0尾部  m>n

            temp = head->next->x;

            head->next->x = pm->next->x;

            pm->next->x = temp;

        }

    }

    return head;

}

void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针

    struct cell* tmp, * p;

    tmp = head->next;

    while (tmp != NULL) {

        printf("%d", tmp->x);

        if (tmp->next != NULL)printf(" ");

        tmp = tmp->next;

    }

}

void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针

    struct cell* temp, * p;

    temp = head;

    while (temp != NULL) {

        p = temp;

        temp = temp->next;

        free(p);

    }

}

int main(void) {

    struct cell* head;

    int m, n;

    head = build();

    scanf("%d %d", &m, &n);

    head = swap(head, m, n);

    if (head != NULL)

        print(head);

    else

        printf("NULL");

    release(head);

}

4. (程序题)

题目编号:Exp09-Enhance03

题目名称:合并单链表

题目描述:请填写缺失代码完成程序,实现如下功能:

    首先从键盘输入一行以0结束的若干非零整数,建立一个单链表,输入的整数顺序为数字非递减顺序;

    然后以同样的方式,仍在第一行继续输入并建立第二个单链表;

    之后将两个链表合并形成一个新链表,使得新链表依然保持数字非递减顺序;

    最后验证输出新链表所有值,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符。若是空链表,则输出NULL。

例如,

    输入:2 3 4 4 5 6 0 1 3 4 6 7 0

    输出:1 2 3 3 4 4 4 5 6 6 7

    输入:0 0

    输出:NULL

***注意***:

    提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。


  #include <stdio.h>

#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * tail;
 head = tmp = tail = NULL;
 int n, i;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
struct cell* combine(struct cell* p, struct cell* q) {//合并两个链表p和q
 struct cell* head= NULL,*p0=NULL,*q0=NULL,*r=NULL;
 if (p == NULL && q!= NULL) return q;
 if (p != NULL && q == NULL) return p;
 if (p == NULL && q == NULL) return NULL;
 /*请在以下位置补充完整,实现函数combine的功能
       ......
       ......
       ......
   */
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head1,*head2, *result;
 head1 = build();
 head2 = build();
 result = combine(head1,head2);
 if (result != NULL)
  print(result);
 else
  printf("NULL");
 release(result);
 return 0;
}
    #include <stdio.h>

    #include <malloc.h>


    struct cell {//单链表结点结构体定义

        int x;

        struct cell* next;

    };

    struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回

        struct cell* head, * tmp, * tail;

        head = tmp = tail = NULL;

        int n, i;

        head = (cell*)malloc(sizeof(cell));

        tmp = head;

        head->next = NULL;


        while (head != NULL) {

            scanf("%d", &n);

            if (n == 0)break;

            tail = (cell*)malloc(sizeof(cell));

            tail->x = n;

            tail->next = NULL;

            tmp->next = tail;

            tmp = tail;

        }

        if (head->next == NULL) {

            free(head);

            head = NULL;

        }

        return head;//返回单链表头

    }

    struct cell* combine(struct cell* p, struct cell* q) {//合并两个链表p和q

        struct cell* head = NULL, * p0 = NULL, * q0 = NULL, * r = NULL;

        struct cell* temp = NULL;

        if (p == NULL && q != NULL) return q;

        if (p != NULL && q == NULL) return p;

        if (p == NULL && q == NULL) return NULL;


        head = (cell*)malloc(sizeof(cell));

        head->next = NULL;

        temp = head;

        p0 = p->next;

        q0 = q->next;

        while (p0 != NULL) {

            r = (cell*)malloc(sizeof(cell));

            temp->next = r;

            r->x = p0->x;

            p0 = p0->next;

            r->next = NULL;

            temp = r;

        }


        while (q0 != NULL) {

            r = (cell*)malloc(sizeof(cell));

            temp->next = r;

            r->x = q0->x;

            q0 = q0->next;

            r->next = NULL;

            temp = r;

        }


        //next begin bubble sort


        return head;

    }

    void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针

        struct cell* temp;

        temp = head->next;

        for (; temp != NULL; temp = temp->next) {

            struct cell* temp1 = temp->next;

            for (; temp1 != NULL; temp1 = temp1->next) {

                if (temp->x >= temp1->x) {

                    int tempnum = temp->x;

                    temp->x = temp1->x;

                    temp1->x = tempnum;

                }

            }

        }


        temp = head->next;


        while (temp != NULL) {

            printf("%d", temp->x);

            if (temp->next != NULL)printf(" ");

            temp = temp->next;

        }

    }

    void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针

        struct cell* tmp, * p;

        tmp = head;

        while (tmp != NULL) {

            p = tmp;

            tmp = tmp->next;

            free(p);

        }

    }

    int main(void) {

        struct cell* head1, * head2, * result;

        head1 = build();

        head2 = build();

        result = combine(head1, head2);

        if (result != NULL)

            print(result);

        else

            printf("NULL");

        release(result);

        return 0;

    }


5. (程序题)

题目编号:Exp09-Basic01

题目名称:创建单链表

题目描述:请填写缺失代码完成程序,实现如下功能:

根据从键盘随机输入以0结束的若干非零整数,建立一个单链表;之后将此链表中保存的数字顺次输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符;若是空链表,则输出NULL。

例如, 

输入:5 4 2 1 3 0 

输出:5 4 2 1 3

输入:0 5 4 2 1 3 0 

输出:NULL

***注意***:

提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。

#include <stdio.h>
#include <malloc.h>
struct cell { //单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) { //新建单链表,并将建好的单链表首结点地址返回
 struct cell* head,*tmp,*p;
 head = tmp = p = NULL;
 int n;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
void print(struct cell* head) {//打印整个单链表,head是指向单链表首结点的指针
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是指向单链表首结点的指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head;
 head = build();
 if(head!=NULL)
        print(head);
    else
        printf("NULL");
 release(head);
}
    #include <stdio.h>

    #include <malloc.h>

    struct cell { //单链表结点结构体定义

        int x;

        struct cell* next;

    };

    struct cell* build(void) { //新建单链表,并将建好的单链表首结点地址返回

        struct cell* head, * tmp, * p;

        head = tmp = p = NULL;

        int n;

        head = (cell*)malloc(sizeof(cell));

        scanf("%d", &head->x);

        if (head->x == 0)return NULL;

        head->next = NULL;

        tmp = head;

        while (head->x != 0) {

            scanf("%d", &n);

            if (n == 0)break;

            else {

                p = (cell*)malloc(sizeof(cell));

                p->x = n;

                tmp->next = p;

                tmp = p;

                p->next = NULL;

            }

        }

        return head;//返回单链表头

    }

    void print(struct cell* head) {//打印整个单链表,head是指向单链表首结点的指针

        struct cell* p;

        p = head;

        do

        {

            printf("%d", p->x);

            if (p->next != NULL)printf(" ");

            p = p->next;

        } while (p!=NULL);

    }

    void release(struct cell* head) {//释放单链表空间,head是指向单链表首结点的指针

        struct cell* temp = head;

        while (temp != NULL) {

            struct cell* pt = temp;

            temp = temp->next;

            free(pt);

        }

    }

    int main(void) {

        struct cell* head;

        head = build();

        if (head != NULL)

            print(head);

        else

            printf("NULL");

        release(head);

    }


6. (程序题)

题目编号:Exp09-Basic02,GJBook3-13-06

题目名称:删除单链表重复结点

题目描述:请填写缺失代码完成程序,实现如下功能:

首先根据键盘随机输入,以0结束的若干非零整数建立单链表;然后删除此链表中值重复的结点仅保留一个,且不改变原有结点顺序;最后将删除后链表中各结点值输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符;若是空链表,则输出NULL。

例如,

输入:5 4 2 1 3 0 输出:5 4 2 1 3

输入: 4 2 1 3 3 2 0 输出:4 2 1 3

输入: 0 4 2 3 2 0 输出:NULL

***注意***:

提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。

#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 /*请在以下位置补充完整,实现函数build的功能
       ......
       ......
       ......
   */
 return head;//返回单链表头
}
struct cell* del2one(struct cell* head) {//删除重复结点只保留一个,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数del2one的功能
       ......
       ......
       ......
   */
 return head;//返回删除重复结点的单链表头
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 struct cell* head;
 head = build();
 head=del2one(head);
 if(head!=NULL)
        print(head);
    else
        printf("NULL");
 release(head);
}

#include <stdio.h>

#include <malloc.h>

struct cell {//单链表结点结构体定义

    int x;

    struct cell* next;

};

struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回

    struct cell* head, * tmp, * p;

    head = tmp = p = NULL;

    int n;


    head = (cell*)malloc(sizeof(cell));

    head->next = NULL;

    tmp = head;


    do {

        scanf("%d", &n);

        if (n == 0)break;

        else {

            p = (cell*)malloc(sizeof(cell));

            p->x = n;

            tmp->next = p;

            p->next = NULL;

            tmp = p;

        }

    } while (n != 0);


    if (head->next == NULL) {

        free(head);

        head = NULL;

    }

    return head;//返回单链表头

}

struct cell* del2one(struct cell* head) {//删除重复结点只保留一个,head是单链表首结点指针

    struct cell* p, * temp, * cur;

    p = head;

    while (p != NULL) {

        temp = p;

        while (temp->next != NULL) {

            if (temp->next->x == p->x) {

                cur = temp->next;

                temp->next = cur->next;

                free(cur);

            }

            else {

                temp = temp->next;

            }

        }

        p = p->next;

    }

    return head;//返回删除重复结点的单链表头

}

void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针

    struct cell* p;

    p = head->next;

    while (p != NULL) {

        printf("%d", p->x);

        if (p->next != NULL)printf(" ");

        p = p->next;

    }

}

void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针

    struct cell* temp = head;

    while (temp != NULL) {

        struct cell* pt = temp;

        temp = temp->next;

        free(pt);

    }

}

int main(void) {

    struct cell* head;

    head = build();

    head = del2one(head);

    if (head != NULL)

        print(head);

    else

        printf("NULL");

    release(head);

}

7. (程序题)

题目编号 :Exp09-Basic05,GJBook3例-13-04

题目名称:单链表存储法雷序列

题目描述:请填写缺失代码完成程序,实现如下功能:

给定一个正整数n,用单链表递增存储n阶法雷序列各项值。n阶法雷序列是把所有不可约分的分数j/i(0<i≤n; 0≤j≤i)递增排序的序列。

输入一个正整数n;输出n阶法雷序列各项分数形式,分数的分子和分母间以/连接,各个分数间以一个西文空格间隔,最后一个数字后无任何字符。若是空链表或n不符合要求,则输出NULL。

例如,

输入:3 

输出:0/1 1/3 1/2 2/3 1/1

***注意***:

提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。


 

#include <stdio.h>
#include <malloc.h>
struct  farlei_item {
 int   numerator, denominator;   // 分子、分母
 struct  farlei_item* next;   // 连接部分
};
typedef  struct  farlei_item* farleipointer;
int  gcd(int x, int y) {    /*  求最大公约数 */
 /*请在以下位置补充完整,实现函数gcd的功能
       ......
       ......
       ......
   */
}
/*构造法雷序列,并返回序列头指针*/
farleipointer farlei(int n) {
 int i, j;
 farleipointer fn, r, r0, p;
 fn = r = r0 = p = NULL;
 if (n < 1)return NULL; //如果n<=0,则没有法雷序列
 fn = (farleipointer)malloc(sizeof(struct farlei_item));  //构造0/1
 fn->numerator = 0;
 fn->denominator = 1;
 p = (farleipointer)malloc(sizeof(struct farlei_item));   //构造1/1
 p->numerator = 1;
 p->denominator = 1;
 fn->next = p;
 p->next = NULL;
 /*请在以下位置补充完整,实现函数farlei的功能
       ......
       ......
       ......
   */
 return fn;
}
void print(farleipointer fn) {//输出fn引导的法雷序列
 /*请在以下位置补充完整,实现函数print的功能
       ......
       ......
       ......
   */
}
void release(farleipointer head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能
       ......
       ......
       ......
   */
}
int main(void) {
 int n;
 farleipointer fn;
 scanf("%d", &n);
 fn = farlei(n); //生成n级法雷序列
 if(fn!=NULL)
        print(fn);
    else
        printf("NULL");
 release(fn);
 return 0;
}
    #include <stdio.h>

    #include <malloc.h>

    struct  farlei_item {

        int   numerator, denominator;   // 分子、分母

        struct  farlei_item* next;   // 连接部分

    };

    typedef  struct  farlei_item* farleipointer;


    int gcd(int x, int y) {    /*  求最大公约数 */

        if (y == 0)return x;

        else return gcd(y, x % y);

    }


    /*构造法雷序列,并返回序列头指针*/

    farleipointer farlei(int n) {

        int i, j;

        farleipointer fn, r, r0, p;

        fn = r = r0 = p = NULL;

        if (n < 1)return NULL; //如果n<=0,则没有法雷序列

        fn = (farleipointer)malloc(sizeof(struct farlei_item));  //构造0/1

        fn->numerator = 0;

        fn->denominator = 1;

        p = (farleipointer)malloc(sizeof(struct farlei_item));   //构造1/1

        p->numerator = 1;

        p->denominator = 1;

        fn->next = p;

        p->next = NULL;

        for (i = 2; i <= n; i++) {

            for (j = 1; j < i; j++) {

                if (gcd(i, j) == 1) {

                    r = fn;

                    while (j * (r->denominator) > i * (r->numerator)) {

                        r0 = r;

                        r = r->next;

                    }

                    p = (farleipointer)malloc(sizeof(struct farlei_item));

                    p->numerator = j;

                    p->denominator = i;

                    p->next = r;

                    r0->next = p;

                }

            }

        }


        return fn;

    }

    void print(farleipointer fn) {//输出fn引导的法雷序列


        farleipointer temp;

        temp = fn;

        while (temp != NULL) {

            printf("%d/%d", temp->numerator,temp->denominator);

            if (temp->next != NULL)printf(" ");

            temp = temp->next;

        }

    }

    void release(farleipointer head) {//释放单链表空间,head是单链表首结点指针

        farleipointer tmp, p;

        tmp = head;

        while (tmp != NULL) {

            p = tmp;

            tmp = tmp->next;

            free(p);

        }

    }

    int main(void) {

        int n;

        farleipointer fn;

        scanf("%d", &n);

        fn = farlei(n); //生成n级法雷序列

        if (fn != NULL)

            print(fn);

        else

            printf("NULL");

        release(fn);

        return 0;

    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值