摘自子墨的博客
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;
}