0
点赞
收藏
分享

微信扫一扫

C++数据结构——链表

云岭逸人 2022-04-18 阅读 50

闲鱼帮别人写的一些链表题目,可以拿去用或者学习,请勿复制。

初识链表

题目描述

输入

输出

样例输入
1 4 99 21 50 61 32 4 -1
样例输出
The maximum,minmum and the total are:99 1 272

#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
    int  data;
    struct LNode * next;
}LNode,*LinkList;
LinkList createlist(LinkList head)
{
    int temp;
    LinkList p;
    while(scanf("%d",&temp)!=EOF)
    {
        if(temp==-1) break;
        p=(LinkList)malloc(sizeof(LNode));
        p->data=temp;
        p->next=head->next;
        head->next=p;
    }
    return head;
}
//void ListPint(LinkList head)
//{
//  LinkList p;
//  p=head->next;
//  while(p!=NULL)
//  {
//      printf("%d\n",p->data);
//      p=p->next;
//  }
//}
void findAndSum(LinkList head)
{
    LinkList p;
    p=head->next;
    int min = p->data,max=p->data;
    int sum=0;
    while(p!=NULL)
    {
        if(p->data<=min)
        {
            min=p->data;
        }
        if(p->data>max){
            max=p->data;
        }
        sum+=p->data;
        p=p->next;
    }
    printf("The maximum,minmum and the total are:%d %d %d",max,min,sum);
}
int  main()
{
    int i;
    LinkList head;
    head=(LinkList)malloc(sizeof(LNode));
    head->next=NULL;
    head = createlist(head);
    findAndSum(head);
}

链表排序

题目描述

输入

输出

样例输入
49 38 65 97 76 13 27 49 -1
样例输出
The new list is:13 27 38 49 49 65 76 97

#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
    int  data;
    struct LNode * next;
}LNode,*LinkList;
LinkList createlist(LinkList head)
{
    int temp;
    LinkList p;
    while(scanf("%d",&temp)!=EOF)
    {
        if(temp==-1) break;
        p=(LinkList)malloc(sizeof(LNode));
        p->data=temp;
        p->next=head->next;
        head->next=p;
    }
    return head;
}
void ListPint(LinkList head)
{
    LinkList p;
    p=head->next;
    printf("The new list is:");
    while(p!=NULL)
    {
        if(p==head->next)
            printf("%d",p->data);
        else printf(" %d",p->data);
        p=p->next;
    }
}
 
void sortList(LinkList head)
{
    LinkList p,q;
    p=head->next;
    while(p!=NULL){
        q=p;
        while(q!=NULL){
            if(q->data<p->data){
                int temp=p->data;
                p->data=q->data;
                q->data=temp;
            }
            q=q->next;
        }
        p=p->next;
    }
}
 
int  main()
{
    int i;
    LinkList head;
    head=(LinkList)malloc(sizeof(LNode));
    head->next=NULL;
    head = createlist(head);
    sortList(head);
    ListPint(head);
}

链表匹配

题目描述

要求:

输入

输出

样例输入 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 1:
ListB is the sub sequence of ListA.

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

#include<stdio.h>
#include<malloc.h>
typedef struct LNode {
    int  data;
    struct LNode * next;
} LNode,*LinkList;
LinkList createlist(LinkList head) {
    int temp;
    LinkList p=head,q;
    while(scanf("%d",&temp)!=EOF) {
        if(temp==-1) break;
        q=(LinkList)malloc(sizeof(LNode));
        q->data=temp;
        q->next=NULL;
        p->next=q;
        p=p->next;
    }
    return head;
}
void ansPint(int num) {
    if(num==1){
        printf("ListB is the sub sequence of ListA.");
    }else{
        printf("ListB is not the sub sequence of ListA.");
    }
}
 
void print(LinkList head)
{
    LinkList p;
    p=head->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
 
void judgeLists(LinkList headA,LinkList headB) {
    LinkList p,q,g;
    p=headA->next;
    while(p!=NULL) {
        q=headB->next;
        if(p->data==q->data){
            g=p;
             
            while(q!=NULL&&g!=NULL){
                if(g->data!=q->data) break;
                g=g->next;
                q=q->next;
            }
            if(q==NULL){
                ansPint(1);
                return;
            }
        }
        p=p->next;
    }
    ansPint(0);
}
 
int  main() {
    int i;
    LinkList headA,headB;
    headA=(LinkList)malloc(sizeof(LNode));
    headB=(LinkList)malloc(sizeof(LNode));
    headA->next=NULL;
    headB->next=NULL;
    headA = createlist(headA);
    headB = createlist(headB);
//  print(headA);
//  print(headB);
    judgeLists(headA,headB);
}

链表交换

题目描述

输入

输出

样例输入
1 2 3 4 5 6 7 8 9 10 -1
1 1 4 7
样例输出
The new list is:4 5 6 7 2 3 1 8 9 10

#include<stdio.h>
#include<malloc.h>
typedef struct LNode {
    int  data;
    struct LNode * next;
} LNode,*LinkList;
LinkList createlist(LinkList head) {
    int temp;
    LinkList p=head,q;
    while(scanf("%d",&temp)!=EOF) {
        if(temp==-1) break;
        q=(LinkList)malloc(sizeof(LNode));
        q->data=temp;
        q->next=NULL;
        p->next=q;
        p=p->next;
    }
    return head;
}
 
void print(LinkList head)
{
    LinkList p;
    p=head->next;
    printf("The new list is:");
    while(p!=NULL){
        if(p==head->next){
            printf("%d",p->data);
        }else{
            printf(" %d",p->data);
        }
        p=p->next;
    }
}
 
void changeLists(LinkList head) {
    LinkList p,S1,S2,T1,T2,g;
    p=head;
    int s1,t1,s2,t2;
    scanf("%d %d %d %d",&s1,&t1,&s2,&t2);
    int k=0;
    while(p!=NULL){
        if(k==s1-1){
            S1=p;
        }
        if(k==s2-1){
            S2=p;
        }
        if(k==t1){
            T1=p;
        }
        if(k==t2){
            T2=p;
        }
        k++;
        p=p->next;
    }
    g=S1->next;
    S1->next=S2->next;
    S2->next=g;
    g=T1->next;
    T1->next=T2->next;
    T2->next=g;
}
 
int  main() {
    int i;
    LinkList head;
    head=(LinkList)malloc(sizeof(LNode));
    head->next=NULL;
    head = createlist(head);
    changeLists(head);
    print(head);
}
举报

相关推荐

0 条评论