初识链表:学校oj的题目

Problem A: 实验11_4_初识链表
Time Limit: 1 Sec Memory Limit: 128 MB
Description

已知一个正整数序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后统计这个序列中元素的最大值与最小值,计算序列全部元素之和。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。
Input

一个正整数序列,正整数序列元素的个数未知,但以输入“-1”结束,输入“-1”前至少输入一个正整数。序列中的元素范围在1—999999999之间。
Output

三个正整数,即最大值、最小值、所有元素之和。
数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。
请注意输入输出格式。

Sample Input Copy

1 4 99 21 50 61 32 4 -1

Sample Output Copy

The maximum,minmum and the total are:99 1 272

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;
Node *head, *p, *r;
int main()
{
    int x, maxn, minn;
    long long tot = 0;
    scanf("%d", &x);
    maxn = minn = x;
    head = (Node *)malloc(sizeof(Node));
    r = head;
    while(x!=-1)
    {
        p = (Node *)malloc(sizeof(Node));
        p -> data = x;
        p -> next = NULL;
        r -> next = p;
        r = p;
        if(x>maxn) maxn = x;
        if(x<minn) minn = x;
        tot += x;
        scanf("%d", &x);
    }   
    printf("The maximum,minmum and the total are:%d %d %lld\n", maxn, minn, tot);
    return 0;
}

Problem B: 实验11_10_链表排序
Time Limit: 1 Sec Memory Limit: 128 MB
Description

已知一个正整数组成的无序序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后将这个链表进行排序,使得排序后的链表为递增序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在排序的过程中,你可以自己选择排序算法(冒泡排序、选择排序等),但必须是通过修改结点的指针域来进行排序,而不是对结点的数据域进行修改。程序结束后要释放所有节点占据的空间。
Input

一个元素个数未知的正整数序列,以输入“-1”结束,输入“-1”前至少输入一个正整数。
Output

经过排序后的链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
Sample Input Copy

49 38 65 97 76 13 27 49 -1
Sample Output Copy

The new list is:13 27 38 49 49 65 76 97

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;
Node *head, *p, *r;
int build_list();
void print();
void bubble_sort(int num);
void free_list();
int main()
{
    int num;
    head = (Node *)malloc(sizeof(Node));
    r = head;
    num = build_list();
    bubble_sort(num);
    print();
    free_list();
    return 0;
}
int build_list()
{
    int x, num = 0;    
    scanf("%d", &x);
    while(x!=-1)
    {
        num ++;
        p = (Node *)malloc(sizeof(Node));
        p -> data = x;
        p -> next = NULL;
        r -> next = p;
        r = p;
        scanf("%d", &x);
    }
    return num;
}

void bubble_sort(int num)
{
    int i, j;
    Node *front, *before, * back;  
    for(i=1;i<num;i++)
    {
        before = head;
        front = head -> next;
        back = front -> next;
        for(j=1;j<=num-i;j++)
        {
            if(front -> data > back -> data)
            {
                before -> next = back;
                front -> next = back -> next;
                back -> next = front;
            }
            before = before -> next;
            front = before -> next;
            back = front -> next;
        }
    }
    return ;
}

void print()
{
    Node * tem;
    tem = head -> next;
    printf("The new list is:");
    while(tem != NULL)
    {
        printf("%d ", tem->data);
        tem = tem -> next;
    }
    printf("\n");
    return ;
}

void free_list()
{
    p = head;
    while(p != NULL)
    {
        r = p -> next;
        free(p);
        p = r;
    }
    return ;
}

Problem C: 实验11_11_链表匹配
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 393 Solved: 144
[Submit] [Status] [Creator:admin]
Description

已知两个由正整数组成的无序序列A、B,每个序列的元素个数未知,但至少有一个元素。你的任务是判断序列B是否是序列A的连续子序列。假设B是“1 9 2 4 18”,A是“33 64 1 9 2 4 18 7”,B是A的连续子序列;假设B是“1 9 2 4 18”,A是“33 1 9 64 2 4 18 7”,B不是A的连续子序列。
要求:
建立两个单链表A、B用于存储两个正整数序列,然后按照题目的要求,判断链表B是否是链表A的连续子序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在程序结束前要释放链表A、B中的所有节点。
Input

依次输入两个乱序的正整数序列A、B,序列中元素个数未知,但每个序列至少有一个元素,并以输入“-1”结束,每个序列占一行。
Output

如果序列B是序列A的连续子序列,则输出“ListB is the sub sequence of ListA.”,否则输出“ListB is not the sub sequence of ListA.”。
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
Sample Input Copy

Sample 1:
5 4 3 2 1 -1
3 2 1 -1

Sample 2:
1 2 3 4 5 6 7 8 9 -1
1 2 3 4 5 6 7 8 0 -1
Sample Output Copy

Sample 1:
ListB is the sub sequence of ListA.

Sample 2:
ListB is not the sub sequence of ListA.

