SDUT PTA 链表

7-1 单链表的创建及遍历

读入n值及n个整数,建立单链表并遍历输出。

输入格式:

读入n及n个整数。

输出格式:

输出n个整数,以空格分隔(最后一个数的后面没有空格)。

输入样例:

在这里给出一组输入。例如:

2
10 5

输出样例:

在这里给出相应的输出。例如:

10 5

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;

struct node{
    int data;
    struct node *next;
};
int main()
{
    int n;
    cin >> n;
    struct node *h, *t, *p;
    h = new node;
    h->next = NULL;
    t = h;
    for(int i = 0; i < n; i++){
        p = new node;
        cin >> p->data;
        p->next = NULL;
        t->next = p;
        t = p;
    }
    p = h->next;
    while(p){
        if(p->next == NULL) cout << p->data;
        else cout << p->data << " ";
        p = p->next;
    }
    return 0;
}

7-2 两个有序链表序列的合并

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 3 5 -1
2 4 6 8 10 -1

输出样例:

1 2 3 4 5 6 8 10

代码长度限制

16 KB

时间限制

1500 ms

内存限制

128 MB

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *h1, *t1, *p1;
    h1 = new node;
    h1->next = NULL;
    t1 = h1;
    struct node *h2, *t2, *p2;
    h2 = new node;
    h2->next = NULL;
    t2 = h2;
    int x;
    while(cin >> x && x != - 1){
        p1 = new node;
        p1->data = x;

        p1->next = NULL;
        t1->next = p1;
        t1 = p1;
    }
    while(cin >> x && x != - 1){
        p2 = new node;
        p2->data = x;

        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;
    }
    if(h->next == NULL) cout << "NULL";
    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-3 两个有序链表序列的交集

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 2 5 -1
2 4 5 8 10 -1

输出样例:

2 5

代码长度限制

16 KB

时间限制

1000 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    struct node *next;
};
// 尾插法创建链表
struct node *creat(){
    struct node *h = NULL, *t, *p;
    h = new node;
    h->next = NULL;
    t = h;
    int x;
    while(cin >> x && x != -1){
        p = new node;
        p->data = x;
        p->next = NULL;
        t->next = p;
        t = p;
    }
    return h -> next;// 不理解
}
int main()
{
    struct node *h1 = creat();
    struct node *h2 = creat();
    int f = 0, num = 0;
    struct node *p1 = h1;
    struct node *p2 = h2;
    while(p1 && p2){
        if(p1->data < p2->data){
            p1 = p1->next;
        }
        else if(p1->data == p2->data){
            num++;
            if(f == 0){
                f = 1;
                printf("%d", p1->data);
            }
            else printf(" %d", p1->data);
            p1 = p1->next;
            p2 = p2->next;
        }
        else{
            p2 = p2->next;
        }
    }
    if(num == 0) printf("NULL");
    return 0;
}

**为什么为什么!!为什么返回h->next???

7-4 约瑟夫环

N个人围成一圈顺序编号,从1号开始按1、2、3......顺序报数,报p者退出圈外,其余的人再从1、2、3开始报数,报p的人再退出圈外,以此类推。
请按退出顺序输出每个退出人的原序号。

输入格式:

输入只有一行,包括一个整数N(1<=N<=3000)及一个整数p(1<=p<=5000)。

输出格式:

按退出顺序输出每个退出人的原序号,数据间以一个空格分隔,但行尾无空格。

输入样例:

在这里给出一组输入。例如:

7 3

输出样例:

3 6 2 7 5 1 4

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int eps = 1e-8;// 一个极小值
const int N = 1e4 + 100;
const int M = 3e3 + 100;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
#define PI acos(-1.0)
#define Equ(a, b) ((fabs((a) - (b))) < (eps))
string a, b;

