SDUT PTA 链表

**如果不要求非得写链表(或者链表函数)的话,可以用数组模拟链表hhh,代码将会非常简单。

7-1 数据结构实验之链表一:顺序建立链表 (20 分)

输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。

输入格式:

第一行输入整数的个数N(1 <= N <= 100000)。

第二行依次输入每个整数。

输出格式:

输出这组整数。

输入样例:

8
12 56 4 6 55 15 33 62

输出样例:

12 56 4 6 55 15 33 62
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node*next;
};
int main()
{
    struct node *h, *t, *p;
    h = (struct node*)malloc(sizeof(struct node));
    h->next = NULL;
    t = h;
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = NULL;
        t->next = p;
        t = p;
    }
    p = h->next;
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

7-2 数据结构实验之链表二:逆序建立链表 (20 分)

输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。

输入格式:

第一行输入整数N(1<=N<=100000)。

第二行依次输入N个整数,逆序建立单链表。

输出格式:

依次输出单链表所存放的数据。

输入样例:

10
11 3 5 27 9 12 43 16 84 22

输出样例:

22 84 16 43 12 9 27 5 3 11
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};
// 头插法建立链表
int main()
{
    int n;
    struct node *head, *p;
    head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = head->next;
        head->next = p;
    }
    p = head->next;
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

7-3 数据结构实验之链表三:链表的逆置 (20 分)

输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。

输入格式:

输入多个整数,以-1作为结束标志。(整数个数不超过100000,不低于1)

输出格式:

输出逆置后的单链表数据。

输入样例:

12 56 4 6 55 15 33 62 -1

输出样例:

62 33 15 55 6 4 56 12

#include<stdio.h>
#include<stdlib.h>
// 头插法
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n;
    struct node *h, *p;
    h = (struct node*)malloc(sizeof(struct node));
    h->next = NULL;
    while(~scanf("%d", &n) && n != -1){
        p = (struct node*)malloc(sizeof(struct node));
        p->data = n;
        p->next = h->next;
        h->next = p;
    }
    p = h->next;
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

7-4 数据结构实验之链表四:有序链表的归并 (20 分)

分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。

输入格式:

第一行输入M与N的值。(1 <= M <=100000, 1 <= N <= 100000)

第二行依次输入M个有序的整数。

第三行依次输入N个有序的整数。

输出格式:

输出合并后的单链表所包含的M+N个有序的整数。

输入样例:

6 5
1 23 26 45 66 99
14 21 28 50 100

输出样例:

1 14 21 23 26 28 45 50 66 99 100
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};
int main()
{
    int m, n;
    scanf("%d%d", &m, &n);
    struct node *h1, *t1, *p1;
    h1 = (struct node*)malloc(sizeof(struct node));
    h1->next = NULL;
    t1 = h1;
    struct node *h2, *t2, *p2;
    h2 = (struct node*)malloc(sizeof(struct node));
    h2->next = NULL;
    t2 = h2;
    for(int i = 0; i < m; i++){
        p1 = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p1->data);
        p1->next = NULL;
        t1->next = p1;
        t1 = p1;
    }
    for(int i = 0; i < n; i++){
        p2 = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p2->data);
        p2->next = NULL;
        t2->next = p2;
        t2 = p2;
    }
    struct node *h, *t, *p;
    h = h1;
    p1 = h1->next;
    p2 = h2->next;
    free(h2);
    t = h1;
    while(p1 && p2){
        if(p1->data < p2->data){
            t->next = p1;
            t = p1;
            p1 = p1->next;
        }
        else {
            t->next = p2;
            t = p2;
            p2 = p2->next;
        }
        if(p1 == 0) t->next = p2;
        else t->next = p1;
    }
    p = h->next;
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

7-5 数据结构实验之链表五:单链表的拆分 (20 分)

输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。

输入格式:

第一行输入整数N(1<=N<=100000)。

第二行依次输入N个整数。 (保证奇数偶数都存在)

输出格式:

第一行分别输出偶数链表与奇数链表的元素个数;

第二行依次输出偶数子链表的所有数据;

第三行依次输出奇数子链表的所有数据。

输入样例:

10
1 3 22 8 15 999 9 44 6 1001

输出样例:

4 6
22 8 44 6
1 3 15 999 9 1001

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

struct node{
    int data;
    struct node *next;
};
int main()
{
    int n, x;
    int cnt1 = 0, cnt2 = 0;
    struct node *h1, *t1, *p1;
    h1 = (struct node*)malloc(sizeof(struct node));
    h1->next = NULL;
    t1 = h1;
    struct node *h2, *t2, *p2;
    h2 = (struct node*)malloc(sizeof(struct node));
    h2->next = NULL;
    t2 = h2;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        p1 = (struct node*)malloc(sizeof(struct node));
        p2 = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &x);
        if(x % 2 == 0){
            p2->data = x;
            p2->next = NULL;
            t2->next = p2;
            t2 = p2;
            cnt2 ++;
        }
        else {
            p1->data = x;
            p1->next = NULL;
            t1->next = p1;
            t1 = p1;
            cnt1++;
        }
    }
    printf("%d %d\n", cnt2, cnt1);
    p2 = h2->next;
    while(p2){
        if(p2->next == NULL) printf("%d\n", p2->data);
        else printf("%d ", p2->data);
        p2 = p2->next;
    }
    p1 = h1->next;
    while(p1){
        if(p1->next == NULL) printf("%d\n", p1->data);
        else printf("%d ", p1->data);
        p1 = p1->next;
    }
    return 0;
}

