实验二:函数与程序结构

A.本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。

函数接口定义:
void input();
该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:
struct stud_node {
    int              num;      /*学号*/
    char             name[20]; /*姓名*/
    int              score;    /*成绩*/
    struct stud_node *next;    /*指向下个结点的指针*/
};
单向链表的头尾指针保存在全局变量head和tail中。
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};
struct stud_node *head, *tail;

void input();

int main()
{
    struct stud_node *p;
    
    head = tail = NULL;
    input();
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
输出样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85

/*判断是否带有头结点
本题不带有头结点
创建链表时原测试程序没有给出head和tail所以需要自己声明且定义为NULL
删除时,要判断是删除头还是删除中间
删除头,则头变成原来头的下一个
若删除头后为空,则需直接return NULL
若删除中间部分,则定义两个指针p,ptr一前一后
注意每次循环后都要重新规定p,ptr的先后顺序
*/
#include <stdio.h>
#include <stdlib.h>

struct stud_node
{
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist()
{
    int number = 0;
    //本题与上题不同本题head和tail不是全局变量需要在函数里边单独声明
     struct stud_node *head, *tail;
    //而且必须初始化
     head = tail = NULL;
     while( scanf("%d",&number) && number!=0 )
     {
         struct stud_node *p = (struct stud_node *)malloc(sizeof(struct stud_node ));
         p -> num = number;
         scanf("%s %d",p -> name,&p -> score);
         if(head == NULL)
             head = tail = p;
         else
            {
             tail -> next = p;
             tail = p;
             tail -> next = NULL;
            }
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *p;
    struct stud_node *ptr;
    // 判断第一个节点是不是要删的可能删完一个下一个是新的头又要再删
    // 因为是调用一次函数但是要删掉所有的符合要求的数据所有要用循环
    while(head != NULL)
        {
            if( head -> score < min_score)// 删除头结点
            {
                p = head;
                head = head -> next;
                free(p);
            }
            else
                break;
    }
    if(head == NULL)// 若第一个节点删完之后为空
        return NULL;
    else
    {
        // 定下先后顺序从第二个节点开始判断删除(因为第一个节点已经单独判断过了)
        p = head;// 指针p指向的是前一个节点
        ptr = p -> next;// 指针ptr指向的现在的节点
    }
    while(ptr != NULL)
        {   //从第二个节点开始删除,若不删则再定一下p和ptr的先后顺序
            //(else语句不要忘记)因为ptr总是变的,因此p也要变
            if(ptr -> score < min_score)
            {
                p -> next = ptr -> next;
                free(ptr);
            }
            else
            {
                p = ptr;
            }
            ptr = p -> next;
        }
    return head;
}

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;
    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);
    return 0;
}

 

B.本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

函数接口定义:
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:
struct stud_node {
    int              num;      /*学号*/
    char             name[20]; /*姓名*/
    int              score;    /*成绩*/
    struct stud_node *next;    /*指向下个结点的指针*/
};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;

    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出样例:
2 wang 80
4 zhao 85

/*判断是否带有头结点
本题不带有头结点
创建链表时原测试程序没有给出head和tail所以需要自己声明且定义为NULL
删除时,要判断是删除头还是删除中间
删除头,则头变成原来头的下一个
若删除头后为空,则需直接return NULL
若删除中间部分,则定义两个指针p,ptr一前一后
注意每次循环后都要重新规定p,ptr的先后顺序
*/
#include <stdio.h>
#include <stdlib.h>

struct stud_node
{
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist()
{
    int number = 0;
    //本题与上题不同本题head和tail不是全局变量需要在函数里边单独声明
     struct stud_node *head, *tail;
    //而且必须初始化
     head = tail = NULL;
     while( scanf("%d",&number) && number!=0 )
     {
         struct stud_node *p = (struct stud_node *)malloc(sizeof(struct stud_node ));
         p -> num = number;
         scanf("%s %d",p -> name,&p -> score);
         if(head == NULL)
             head = tail = p;
         else
            {
             tail -> next = p;
             tail = p;
             tail -> next = NULL;
            }
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *p;
    struct stud_node *ptr;
    // 判断第一个节点是不是要删的可能删完一个下一个是新的头又要再删
    // 因为是调用一次函数但是要删掉所有的符合要求的数据所有要用循环
    while(head != NULL)
        {
            if( head -> score < min_score)// 删除头结点
            {
                p = head;
                head = head -> next;
                free(p);
            }
            else
                break;
    }
    if(head == NULL)// 若第一个节点删完之后为空
        return NULL;
    else
    {
        // 定下先后顺序从第二个节点开始判断删除(因为第一个节点已经单独判断过了)
        p = head;// 指针p指向的是前一个节点
        ptr = p -> next;// 指针ptr指向的现在的节点
    }
    while(ptr != NULL)
        {   //从第二个节点开始删除,若不删则再定一下p和ptr的先后顺序
            //(else语句不要忘记)因为ptr总是变的,因此p也要变
            if(ptr -> score < min_score)
            {
                p -> next = ptr -> next;
                free(ptr);
            }
            else
            {
                p = ptr;
            }
            ptr = p -> next;
        }
    return head;
}

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;
    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值