SDUT---数据结构---链表部分题目

这篇博客详细介绍了数据结构中的链表操作,包括顺序建立、逆序建立、链表逆置、有序链表归并等。特别讨论了C/C++中地址和空间管理的难点,以及如何实现单链表的拆分、有序链表的建立和删除重复元素。还提及了双向链表的实现,认为其不过是链表的变形。

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

数据结构实验之链表一:顺序建立链表

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

struct yyh
{
    int data;
    struct yyh* next;
}*head,*p,*tail;
int main()
{
    int n;
    scanf("%d",&n);
    head=new struct yyh();
    head->next=NULL;
    tail=head;
    for(int i=1; i<=n; i++)
    {
        p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
    return 0;
}

数据结构实验之链表二:逆序建立链表

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

struct yyh
{
    int data;
    struct yyh* next;
}*head,*p;
int main()
{
    int n;
    scanf("%d",&n);
    head=new struct yyh();
    head->next=NULL;
    while(n--)
    {
        p=new struct yyh();
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
    return 0;
}

数据结构实验之链表三:链表的逆置

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

struct yyh
{
    int data;
    struct yyh* next;
}*head,*p;
int main()
{
    int n;
    head=new struct yyh();
    head->next=NULL;
    while(~scanf("%d",&n)&&n!=-1)
    {
        p=new struct yyh();
        p->data=n;
        p->next=head->next;
        head->next=p;
    }
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
    return 0;
}

数据结构实验之链表四:有序链表的归并

这个题很有意思,尤其是mergeSort里的备注,关于地址和开空间的问题,是C/C++最有意思也挺有难度的地方,正是因为地址这种东西,C/C++才屹立不倒;

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

struct yyh
{
    int data;
    struct yyh* next;
};

struct yyh* create(int w)
{
    struct yyh* head=new struct yyh();
    struct yyh* tail;
    head->next=NULL;
    tail=head;
    while(w--)
    {
        struct yyh* p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
struct yyh* mergeSort(struct yyh* head1,struct yyh* head2)
{
    struct yyh* p,* q,* tail,* head;
    head=new struct yyh();
    //这里注意,我们要建立一个新的链表,他的头是head,但是这个head没有给它开辟空间
    //所以我们必须先给head开辟一块新空间,mergeSort结束再把这个head的地址传给主函数里的head
    //注意,这里的head和主函数里的head不是一个head,主函数里的head呢
    //只是一个没有开辟它指向的struct yyh类型空间的指针。
    //另外也不要给主函数里的head开辟空间,因为这里的head是我们新建出来的,他不能指向主函数里
    //那个head指向的空间,依旧需要重新建立空间;
    //当然,如果你非要用主函数里的那个head,可以看下面一个代码
    //那个就是把主函数里的head传到这里来了;
    p=head1->next;
    q=head2->next;
    head->next=NULL;
    tail=head;
    delete(head1);
    delete(head2);
    while(p!=NULL&&q!=NULL)
    {
        if(p->data<=q->data)
        {
            tail->next=p;
            tail=p;
            p=p->next;
            tail->next=NULL;
        }
        else
        {
            tail->next=q;
            tail=q;
            q=q->next;
            tail->next=NULL;
        }
    }
    //注意下面的操作
    if(p!=NULL)
    {
        tail->next=p;
    }
    if(q!=NULL)
    {
        tail->next=q;
    }
    //因为链表之间的链接已经在重新排序之前连接好了,所以
    //当一个链表的所有成员都连上,而另一个还有N个成员没法
    //连上时,只需要连已和前边结点断开链接的那个结点,其后面的结点就一起连到总链表最后了
    return head;
}
void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}
int main()
{
    int n,m;
    struct yyh* head1,*head2,*head;
    scanf("%d %d",&n,&m);
    head1=create(n);
    head2=create(m);
    head=mergeSort(head1,head2);
    display(head);
    return 0;
}

这种也可以:

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

struct yyh
{
    int data;
    struct yyh* next;
};

struct yyh* create(int w)
{
    struct yyh* head=new struct yyh();
    struct yyh* tail;
    head->next=NULL;
    tail=head;
    while(w--)
    {
        struct yyh* p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
struct yyh* mergeSort(struct yyh* head1,struct yyh* head2,struct yyh* head)
{
    struct yyh* p,* q,* tail;
    head=new struct yyh();
    p=head1->next;
    q=head2->next;
    head->next=NULL;
    tail=head;
    delete(head1);
    delete(head2);
    while(p!=NULL&&q!=NULL)
    {
        if(p->data<=q->data)
        {
            tail->next=p;
            tail=p;
            p=p->next;
            tail->next=NULL;
        }
        else
        {
            tail->next=q;
            tail=q;
            q=q->next;
            tail->next=NULL;
        }
    }
    if(p!=NULL)
    {
        tail->next=p;
    }
    if(q!=NULL)
    {
        tail->next=q;
    }
    return head;
}
void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}
int main()
{
    int n,m;
    struct yyh* head1,*head2,*head;
    scanf("%d %d",&n,&m);
    head1=create(n);
    head2=create(m);
    head=new struct yyh();
    head=mergeSort(head1,head2,head);
    display(head);
    return 0;
}

下面这种是就地取材,把head1当做head,不需要再为head开辟空间。而head1在create函数里已经开辟了空间,不需要再开辟空间了

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

struct yyh
{
    int data;
    struct yyh* next;
};

struct yyh* create(int w)
{
    struct yyh* head=new struct yyh();
    struct yyh* tail;
    head->next=NULL;
    tail=head;
    while(w--)
    {
        struct yyh* p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
struct yyh* mergeSort(struct yyh* head1,struct yyh* head2)
{
    struct yyh* p,* q,* tail;
    p=head1->next;
    q=head2->next;
    head1->next=NULL;
    tail=head1;
    delete(head2);
    while(p!=NULL&&q!=NULL)
    {
        if(p->data<=q->data)
        {
            tail->next=p;
            tail=p;
            p=p->next;
            tail->next=NULL;
        }
        else
        {
            tail->next=q;
            tail=q;
            q=q->next;
            tail->next=NULL;
        }
    }
    if(p!=NULL)
    {
        tail->next=p;
    }
    if(q!=NULL)
    {
        tail->next=q;
    }
    return head1;
}
void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}
int main()
{
    int n,m;
    struct yyh* head1,* head2;
    scanf("%d %d",&n,&m);
    head1=create(n);
    head2=create(m);
    head1=mergeSort(head1,head2);
    display(head1);
    return 0;
}

数据结构实验之链表五:单链表的拆分

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

struct yyh
{
    int data;
    struct yyh* next;
};

struct yyh* create(int w)
{
    struct yyh* head=new struct yyh();
    struct yyh* tail;
    head->next=NULL;
    tail=head;
    while(w--)
    {
        struct yyh* p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
void splitAndDisplay(struct yyh* head)
{
    struct yyh* p,* head1,* head2,* tail1,* tail2;
    int k1=0,k2=0;
    head1=new struct yyh();
    head2=new struct yyh();
    p=head->next;
    tail1=head1;
    tail2=head2;
    while(p->next!=NULL)
    {
        if(p->data%2==0)
        {
            tail1->next=p;
            tail1=p;
            k1++;
        }
        else
        {
            tail2->next=p;
            tail2=p;
            k2++;
        }
        p=p->next;
    }
    if(p->data%2==0)
    {
        k1++;
        tail1->next=p;
        tail2->next=NULL;
    }
    else
    {
        k2++;
        tail2->next=p;
        tail1->next=NULL;
    }
    printf("%d %d\n",k1,k2);
    head1=head1->next;
    head2=head2->next;
    while(head1!=NULL)
    {
        if(head1->next==NULL)
        {
            printf("%d\n",head1->data);
        }
        else
        {
            printf("%d ",head1->data);
        }
        head1=head1->next;
    }
    while(head2!=NULL)
    {
        if(head2->next==NULL)
        {
            printf("%d\n",head2->data);
        }
        else
        {
            printf("%d ",head2->data);
        }
        head2=head2->next;
    }
}
int main()
{
    int n;
    struct yyh* head;
    scanf("%d",&n);
    head=create(n);
    splitAndDisplay(head);
    return 0;
}

数据结构实验之链表六:有序链表的建立

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

struct yyh
{
    int data;
    struct yyh* next;
};

struct yyh* create(int w)
{
    struct yyh* head=new struct yyh();
    struct yyh* tail,* q,* t;
    head->next=NULL;
    tail=head;
    while(w--)
    {
        struct yyh* p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        int flag;
        if(head->next==NULL)
        {
            tail->next=p;
            tail=p;
        }
        else
        {
            flag=1;
            q=head;
            t=head->next;
            while(t!=NULL)
            {
                if(p->data>t->data)
                {
                    q=q->next;
                    t=t->next;
                }
                else
                {
                    p->next=q->next;
                    q->next=p;
                    flag=0;
                    break;
                }
            }
        }
        if(flag==1)
        {
            tail->next=p;
            tail=p;
        }
    }
    return head;
}

void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}
int main()
{
    int n;
    struct yyh* head;
    scanf("%d",&n);
    head=create(n);
    display(head);
    return 0;
}

数据结构实验之链表七:单链表中重复元素的删除

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

struct yyh
{
    int data;
    struct yyh* next;
};

int n;
struct yyh* create(int w)
{
    struct yyh* head,* p;
    head=new struct yyh();
    head->next=NULL;
    while(w--)
    {
        p=new struct yyh();
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return head;
}

struct yyh * Delete(struct yyh* head)
{
    struct yyh * p,* q,* t;
    for(p=head->next; p!=NULL; p=p->next)
    {
        t=p;
        q=t->next;
        while(q!=NULL)
        {
            if(q->data==p->data)
            {
                t->next=q->next;
                q=t->next;
                n--;
            }
            else
            {
                t=t->next;
                q=q->next;
            }
        }
    }
    return head;
}

void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}

int main()
{
    struct yyh* head;
    scanf("%d",&n);
    head=create(n);
    printf("%d\n",n);
    display(head);
    head=Delete(head);
    printf("%d\n",n);
    display(head);
    return 0;
}

数据结构实验之链表九:双向链表

加一个prior前置指针即可,双向链表也好,循环链表也好,没有什么难的,就是变形罢了;

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

struct yyh
{
    int data;
    struct yyh* prior;
    struct yyh* next;
};

int n,m;
struct yyh* create(int w)
{
    struct yyh* head,* p,* tail;
    head=new struct yyh();
    head->next=NULL;
    tail=head;
    while(w--)
    {
        p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        p->prior=tail;
        tail=p;
    }
    return head;
}

void display(struct yyh* head)
{
    while(m--)
    {
        int t;
        scanf("%d",&t);
        struct yyh* p=head->next;
        while(p!=NULL)
        {
            if(p->data==t)
            {
                break;
            }
            p=p->next;
        }
        if(p->next!=NULL&&p->prior!=head)//注意头结点的问题,前置指针摸到头结点就算到头了
        {
            printf("%d %d\n",p->prior->data,p->next->data);
        }
        else if(p->prior!=head&&p->next==NULL)
        {
            printf("%d\n",p->prior->data);
        }
        else if(p->next!=NULL&&p->prior==head)
        {

            printf("%d\n",p->next->data);
        }
    }
}

int main()
{
    struct yyh* head;
    scanf("%d %d",&n,&m);
    head=create(n);
    display(head);
    return 0;
}

数组操作

有一说一这个题还是很考验基础功的。

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

struct yyh
{
    int data;
    struct yyh* next;
};
int n,m;

struct yyh* create(int n)
{
    struct yyh* head,* p,* tail;
    head=new struct yyh();
    head->next=NULL;
    tail=head;
    for(int i=0; i<n; i++)
    {
        p=new struct yyh();
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}

void first(struct yyh* head,int a,int b)
{
    struct yyh* q;
    q=head;
    struct yyh* t=new struct yyh();
    t->data=b;
    int flag=0;
    while(flag<a)
    {
        q=q->next;
        flag++;
    }
    t->next=q->next;
    q->next=t;
}

void second(struct yyh* head,int a)
{
    struct yyh* q;
    q=head;
    int flag=0;
    while(flag<a)
    {
        q=q->next;
        flag++;
    }
    q->next=q->next->next;
}

void third(struct yyh* head,int a,int b)
{
    struct yyh* q;
    q=head;
    int flag=0;
    while(flag<a)
    {
        q=q->next;
        flag++;
    }
    q->next->data=b;
}

void display(struct yyh* head)
{
    struct yyh* p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next!=NULL)
        {
            printf("%d ",p->data);
        }
        else
        {
            printf("%d\n",p->data);
        }
        p=p->next;
    }
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        struct yyh* head;
        head=create(n);
        int num,a,b;
        while(m--)
        {
            scanf("%d",&num);
            if(num==1)
            {
                scanf("%d %d",&a,&b);
                first(head,a,b);
            }
            else if(num==2)
            {
                scanf("%d",&a);
                second(head,a);
            }
            else if(num==3)
            {
                scanf("%d %d",&a,&b);
                third(head,a,b);
            }
            else if(num==4)
            {
                display(head);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值