线性表作业汇总

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44056753/article/details/102712697

写在前面

一些题目涉及到多种解法,我会尽量用多种方法解决,如果还有别的方法,欢迎交流,对于一些使用STl的题目,我会先更新使用STL的代码,然后会尽力不使用STL再做一遍,由于时间关系有的题目思路可能写的比较简单

7-1 jmu-ds-顺序表区间元素删除 (15 分)

若一个线性表L采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)。

输入格式:
三行数据,第一行是顺序表的元素个数,第二行是顺序表的元素,第三行是x和y。

输出格式:
删除元素值在[x,y]之间的所有元素后的顺序表。

输入样例:
10
5 1 9 10 67 12 8 33 6 2
3 10

输出样例:
1 67 12 33 2

#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int num[n];
    for(int i=0;i<n;i++)
        cin>>num[i];
    int l,r;
    cin>>l>>r;
    bool flag=false;
    for(int i=0;i<n;i++){
        if(num[i]>r||num[i]<l){
            if(!flag){
                cout<<num[i];
                flag=true;
            }
            else
                cout<<" "<<num[i];
        }
    }
}

7-2 两个有序链表序列的合并 (20 分)

已知两个非降序链表序列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

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
struct node
{
    int num;
    struct node *next;
};
node creatList()
{
    node *head,*r,*p;
    int k;
    head = (node*)malloc(sizeof(node));
    cin>>k;
    head->next=nullptr;
    if(k==-1)
    return *head;
    while(k!=-1)
    {
        p=(node*)malloc(sizeof(node));
        p->num=k;
        p->next==nullptr;
        if(head->next==nullptr)
            head->next=p;
        else
            r->next=p;
        r=p;
        cin>>k;
    }
    r->next=nullptr;
    return *head;
}
void mergeList(node l1,node l2)
{

    node *head,*a,*b,*c;
    head = (node*)malloc(sizeof(node));
    head->next=nullptr;
    b=l1.next;
    c=l2.next;
    while(b!=nullptr&&c!=nullptr)
    {
        if(b!=nullptr&&c!=nullptr)
        {
            if(b->num>c->num)
            {
                if(head->next==nullptr)
                    head->next=c;
                else
                    a->next=c;
                a=c;
                c=c->next;
            }
            else
            {
                if(head->next==nullptr)
                    head->next=b;
                else
                    a->next=b;
                a=b;
                b=b->next;
            }
        }
        else if(b!=nullptr)
        {
            if(head->next==nullptr)
                head->next=b;
            else
                a->next=b;
        }
        else if(c!=nullptr)
        {
            if(head->next==nullptr)
                head->next=c;
            else
                a->next=c;
        }
    }
    if(b)
        a->next=b;
    else if(c)
        a->next=c;
    bool flag=false;
    c=head->next;
    if(c==nullptr)
        cout<<"NULL";
    else
    while(c!=nullptr)
    {
        if(!flag)
        cout<<c->num;
        else
            cout<<" "<<c->num;
        flag=true;
        c=c->next;
    }
    free(head);
    free(c);
}
int main()
{
    node L1,L2,L3,*L;
    L1=creatList();
    L2=creatList();
    mergeList(L1,L2);
}

7-3 两个有序链表序列的交集 (20 分)

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

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

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

输入样例:
1 2 5 -1
2 4 5 8 10 -1
输出样例:
2 5

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
struct node
{
    int num;
    struct node *next;
};
node creatList()
{
    node *head,*r,*p;
    int k;
    head = (node*)malloc(sizeof(node));
    cin>>k;
    head->next=nullptr;
    if(k==-1)
        return *head;
    while(k!=-1)
    {
        p=(node*)malloc(sizeof(node));
        p->num=k;
        p->next==nullptr;
        if(head->next==nullptr)
            head->next=p;
        else
            r->next=p;
        r=p;
        cin>>k;
    }
    r->next=nullptr;
    return *head;
}
void mergeList(node l1,node l2)
{

    node *head,*a,*b,*c;
    head = (node*)malloc(sizeof(node));
    head->next=nullptr;
    a=head;
    b=l1.next;
    c=l2.next;
    while(b!=nullptr&&c!=nullptr)
    {
        if(b->num>c->num)
        {
            while(b->num>c->num&&b!=nullptr&&c!=nullptr)
            {
                c=c->next;
                if(c==nullptr)
                    break;
                if(c->num==b->num)
                {
                    if(head->next==nullptr)
                        head->next=b;
                    else
                        a->next=b;
                    a=b;
                    c=c->next;
                    b=b->next;
                    break;
                }
            }
        }
        else if(c->num==b->num)
        {
            if(head->next==nullptr)
                head->next=b;
            else
                a->next=b;
            a=b;
            c=c->next;
            b=b->next;
        }
        else
        {
            while(b->num<c->num&&b!=nullptr&&c!=nullptr)
            {
                b=b->next;
                if(b==nullptr)
                    break;
                if(b->num==c->num)
                {
                    if(head->next==nullptr)
                        head->next=b;
                    else
                        a->next=b;
                    a=b;
                    b=b->next;
                    c=c->next;
                    break;
                }
            }
        }
        a->next=nullptr;
    }

    bool flag=false;
    c=head->next;
    if(c==nullptr)
        cout<<"NULL";
    else
    {
        while(c!=nullptr)
        {
            if(!flag)
                cout<<c->num;
            else
                cout<<" "<<c->num;
            flag=true;
            c=c->next;
        }
    }
}
int main()
{
    node L1,L2,L3;
    L1=creatList();
    L2=creatList();
    mergeList(L1,L2);
}