struct node{
    int num;
    struct node *next;
};
struct node *creat(int n){// 创建链表--尾插法
    struct node *h, *p, *t;
    h = new node;
    t = h;
    h->num = 1;
    for(int i = 2; i <= n; i++){
        p = new node;
        p->num = i;
        p->next = NULL;
        t->next = p;
        t = p;
    }
    t->next = h;// 形成约瑟夫环
    return h;
}
void del(struct node *h, int n, int m){// 本质是删除结点操作
    int num = 0;
    struct node *p, *q;
    q = h;
    while(q->next != h) q = q->next;// 使其从头结点(1号)开始数,此时q是倒数第二个
    while(q->next != q){// 若还没完成一次循环
        p = q->next;// p从头结点往后遍历
        num++;// 模拟向后数
        if(num % m == 0){
            q->next = p->next;// 跳过p结点(即删除
            cout << p->num << " ";
            delete(p);
        }
        else q = p;// 往后遍历
    }
    cout << q->num;
}
int main()
{
    int n, p;
    cin >> n >> p;
    struct node *h;
    h = creat(n);
    del(h, n, p);
    return 0;
}

** 

#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 10;
int num[N];
int main()
{
    int n, p;
    cin >> n >> p;
    for(int i = 0; i < n; i++){
        num[i] = i + 1;
    }
    int t = n, f = 0, cnt = 0;
    while(t){
        for(int i = 0; i < n; i++){
            if(num[i]) cnt++;
            if(cnt == p){
                if(f) cout << " ";
                cout << i + 1;
                num[i] = 0;
                cnt = 0;
                t--;
                f = 1;
            }
        }
    }
    return 0;
}

7-5 链表去重

给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所有被删除的结点须被保存在另一个链表上。例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15。

输入格式:

输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤105,为结点总数)。一个结点的地址是非负的 5 位整数,空地址 NULL 用 -1 来表示。

随后 N 行,每行按以下格式描述一个结点:

地址 键值 下一个结点

其中地址是该结点的地址,键值是绝对值不超过104的整数,下一个结点是下个结点的地址。

输出格式:

首先输出去重后的链表,然后输出被删除的链表。每个结点占一行,按输入的格式输出。

输入样例:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

输出样例:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
// 用数组模拟链表:使用两个一维数组分别存放data和next
struct name{
    int data;
    int next;
}a[N];
int main()
{
    int h, n;// 头结点和数的数量
    int tidx[N];// 未被删除的元素下标
    int fidx[N];// 已被删除的元素的下标
    int jue[N];// 每个数的绝对值
    int t = 0, f = 0;
    cin >> h >> n;
    for(int i = 0; i < n; i++){
        int idx, x, nidx;
        cin >> idx >> x >> nidx;
        a[idx].data = x;
        a[idx].next = nidx;
    }
    if(n == 1) printf("%05d %d %d", h, a[h].data, -1);
    else{
        while(h != -1){
            if(jue[abs(a[h].data)] == 0){
                tidx[t++] = h;
                jue[abs(a[h].data)] = 1;
            }
            else{
                fidx[f++] = h;
            }
            h = a[h].next;
        }
        for(int i = 0; i < t - 1; i++){
            printf("%05d %d %05d\n", tidx[i], a[tidx[i]].data, tidx[i+1]);
        }
        printf("%05d %d %d\n", tidx[t-1], a[tidx[t-1]].data, -1);
        for(int i = 0; i < f - 1; i++){
            printf("%05d %d %05d\n", fidx[i], a[fidx[i]].data, fidx[i+1]);
        }
        printf("%05d %d %d\n", fidx[f-1], a[fidx[f-1]].data, -1);
    }
    return 0;
}

7-6 带头节点的双向循环链表操作

本题目要求读入一系列整数,依次插入到双向循环链表的头部和尾部,然后顺序和逆序输出链表。

链表节点类型可以定义为

typedef int DataType;
typedef struct LinkedNode{
    DataType data;
    struct LinkedNode *prev;
    struct LinkedNode *next;
}LinkedNode;

链表类型可以定义为

typedef struct LinkedList{
  int length; /* 链表的长度 */
  LinkedNode head; /* 双向循环链表的头节点 */
}LinkedList;

