数据结构期末考试复习资料(编程题)

本文深入讲解了链表的多种操作,包括两个有序链表的合并与交集算法,以及循环单链表的基本运算如初始化、插入、删除等,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-90 两个有序链表序列的合并 (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 <bits/stdc++.h>

using namespace std;

typedef struct Node
{
    int data;
    Node *next;
}*LinkList;

void Creat_list(LinkList &L)
{
    int n; scanf("%d",&n);
    if(n==-1) return;

    L=(Node *)malloc(sizeof(Node));
    Node *p=L;
    p->data=n;
    p->next=NULL;

    while(true)
    {
        scanf("%d",&n);
        if(n==-1) break;
        Node *t=new Node();
        t->data=n;
        t->next=NULL;
        p->next=t;
        p=p->next;
    }
}

void hebin_list(LinkList &L,LinkList &L1,LinkList &L2)
{
    Node *p=L;
    Node *q=L1;
    Node *t=(Node *)malloc(sizeof(Node));
    t->next=NULL;
    L2=t;

    while(p!=NULL&&q!=NULL)
    {
        if(p->data<q->data)
        {
            t->next=p;
            p=p->next;
        }
        else
        {
            t->next=q;
            q=q->next;
        }
        t=t->next;
    }

    while(p!=NULL)
    {
        t->next=p;
        p=p->next;
        t=t->next;
    }

    while(q!=NULL)
    {
        t->next=q;
        q=q->next;
        t=t->next;
    }


}

int main()
{
    LinkList s=NULL,s1=NULL,s2=NULL;
    Creat_list(s);
    Creat_list(s1);
    hebin_list(s,s1,s2);

    if(s2->next==NULL)
    {
        printf("NULL\n");
        return 0;
    }

    bool flag=true;
    s2=s2->next;
    while(s2!=NULL)
    {
        if(flag)
        {
           printf("%d",s2->data);
           flag=false;
        }
        else printf(" %d",s2->data);
        s2=s2->next;
    }
    return 0;
}


//一定要注意每一步的NULL的初始化,否则此题无法求解!!!

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

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

输入格式:

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

输出格式:

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

输入样例:

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

输出样例:

2 5

 

#include <bits/stdc++.h>

using namespace std;

typedef struct Node
{
    int data;
    Node *next;
}*LinkList;

void Creat_list(LinkList &L)
{
    int n; scanf("%d",&n);
    if(n==-1) return;

    L=(Node *)malloc(sizeof(Node));
    Node *p=L;
    p->data=n;
    p->next=NULL;

    while(true)
    {
        scanf("%d",&n);
        if(n==-1) break;
        Node *t=new Node();
        t->data=n;
        t->next=NULL;
        p->next=t;
        p=p->next;
    }
}

void xiangjiao_list(LinkList &L,LinkList &L1,LinkList &L2)
{
    Node *p=L;
    Node *q=L1;
    Node *t=(Node *)malloc(sizeof(Node));
    t->next=NULL;
    L2=t;

    while(p!=NULL)
    {
        bool flag=true;
        while(q!=NULL)
        {
            if(q->data==p->data)
            {
                flag=false;
                q=q->next;
                break;
            }
            if(q->data>p->data)
            {
                break;
            }
            q=q->next;
        }
        if(!flag)
        {
             t->next=p;
             t=t->next;
        }
        p=p->next;
    }

}

int main()
{
    LinkList s=NULL,s1=NULL,s2=NULL;
    Creat_list(s);
    Creat_list(s1);
    xiangjiao_list(s,s1,s2);

    if(s2->next==NULL)
    {
        printf("NULL\n");
        return 0;
    }

    bool flag=true;
    s2=s2->next;
    while(s2!=NULL)
    {
        if(flag)
        {
           printf("%d",s2->data);
           flag=false;
        }
        else printf(" %d",s2->data);
        s2=s2->next;
    }
    return 0;
}

 

7-113 jmu-ds-循环单链表的基本运算 (15 分)

实现循环单链表的基本运算:初始化、插入、删除、求表的长度、判空、释放。
(1)初始化循环单链表L,输出(L->next==L)的逻辑值;
(2)依次采用尾插法插入元素:输入分两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。
(3)输出循环单链表L;
(4)输出循环单链表L的长度;
(5)判断循环单链表L是否为空;
(6)输出循环单链表L的第3个元素;
(7)输出元素a的位置;
(8)在第4个元素位置上插入‘w’元素;
(9)输出循环单链表L;
(10)删除L的第5个元素;
(11)输出循环单链表L;
(12)释放循环单链表L。

输入格式:

两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。

输出格式:

按照程序要求输出

输入样例:

5
a b c d e

输出样例:

1
a b c d e
5
no
c
1
a b c w d e
a b c w e

#include <bits/stdc++.h>

using namespace std;

typedef struct Node
{
    char data;
    Node *next;
}*LinkList;

void printf_list(LinkList &L)
{
    Node *p=L->next;
    bool flag=true;
    while(p!=L)
    {
        if(flag)
        {
            printf("%c",p->data);
            flag=false;
        }
        else printf(" %c",p->data);

        p=p->next;
    }

    printf("\n");
}

void Insert_end_list(LinkList &L)
{
    L=(Node *)malloc(sizeof(Node));
    Node *p=L;
    char ch;

    int n; scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf(" %c",&ch);
        Node *t=(Node *)malloc(sizeof(Node));
        t->data=ch;
        t->next=NULL;
        p->next=t;
        p=p->next;
    }

    p->next=L;

}

int size_list(LinkList &L)
{
    int len=0;
    Node *p=L->next;
    while(p!=L)
    {
        len++;
        p=p->next;
    }
    return len;
}

bool empty_list(LinkList &L)
{
    if(L->next==L) return true;
    else return false;
}

void printf_pos(LinkList &L,int pos)
{
    int len=0;
    Node *p=L->next;
    while(p!=L)
    {
        len++;
        if(len==pos)
        {
            printf("%c\n",p->data);
            break;
        }
        p=p->next;
    }
}

void printf_value_position(LinkList &L,char a)
{
    int len=0;
    Node *p=L->next;

    while(p!=L)
    {
        len++;
        if(p->data==a)
        {
            printf("%d\n",len);
            break;
        }

        p=p->next;
    }
}

void Insert_position_list(LinkList &L,int pos)
{
    Node *p=L->next;
    int len=0;

    while(p!=L)
    {
        len++;
        if(len+1==pos) break;
        p=p->next;
    }

    Node *t=(Node *)malloc(sizeof(Node));
    t->data='w';
    t->next=p->next;
    p->next=t;

}

void delete_list(LinkList &L,int pos)
{
    Node *p=L->next;
    int len=0;

    while(p!=L)
    {
        len++;
        if(len+1==pos) break;
        p=p->next;
    }
    p->next=p->next->next;

}


int main()
{
    LinkList s=NULL;
    if(s==NULL) printf("1\n");
    Insert_end_list(s);

    printf_list(s);

    printf("%d\n",size_list(s));

    if(empty_list(s)) printf("yes\n");
    else printf("no\n");

    printf_pos(s,3);

    printf_value_position(s,'a');

    Insert_position_list(s,4);

    printf_list(s);

    delete_list(s,5);

    printf_list(s);

    free(s);
    return 0;
}

//注意空格%c的使用,一定要注意读入格式!!!
//注意插入时的t->next=p->next;不是p->next->next;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值