附一句:这道题真的搞死我…
链表只是数据结构,算法为字符串匹配,我一开始是找到第一个符合的元素然后向后一一匹配,后来发现还有很多的其他情况… T^T

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;
Node *head[3], *p, *r, *pa, *pb;
int num[3];

int build_list();
void free_list(int num);
void judge();
int main()
{
    int i;
    head[0] = NULL;
    for(i=1;i<=2;i++)
    {
        head[i] = (Node *)malloc(sizeof(Node));
        r = head[i];
        num[i] = build_list();
    }
    pa = head[1] -> next;
    pb = head[2] -> next;
    judge();
    free_list(1);
    free_list(2);
    return 0;
}

int build_list()
{
    int x, num = 0;
    scanf("%d", &x);
    while(x!=-1)
    {
        num ++;
        p = (Node *)malloc(sizeof(Node));
        p -> data = x;
        p -> next = NULL;
        r -> next = p;
        r = p;
        scanf("%d", &x);
    }
    return num;
}

void free_list(int num)
{
    p = head[num];
    while(p!=NULL)
    {
        r = p -> next;
        free(p);
        p = r;
    }
    return ;
}

void judge()
{
    int flag = 0;
    Node *store_loc = head[1];
    while(pa!=NULL && !flag)
    {
        pa = store_loc -> next;
        pb = head[2] -> next;
        while(pa!=NULL && pa->data !=  pb -> data)
            pa = pa -> next;//找到与第一个元素匹配的a的位置
        if(pa!=NULL) store_loc = pa;
        while(pa!=NULL && pb!= NULL && pa->data == pb->data)
        {
            pa = pa -> next;
            pb = pb -> next;
        }
        if(pb==NULL) flag = 1;//如果遍历到b序列的末尾,就说明是子序列
    }
    if(!flag) printf("ListB is not the sub sequence of ListA.\n");
    else printf("ListB is the sub sequence of ListA.\n");
    return ;
}

Problem D: 实验11_13_链表交换
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 222 Solved: 107
[Submit] [Status] [Creator:admin]
Description

已知一个正整数序列,序列元素个数未知,但至少有两个元素,你的任务是建立一个单链表用于存储这个正整数序列。然后实现交换此链表中任意指定的两段,第一段为[s1,t1],第二段[s2,t2]。s1、t1、s2、t2代表链表的第几个节点,且满足s1<=t1,s2<=t2,t1<s2,s2一定小于等于链表节点的总个数。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。最后将链表的全部节点释放。
Input

输入一个正整数序列,以输入“-1”结束,序列中元素个数未知,但输入“-1”前至少输入两个正整数。然后是四个整数,即为s1、t1、s2、t2。
Output

经过处理后的新链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
Sample Input Copy

1 2 3 4 5 6 7 8 9 10 -1
1 1 4 7
Sample Output Copy

The new list is:4 5 6 7 2 3 1 8 9 10

这道题要分两种情况讨论(或者是有更好的办法但我没有发现)
交换的两块是否相连会对操作方式产生影响:

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;
Node *head, *p, *r;
int num = 0, s1, t1, s2, t2;

Node *findloction(int loc);
void exchange();
void build_list();
void free_list();
void print_list(); 
int main()
{
    build_list();//建立链表
    scanf("%d %d %d %d", &s1, &t1, &s2, &t2);
    exchange();//进行交换
    print_list();//打印链表
    free_list();//free链表
    return 0;
}

void build_list()
{
    int x;
    head = (Node *)malloc(sizeof(Node));
    r = head;
    scanf("%d", &x);
    while(x!=-1)
    {
        num ++;
        p = (Node *)malloc(sizeof(Node));
        p -> data = x;
        p -> next = NULL;
        r -> next = p;
        r = p;
        scanf("%d", &x);
    }
    return ;
}

void free_list()
{
    p = head;
    while(p!=NULL)
    {
        r = p -> next;
        free(p);
        p = r;
    }
    return ;
}

void exchange()
{
    Node *before1, *before2, *front1, *front2, *back1, *back2, *tem;
    before1 = findloction(s1-1);
    before2 = findloction(s2-1);
    front1 = before1 -> next;
    front2 = before2 -> next;
    back1 = findloction(t1);
    back2 = findloction(t2);
    if(s2-t1-1)//如果两部分不相连
    {
        tem = back2 -> next;
        before1 -> next = front2;
        back2 -> next = back1 -> next;
        before2 ->next = front1;
        back1 -> next = tem;
    }
    else//反之如果两部分相连
    {
        tem = back2 -> next;
        before1 -> next = front2;        
        back2 ->next = front1;
        back1 ->next = tem;
    }
    return ;
}

Node *findloction(int loc)//这个函数用来找第i个节点所在的地址
{
    Node *tem = head;
    int count = 0;
    while(count < loc)
    {
        tem = tem -> next;
        count ++;
    }
    return tem;
}

void print_list()
{
    printf("The new list is:");
    p = head -> next;
    while(p!=NULL)
    {
        printf("%d ", p ->data);
        p = p -> next;
    }
    printf("\n");
    return ;
}

完结撒花🎉~

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值