初始化链表的函数可声明为

void init_list(LinkedList *list);

分配节点的函数可声明为

LinkedNode *alloc_node(DataType data);

头部插入的函数可声明为

void push_front(LinkedList *list, DataType data);

尾部插入的函数可声明为

void push_back(LinkedList *list, DataType data);

顺序遍历的函数可声明为

void traverse(LinkedList *list);

逆序遍历的函数可声明为

void traverse_back(LinkedList *list);

输入格式:

输入一行整数(空格分隔),以-1结束。

输出格式:

第一行输出链表顺序遍历的结果,第二行输出逆序遍历的结果。

输入样例:

在这里给出一组输入。例如:

1 2 3 4 5 6 -1

输出样例:

5 3 1 2 4 6
6 4 2 1 3 5

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int cnt, a[N];
struct node{
    int data;
    node *pre, *nxt;
};
void traverse(node *h){// 顺序输出
    node *p = h->nxt;
    while(p){
        if(p->nxt) cout << p->data << " ";
        else cout << p->data << endl;
        p = p->nxt;
    }
}
void traverse_back(node *t){// 逆序输出
    node *p = t;
    while(p->pre){
        if(p->pre->pre) cout << p->data << " ";
        else cout << p->data << endl;
        p = p->pre;
    }
}
void creat(struct node *h){// 创建链表
    h = new node;
    h->pre = NULL;
    h->nxt = NULL;
    node *t = h;
    node *p, *q;
    for(int i = 1; i <= cnt; i++){
        q = h->nxt;
        int x = a[i];
        if(!q){// 如果当前链表没有节点
            p = new node;
            p->data = x;
            p->pre = h;
            p->nxt = h->nxt;
            h->nxt = p;
            t = p;
        }// 头插
        else{// 当前链表有节点
            if(i % 2 == 1){// 如果i是奇数,这里和cnt++(或++cnt)必须对应起来
                p = new node;
                p->data = x;
                p->pre = h;
                p->nxt = h->nxt;
                p->nxt->pre = p;
                h->nxt = p;
            }// 头插
            else{
                p = new node;
                p->data = x;
                p->nxt = NULL;
                p->pre = t;
                t->nxt = p;
                t = p;
            }// 尾插
        }
    }
    traverse(h);
    traverse_back(t);
}
int main()
{
    int x;
    while(cin >> x && x != -1) a[++cnt] = x;
    node *h = new node;
    h->nxt = NULL;
    creat(h);
    return 0;
}

7-7 单链表就地逆置

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

输入格式:

首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。每组测试输入多个整数,以-1作为该组测试的结束(-1不处理)。

输出格式:

对于每组测试,输出逆置后的单链表数据(数据之间留一个空格)。

输入样例:

1
1 2 3 4 5 -1

输出样例:

5 4 3 2 1

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int cnt, a[N];
struct node{
    int data;
    struct node *nxt;
};
int main()
{
    int t;
    cin >> t;
    while(t--){
        node *h, *p;
        h = new node;
        h->nxt = NULL;
        int x;
        while(cin >> x && x != -1){
            p = new node;
            p->data = x;
            p->nxt = h->nxt;
            h->nxt = p;
        }
        p = h->nxt;
        while(p){
            if(p->nxt) cout << p->data << " ";
            else cout << p->data << endl;
            p = p->nxt;
        }
        free(h);
    }
    return 0;
}

7-8 重排链表 

给定一个单链表 L1​→L2​→⋯→Ln−1​→Ln​,请编写程序将链表重新排列为 Ln​→L1​→Ln−1​→L2​→⋯。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (≤105)。结点的地址是5位非负整数,NULL地址用−1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过105的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。

输出格式:

对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1

代码长度限制

16 KB

时间限制

500 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
// 重排链表

