【无标题】

摘自子墨的博客

1.给定一个链表,每个链表中的结点包括学号、成绩。在其中查找某个学号的学生结点,将其成绩替换成指定的新成绩
.

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

struct Student {
    int num;
    float score;
    struct Student *next;
};

struct List {
    struct Student *head;
    struct Student *tail;
};

struct List *create() {
    struct Student *vh = (struct Student *) malloc(sizeof(struct Student));
    vh->next = NULL;

    struct List *res = (struct List *) malloc(sizeof(struct List));
    res->head = vh;
    res->tail = vh;
    return res;
}

void insert(struct List *list, int num, float score) {
    struct Student *node = (struct Student *) malloc(sizeof(struct Student));
    node->num = num;
    node->score = score;
    node->next = NULL;

    list->tail->next = node;
    list->tail = list->tail->next;
}

void print(struct List *list) {
    struct Student *cur = list->head->next;
    while (cur) {
        printf("%d %.2f\n", cur->num, cur->score);
        cur = cur->next;
    }
    printf("\n");
}

struct Student *find(struct List *list, int num) {
    struct Student *cur = list->head->next;
    while (cur != NULL) {
        if (cur->num == num) {
            return cur;
        }
        cur = cur->next;
    }
    return NULL;
}

void modify(struct List *list, int num, float score) {
    struct Student *node = find(list, num);
    if (node) {
        node->score = score;
    }
}

int main(void) {
    struct List *list = create();
    insert(list, 1, 100);
    insert(list, 2, 90);
    insert(list, 3, 65);
    insert(list, 4, 59);
    print(list);
    modify(list, 4, 60);
    print(list);
    return 0;
}

2.有10个学生的信息,包括学号、姓名、年龄,组成结构体数组。将该数组的10个学生数据读出形成链表
.

#include <stdio.h>

#define N 3
struct Student {
    int num;
    char name[10];
    int age;
    struct Student *next;
} ss[N];


int main(void) {
    int i;
    for (i = 0; i < N; i++) {
        scanf("%d %s %d", &ss[i].num, ss[i].name, &ss[i].age);
    }
    struct Student *head = &ss[0];
    for (i = 1; i < N; i++) {
        ss[i - 1].next = &ss[i];
    }
    struct Student *cur = head;
    while (cur) {
        printf("%d %s %d\n", cur->num, cur->name, cur->age);
        cur = cur->next;
    }
    return 0;
}

3.给定两个链表,每个链表中的结点包括学号、成绩。求两个链表的交集
.

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

struct Student {
    int num;
    float score;
    struct Student *next;
};

struct List {
    struct Student *head;
    struct Student *tail;
};

struct List *create() {
    struct Student *vh = (struct Student *) malloc(sizeof(struct Student));
    vh->next = NULL;

    struct List *res = (struct List *) malloc(sizeof(struct List));
    res->head = vh;
    res->tail = vh;
    return res;
}

void insert(struct List *list, int num, float score) {
    struct Student *node = (struct Student *) malloc(sizeof(struct Student));
    node->num = num;
    node->score = score;
    node->next = NULL;

    list->tail->next = node;
    list->tail = list->tail->next;
}

void print(struct List *list) {
    struct Student *cur = list->head->next;
    while (cur) {
        printf("%d %.2f\n", cur->num, cur->score);
        cur = cur->next;
    }
    printf("\n");
}

struct List *jiaoji(struct List *l1, struct List *l2) {
    struct List *res = create();
    struct Student *i = l1->head->next;
    while (i) {
        struct Student *j = l2->head->next;
        while (j) {
            if (i->num == j->num) {
                insert(res, i->num, i->score);
                break;
            }
            j = j->next;
        }
        i = i->next;
    }
    return res;
}


int main(void) {
    struct List *list = create();
    insert(list, 1, 100);
    insert(list, 2, 90);
    insert(list, 3, 65);
    insert(list, 4, 59);

    struct List *l2 = create();
    insert(l2, 1, 100);
    insert(l2, 3, 65);
    insert(l2, 5, 59);

    struct List *l3 = jiaoji(list, l2);
    print(l3);
    return 0;
}

4.有10个学生的信息,包括学号、姓名、年龄,组成结构体数组。将该数组的10个学生数据读出形成链表
.

#include <stdio.h>

#define N 3
struct Student {
    int num;
    char name[10];
    int age;
    struct Student *next;
} ss[N];


int main(void) {
    int i;
    for (i = 0; i < N; i++) {
        scanf("%d %s %d", &ss[i].num, ss[i].name, &ss[i].age);
    }
    struct Student *head = &ss[0];
    for (i = 1; i < N; i++) {
        ss[i - 1].next = &ss[i];
    }
    struct Student *cur = head;
    while (cur) {
        printf("%d %s %d\n", cur->num, cur->name, cur->age);
        cur = cur->next;
    }
    return 0;
}