7-4 一元多项式的乘法与加法运算 (20 分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

//使用数组模拟
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=4001;
int input1[maxn],input2[maxn];
int ans1[maxn],ans2[maxn];
void mul()
{
    for(int i=maxn-1; i>=0; i--)
        if(input1[i])
            for(int j=maxn-1; j>=0; j--)
                if(input2[j])
                    ans1[i+j]+=input1[i]*input2[j];
}
void add(int input[])
{
    for(int i=maxn-1; i>=0; i--)
        if(input[i])
            ans2[i]+=input[i];
}
void print1(int ans[])
{
    bool flag=false;
    for(int i=maxn-1; i>=0; i--)
        if(ans[i])
        {
            if(!flag)
                cout<<ans[i]<<" "<<i-2000;
            else
                cout<<" "<<ans[i]<<" "<<i-2000;
            flag=true;
        }
    if(!flag)
        cout<<"0 0\n";
    else
        cout<<endl;
}
void print2(int ans[])
{
    bool flag=false;
    for(int i=maxn-1; i>=0; i--)
        if(ans[i])
        {
            if(!flag)
                cout<<ans[i]<<" "<<i-1000;
            else
                cout<<" "<<ans[i]<<" "<<i-1000;
            flag=true;
        }
    if(!flag)
        cout<<"0 0\n";
    else
        cout<<endl;
}
int main()
{
    int n,m,k,p;
    cin>>n;
    while(n--)
    {
        cin>>k>>p;
        p+=1000;
        input1[p]=k;
    }
    cin>>m;
    while(m--)
    {
        cin>>k>>p;
        p+=1000;
        input2[p]=k;
    }
    mul();
    add(input1);
    add(input2);
    print1(ans1);
    print2(ans2);
}

7-5 一元多项式求导 (20 分)

设计函数求一元多项式的导数。

输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。

输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    int a,b;
    queue<int> iDate,iPow;
    while(cin>>a>>b)
    {
        iDate.push(a);
        iPow.push(b);
        if(b==0&&iDate.size()==1)
            break;
    }
    bool flag=false;
    if(iDate.size()==1)
        cout<<"0 0";
    else
        cout<<iDate.front()*iPow.front()<<" "<<iPow.front()-1;
    iDate.pop();
    iPow.pop();
    while(!iDate.empty())
    {
        a=iDate.front();
        b=iPow.front();
        iDate.pop();
        iPow.pop();
        if(b==0||a==0)
            continue;
        cout<<" "<<a*b<<" "<<b-1;
        flag=true;
    }
    cout<<endl;
}

7-6 求链式线性表的倒数第K项 (20 分)

给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字。

输入格式:
输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理)。

输出格式:
输出倒数第K个位置上的数据。如果这个位置不存在,输出错误信息NULL。

输入样例:
4 1 2 3 4 5 6 7 8 9 0 -1
输出样例:
7

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
struct node
{
    int num;
    struct node *next;
};
void creatList(int k)
{
    node *head,*r,*p,*w;
    head = (node*)malloc(sizeof(node));
    head->next=nullptr;
    r=head;
    int ans;
    int len=0;
    int num;
    while(cin>>num&&num>-1)
    {
        p=(node*)malloc(sizeof(node));
        p->num=num;
        p->next=nullptr;
        r->next=p;
        r=p;
        len++;
        if(len>=k)
        {
            node *t=head;
            head=head->next;
            free(t);
        }
    }
    if(len>=k)
        cout<<head->num<<endl;
    else
        cout<<"NULL\n";
}
int main()
{
    int k;
    cin>>k;
    node L1;
    creatList(k);
}

猜你喜欢

转载自blog.csdn.net/weixin_44056753/article/details/102712697
今日推荐