const int N = 1e5 + 10;
struct node{
    int data, next;
}a[N];
int pre[N];
int main()
{
    int fadd, n;
    cin >> fadd >> n;
    int add;
    while(n--){
        cin >> add;
        cin >> a[add].data >> a[add].next;
    }
    n = 1;
    int loc = fadd;
    while(a[loc].next != -1){
        n++;
        pre[a[loc].next] = loc;// 记录下每个结点的前驱
        loc = a[loc].next;
    }// 记录下结点的真实数目
    int cnt = 0;
    while(1){// 上面的while循环已经让loc指向最后一个结点了
        printf("%05d %d ", loc, a[loc].data);// 输出的就是Ln结点
        cnt++;
        if(cnt == n){
           printf("-1\n"); 
           break;
        } 
        else{
            printf("%05d\n", fadd);
        }
        
        printf("%05d %d ", fadd, a[fadd].data);
        cnt++;
        loc = pre[loc];
        if(cnt == n){
            printf("-1\n");
            break;
        }
        else{
            printf("%05d\n", loc);
        }
        fadd = a[fadd].next;
    }
    return 0;
}

/* 1、坑点:数据中可能有不属于链表的结点。。
   所以要记录下有用结点的数目
    2、读懂题目↓
    在样例中,我们首先将所给序列转换成如下序列:
    00100 1 12309
    12309 2 33218
    33218 3 00000
    00000 4 99999
    99999 5 68237
    68237 6 -1
    然后把它变成↓:
    68237 6 00100
    00100 1 99999
    99999 5 12309
    12309 2 00000
    00000 4 33218
    33218 3 -1
*/

二:

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
struct node{
    int ad, data, next;
}a[N], b[N];
int num[N];
int main()
{
    int fadd, n;
    cin >> fadd >> n;
    int add;
    while(n--){
        cin >> add;
        cin >> a[add].data >> a[add].next;
    }
    int cnt = 1;
    while(fadd != -1){
        b[cnt].ad = fadd;
        b[cnt++].data = a[fadd].data;
        fadd = a[fadd].next;
    }
    for(int i = 1; i < cnt; i++){
        if(i % 2 == 1) a[i] = b[cnt - 1 - i / 2];
        else a[i] = b[i / 2];
    }
    for(int i = 1; i < cnt - 1; i++){
        printf("%05d %d %05d\n", a[i].ad, a[i].data, a[i + 1].ad);
    }
    printf("%05d %d -1\n", a[cnt - 1].ad, a[cnt - 1].data);
    return 0;
}

7-9 头插法创建单链表、遍历链表、删除链表

输入一系列自然数(0和正整数),输入-1时表示输入结束。按照输入的顺序,用头插法建立单链表,并遍历所建立的单链表,输出这些数据。注意 -1 不加入链表。

输入格式:

第一行是一个正整数k,表示以下会有k组测试数据。

每组测试数据是一系列以空格隔开的自然数(0和正整数)。数列末尾的 -1 表示本组测试数据结束。按照输入的顺序,用头插法建立单链表,并遍历所建立的单链表,输出这些数据。注意 -1 不加入链表。

输出格式:

对于每组测试数据,输出链表中各节点的数据域。每个数据后有一个空格。每组测试数据的输出占1行。

输入样例:

3
1 2 3 4 5 -1 
30 20 10 -1 
4 2 2 1 1 2 0 2 -1 

输出样例:

在这里给出相应的输出。例如:

5 4 3 2 1 
10 20 30 
2 0 2 1 1 2 2 4 

注意:对每组测试数据,创建链表,遍历链表输出之后,一定要删除链表,否则会出现“内存超限”。

代码长度限制

16 KB

时间限制

2000 ms

内存限制

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int k;
    cin >> k;
    while(k--){
        struct node *h, *p;
        h = new node;
        h->next = NULL;
        int x;
        while(cin >> x && x != -1){
            p = new node;
            p->data = x;

            p->next = h->next;
            h->next = p;
        }
        p = h->next;
        while(p){
            printf("%d ", p->data);
            p = p->next;
        }
        struct node *q;
        q = h->next;
        while(q){
            free(h);
            h = q;
            q = q->next;
        }// 29~35行:删除整个链表
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值