5.给定两个链表a与b,每个链表中的结点包括学号、成绩。要求从a链表中删除与b链表有相同学号的结点
.

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

struct Student {
    int num;
    float score;
    struct Student *next;
};

struct List {
    struct Student *head;
    struct Student *tail;
};

struct List *create() {
    struct Student *vh = (struct Student *) malloc(sizeof(struct Student));
    vh->next = NULL;

    struct List *res = (struct List *) malloc(sizeof(struct List));
    res->head = vh;
    res->tail = vh;
    return res;
}

void insert(struct List *list, int num, float score) {
    struct Student *node = (struct Student *)malloc(sizeof(struct Student));
    node->num = num;
    node->score = score;
    node->next = NULL;

    list->tail->next = node;
    list->tail = list->tail->next;
}

void print(struct List *list) {
    struct Student *cur = list->head->next;
    while (cur) {
        printf("%d %.2f\n", cur->num, cur->score);
        cur = cur->next;
    }
    printf("\n");
}

void delete(struct List *list, int num) {
    struct Student *last = list->head;
    struct Student *cur = last->next;
    while (cur) {
        if (cur->num == num) {
            last->next = cur->next;
            cur->next = NULL;
            free(cur);
            break;
        }
        last = last->next;
        cur = cur->next;
    }
}

void del(struct List *l1, struct List *l2) {
    struct Student *i = l1->head->next;
    while (i) {
        struct Student *t = i->next;
        struct Student *j = l2->head->next;
        while (j) {
            if (i->num == j->num) {
                delete(l1, i->num);
                break;
            }
            j = j->next;
        }
        i = t;
    }
}

int main(void) {
    struct List *list = create();
    insert(list, 1, 100);
    insert(list, 2, 90);
    insert(list, 3, 65);
    insert(list, 4, 59);

    struct List *l2 = create();
    insert(l2, 1, 100);
    insert(l2, 3, 65);
    insert(l2, 5, 59);

    del(list, l2);
    print(list);
    return 0;
}

6.给定两个链表,每个链表中的结点包括学号、成绩,并均为学号升序排列。求两个链表的并集,并集的结果仍按学号升序排列
.

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

struct Student {
    int num;
    float score;
    struct Student *next;
};

struct List {
    struct Student *head;
    struct Student *tail;
};

struct List *create() {
    struct Student *vh = (struct Student *) malloc(sizeof(struct Student));
    vh->next = NULL;

    struct List *res = (struct List *) malloc(sizeof(struct List));
    res->head = vh;
    res->tail = vh;
    return res;
}

void insert(struct List *list, int num, float score) {
    struct Student *node = (struct Student *) malloc(sizeof(struct Student));
    node->num = num;
    node->score = score;
    node->next = NULL;

    list->tail->next = node;
    list->tail = list->tail->next;
}

void print(struct List *list) {
    struct Student *cur = list->head->next;
    while (cur) {
        printf("%d %.2f\n", cur->num, cur->score);
        cur = cur->next;
    }
    printf("\n");
}

struct List *bingji(struct List *l1, struct List *l2) {
    struct List *res = create();
    struct Student *i = l1->head->next;
    struct Student *j = l2->head->next;
    while (i && j) {
        if (i->num < j->num) {
            insert(res, i->num, i->score);
            i = i->next;
        } else {
            insert(res, j->num, j->score);
            j = j->next;
        }
    }
    while (i) {
        insert(res, i->num, i->score);
        i = i->next;
    }
    while (j) {
        insert(res, j->num, j->score);
        j = j->next;
    }
    return res;
}

int main(void) {
    struct List *list = create();
    insert(list, 2, 100);
    insert(list, 4, 90);
    insert(list, 6, 65);
    insert(list, 8, 59);

    struct List *l2 = create();
    insert(l2, 1, 100);
    insert(l2, 3, 65);
    insert(l2, 5, 59);

    struct List *l3 = bingji(list, l2);
    print(l3);
    return 0;
}

10人围成一圈,并从1到10依次分配编号。从编号为1的人开始依次报数1,2,3…报3的人退出,余下的人继续从1开始依次报数,到3退圈。当最后一人留在圈时求其原来的编号
.

#include <stdio.h>

#define N 10
#define M 3

int main(void) {
    int a[N];
    int i, j = 1, cnt = 0;
    for (i = 0; i < N; i++) {
        a[i] = i + 1;
    }
    i = 0;
    while (cnt < N - 1) {
        i = i % 10;
        while (a[i] == 0) {
            i++;
            i = i % 10;
        }
        a[i] = j;
        j++;
        if (a[i] == M) {
            a[i] = 0;
            j = 1;
            cnt++;
        }
        i++;
    }
    for (i = 0; i < N; i++) {
        if (a[i]) {
            printf("%d", i + 1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值