数据结构实验1

7-1 线性表A,B顺序存储合并
有两张非递增有序的线性表A,B,采用顺序存储结构,两张表合并用c表存,要求C为非递减有序的,然后删除C表中值相同的多余元素。元素类型为整型

输入格式:
第一行输入输入表A的各个元素,以-1结束,中间用空格分隔;第二行输入表B的各个元素,以-1结束,中间用空格分隔。

输出格式:
输出结果为表C的非递减有序序列,中间用英文逗号分隔

输入样例:
在这里给出一组输入。例如:

9 8 7 -1
10 9 8 4 3 -1
输出样例:
在这里给出相应的输出。例如:

3,4,7,8,9,10

代码:

#include<stdio.h>
#include<stdlib.h>

//定义链表节点结构
typedef struct Node{
    int value;
    struct Node *next;
}Node;

//创建一个新的链表节点
Node* createNode(int value){
    Node* newNode=(Node*)malloc(sizeof(Node));
    newNode->value=value;
    newNode->next=NULL;
    return newNode;
}

//尾插法
void append(Node** head,int value){
    Node* newNode=createNode(value);
    if(*head==NULL){
        *head=newNode;
    }
    else{
        Node*temp=*head;
        while(temp->next!=NULL){
            temp=temp->next;
        }
        temp->next=newNode;
    }
}

//检查链表中是否包含某个值
int contains(Node*head,int value){
    Node*temp=head;
    while(temp!=NULL){
        if(temp->value==value){
            return 1;   //表示已经包含该值
        }
        temp=temp->next;
    }
    return 0;   //表示不包含该值
}

//合并两个链表并删除重复元素
void m(Node*headA,Node*headB,Node **headC){
    Node* tempA=headA;
    Node* tempB=headB;

    //合并
    while(tempA!=NULL&&tempB!=NULL){
        if(tempA->value<tempB->value){
            if(!contains(*headC,tempA->value)){
                append(headC,tempA->value);
            }
            tempA=tempA->next;
        }
        else if(tempA->value>tempB->value){
            if(!contains(*headC,tempB->value)){
                append(headC,tempB->value);
            }
            tempB=tempB->next;
        }
        else{
            //如果相同,只添加一个
            if(!contains(*headC,tempA->value)){
                append(headC,tempA->value);
            }
            tempA=tempA->next;
            tempB=tempB->next;
        }
    }

    //处理剩余的A链表
    while(tempA!=NULL){
        if(!contains(*headC,tempB->value)){
            append(headC,tempA->value);
        }
        tempA=tempA->next;
    }

    //处理剩余的B链表
    while(tempB!=NULL){
        if(!contains(*headC,tempB->value)){
            append(headC,tempB->value);
        }
        tempB=tempB->next;
    }
        }


//排序
void sortlist(Node**head){
    if(*head==NULL||(*head)->next==NULL){
        return;
    }
    Node*sorted=NULL;
    Node*current=*head;
    while(current!=NULL){
        Node*next=current->next;
        if(sorted==NULL||sorted->value>=current->value){
            current->next=sorted;
            sorted=current;
        }
        else{
            Node*temp=sorted;
            while(temp->next!=NULL&&temp->next->value<current->value){
                temp=temp->next;
            }
            current->next=temp->next;
            temp->next=current;
        }
        current=next;
    }
    *head=sorted;
}

//输出链表
void printlist(Node* head){
    Node* temp=head;
    int first=1;
    while(temp!=NULL){
        if(!first){
            printf(",");
        }
        printf("%d",temp->value);
        first=0;
        temp=temp->next;
    }
    printf("\n");
}
int main(){
    Node* headA=NULL;
    Node* headB=NULL;
    Node* headC=NULL;
    int value;
    while(scanf("%d",&value)&&value!=-1){
        append(&headA,value);
    }
    while(scanf("%d",&value)&&value!=-1){
        append(&headB,value);
    }
    m(headA,headB,&headC);
    sortlist(&headC);
    printlist(headC);
    return 0;
}
posted @   呓语-MSHK  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示