7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)

按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。

输入格式:

第一行输入元素个数 n (1 <= n <= 15);

第二行输入 n 个整数,保证在 int 范围内。

输出格式:

第一行输出初始链表元素个数;

第二行输出按照逆位序所建立的初始链表;

第三行输出删除重复元素后的单链表元素个数;

第四行输出删除重复元素后的单链表。

输入样例:

10
21 30 14 55 32 63 11 30 55 30

输出样例:

10
30 55 30 11 63 32 55 14 30 21
7
30 55 11 63 32 14 21

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

struct node{
    int data;
    struct node *next;
};
int main()
{
    int n;
    struct node *h, *p;
    h = (struct node*)malloc(sizeof(struct node));
    h->next = NULL;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = h->next;
        h->next = p;
    }
    p = h->next;
    printf("%d\n", n);
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    struct node *q, *w;
    p = h->next;// 链表的第一个元素
    while(p){
        q = p;
        w = q->next;// w是游动指针,指向下一个
        while(w){// 没遍历完整个链表时
            // 让一个数与后面的所有数比较(除了第一个数,每个数都得来一次)  类似于冒泡排序
            if(w->data == p->data){// 若后一个与前一个相等
                q->next = w->next;// 跳过w->data
                w = w->next;
                n--;// 把w->data删掉了
            } else{
                q = w;
                w = w->next;
            }// 无论如何,w都是要指向下一个
        }
        p = p->next;// 换到下一个数字与其后面的各个数字比较
    }
    printf("%d\n", n);
    p = h->next;
    while(p){
        if(p->next == NULL) printf("%d\n", p->data);
        else printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

7-7 师--链表的结点插入 (20 分)

给出一个只有头指针的链表和 n 次操作,每次操作为在链表的第 m 个元素后面插入一个新元素x。若m 大于链表的元素总数则将x放在链表的最后。

输入格式:

多组输入。

每组数据首先输入一个整数n(1<=n<=100),代表有n次操作。

接下来的n行,每行有两个整数Mi(0<=Mi<=10000),Xi。

输出格式:

对于每组数据。从前到后输出链表的所有元素,两个元素之间用空格隔开。

输入样例:

4
1 1
1 2
0 3
100 4

输出样例:

3 1 2 4

Hint

样例中第一次操作1 1,由于此时链表中没有元素,1>0,所以此时将第一个数据插入到链表的最后,也就是头指针的后面。

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

struct node{
    int data;
    struct node *next;
};
int main()
{
    int n;
    int m, x;
    struct node *h, *p, *q;
    while(~scanf("%d", &n)){
        h = (struct node*)malloc(sizeof(struct node));
        h->next = NULL;
        for(int i = 0; i < n; i++){
            p = (struct node*)malloc(sizeof(struct node));
            scanf("%d%d", &m, &x);
            q = h;
            while(m-- && q->next) q = q->next;// ?? q不为空
            p->data = x;
            p->next = NULL;
            p->next = q->next;
            q->next = p;
        }
        p = h->next;
        while(p){
            if(p->next == NULL) printf("%d\n", p->data);
            else printf("%d ", p->data);
            p = p->next;
        }
    }
    return 0;
}

7-8 约瑟夫问题 (20 分)

n个人想玩残酷的死亡游戏,游戏规则如下:

n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。

请输出最后一个人的编号。

输入格式:

输入n和m值。 (1<=n<=100 , 1<=m<=100)

输出格式:

输出胜利者的编号。

输入样例:

5 3

输出样例:

4

Hint

第一轮:3被杀

第二轮:1被杀

第三轮:5被杀

第四轮:2被杀

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

struct node
{
    int num;
    struct node *next;
};
// 顺序创建链表(尾插法)
struct node *creat(int n)
{
    struct node *h, *p, *t;
    p = (struct node*)malloc(sizeof(struct node));
    p->num = 1;
    p->next = NULL;
    t = h = p;
    for(int i = 2; i <= n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        p->num = i;
        p->next = NULL;
        t->next = p;
        t = p;
    }
    t->next = h;// 形成闭环
    return h;
}
int del(struct node *h, int n, int m)
{
    int num = 0;
    struct node *p, *q;
    q = h;
    while(q->next != h) q = q->next;
    while(q->next != q){
        p = q->next;
        num++;
        if(num % m == 0){
            q->next = p->next;// 跳过p
            free(p);
        }
        else q = p;// 不断向后查找
    }
    return q->num;
}
int main()
{
    int n, m;
    struct node *h;
    scanf("%d%d", &n, &m);
    h = creat(n);
    printf("%d\n", del(h, n, m));
    return 0;
}

**后来得知了一种完全“数学”的方法,贼简单:

#include<stdio.h>
#include<stdlib.h>
int f(int n, int m){
    return n == 1 ? n : (f(n - 1, m) + m - 1) % n + 1;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    printf("%d\n", f(n, m));
    return 0;
}

 7-9 数据结构实验之链表九:双向链表 (20 分)

学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内存中找到下一个位置,这是一个不会轻易断裂的链。但单链表有一个弱点——不能回指。比如在链表中有两个节点A,B,他们的关系是B是A的后继,A指向了B,便能轻易经A找到B,但从B却不能找到A。一个简单的想法便能轻易解决这个问题——建立双向链表。在双向链表中,A有一个指针指向了节点B,同时,B又有一个指向A的指针。这样不仅能从链表头节点的位置遍历整个链表所有节点,也能从链表尾节点开始遍历所有节点。对于给定的一列数据,按照给定的顺序建立双向链表,按照关键字找到相应节点,输出此节点的前驱节点关键字及后继节点关键字。

输入格式:

第一行两个正整数n(代表节点个数),m(代表要找的关键字的个数)。第二行是n个数(n个数没有重复),利用这n个数建立双向链表。接下来有m个关键字,每个占一行。(1<=n<=50)

输出格式:

对给定的每个关键字,输出此关键字前驱节点关键字和后继节点关键字。如果给定的关键字没有前驱或者后继,则不输出。

注意:每个给定关键字的输出占一行。
一行输出的数据之间有一个空格,行首、行末无空格。

输入样例:

10 3
1 2 3 4 5 6 7 8 9 0
3
5
0

输出样例:

2 4
4 6
9

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

struct node
{
    int data;
    struct node *pre, *next;
};
int main()
{
    int n, m, x;
    scanf("%d%d", &n, &m);
    struct node *h, *t, *p;
    h = (struct node*)malloc(sizeof(struct node));
    h->next = NULL;
    t = h;
    for(int i = 0; i < n; i++){
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = NULL;
        t->next = p;
        p->pre = t;
        t = p;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        int k = 0;
        for(p = h->next; p != NULL; p = p->next){
            k++;
            if(k == n && x == p->data) printf("%d\n", p->pre->data);
            else if(k == 1 && x == p->data) printf("%d\n", p->next->data);
            else if(x == p->data) printf("%d %d\n", p->pre->data, p->next->data);
        }
    }
    return 0;
}

7-10 不敢死队问题 (20 分)

说到“敢死队”,大家不要以为我来介绍电影了,因为数据结构里真有这么道程序设计题目,原题如下:

有M个敢死队员要炸掉敌人的一个碉堡,谁都不想去,排长决定用轮回数数的办法来决定哪个战士去执行任务。如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个战士开始计数,当数到5时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第5时,此战士接着去执行任务。以此类推,直到任务完成为止。

这题本来就叫“敢死队”。“谁都不想去”,就这一句我觉得这个问题也只能叫“不敢死队问题”。今天大家就要完成这道不敢死队问题。我们假设排长是1号,按照上面介绍,从一号开始数,数到5的那名战士去执行任务,那么排长是第几个去执行任务的?

输入格式:

输入包括多组数据,每行一个整数M(0<=M<=10000)(敢死队人数),若M=0,输入结束,不做处理。

输出格式:

输出一个整数n,代表排长是第n个去执行任务。

输入样例:

9
6
223
0

输出样例:

2
6
132
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int num;
    struct node *next;
};
struct node *creat(int m){
    struct node *h, *t, *p;
    p = (struct node*)malloc(sizeof(struct node));
    p->num = 1;
    p->next = NULL;
    t = h = p;
    for(int i = 2; i <= m; i++){
        p = (struct node*)malloc(sizeof(struct node));
        p->num = i;
        p->next = NULL;
        t->next = p;
        t = p;
    }
    t->next = h;
    return h;
}
int del(struct node *h, int m, int a){
    int num = 0, cnt = 0;
    struct node *p, *q;
    q = (struct node*)malloc(sizeof(struct node));
    q = h;
    while(q->next != h) q = q->next;
    while(cnt < m - 1){
        p = q->next;
        num++;
        if(num % a == 0){
            if(p->num == 1){
                break;
            }
            else{
                q->next = p->next;
                free(p);
                cnt++;
            }
        }
        else q = p;
    }
    return cnt + 1;
}
int main()
{
    int m;
    while(~scanf("%d", &m) && m){
        struct node *h;
        h = creat(m);
        int x = 5;
        printf("%d\n", del(h, m, x));
    }
    return 0;
}

**后来的后来,我又得知了一种方法:

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n;
    while(scanf("%d", &n) && n){
    int i = 0, m = 5, p, ans = 0;
    while(++i <= n){
        p = i * m;
        while (p > n) 
            p = p - n + (p - n - 1) / (m - 1);
        ans++;
        if(p == 1) printf("%d\n", ans);
    }
 }
    return 0;
}

**别问,问就是amazing~

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值