zoukankan      html  css  js  c++  java
  • 数据结构实验一

    实现两个集合的差:

    #include"iostream"
    #include"stdio.h"
    #include"algorithm"
    #include"string.h"
    #include"cmath"
    #define maxsize 100
    using namespace std;
    typedef int datatype;
    struct linklist //结点类型
    {
           datatype data; //数据域
           linklist *next; //指针域
    };
    
    void Initlist(linklist *&first) //链表初始化
    {
        first=new linklist;//动态分配空间,如果出现错误则输出 allocate error
        if(first==NULL){cout<<"allocate error!"<<endl;}
        else
        {
            first->next=NULL;
            first->data=0;//记录链表的长度,空着也是空着
        }
    }
    
    void Clearlist(linklist *&first) //清空链表
    {
        linklist *temp;
        while(first->next!=NULL)
        {
            temp=first->next;
            first->next=temp->next;
            delete temp;//释放掉temp所占的空间
        }
    }
    
    bool Empty(linklist *&first) //判断链表是否为空
    {
        if(first->next==NULL) return true;
        return false;
    }
    
    linklist *Search(linklist *&first,datatype x) //查找并返回链表中值域为x的元素地址
    {
        linklist *temp=first->next;
        while(temp!=NULL&&temp->data!=x) temp=temp->next; //找到或者没找到,都返回的是temp
        return temp;
    }
    
    void Insertfront1(linklist *&first,datatype x)//循环输入是头插法建表
    {
        linklist *temp=new linklist;
        if(temp==NULL) {cout<<"allocate error"<<endl;exit(1);}
        temp->data=x;
        temp->next=first->next;
        first->next=temp;
    }
    
    void Output(linklist *&first) //输出链表中的元素
    {
        if(first->next!=NULL)
        {
            cout<<first->next->data<<' ';
            Output(first->next);
        }
        cout<<endl;
    }
    
    int main()
    {
        int case_count=0;
        while(1)
        {
            cout<<"案例"<<++case_count<<":"<<endl;
            linklist *la,*lb,*lc;
        int number_a,number_b;//记录A和B集合中的元素个数
        datatype a[maxsize],b[maxsize];
        Initlist(la);Initlist(lb);Initlist(lc);
        cout<<"A集合中的元素个数:";cin>>number_a;
        cout<<"输入A集合中的元素:";
        for(int i=0;i<number_a;i++)
        {
            cin>>a[i];
            Insertfront1(la,a[i]);
        }
        cout<<"B集合中的元素个数:";cin>>number_b;
        cout<<"输入B集合中的元素:";
        for(int i=0;i<number_b;i++)
        {
            cin>>b[i];
            Insertfront1(lb,b[i]);
        }
        //将A-B的元素放到lc中
        for(int i=0;i<number_a;i++)
        {
            if(Search(lb,a[i])==NULL)
                Insertfront1(lc,a[i]);
        }
        cout<<"输出A-B中的元素:";
        if(Empty(lc))  {cout<<"空集!"<<endl<<endl;;}
        else Output(lc);
        Clearlist(la);Clearlist(lb);Clearlist(lc);//清空链表
        }
        return 0;
    }
    View Code
  • 相关阅读:
    [leetcode]N-Queens II @ Python
    [leetcode]N-Queens @ Python
    [leetcode]Symmetric Tree @ Python
    [leetcode]Same Tree @ Python
    [leetcode]Validate Binary Search Tree @ Python
    [leetcode]Recover Binary Search Tree @ Python
    关于Kubernetes Master高可用的一些策略
    hue 登录访问不了HDFS webhdfs_url 调整
    java项目代码上线
    nginx实现最简单的直播
  • 原文地址:https://www.cnblogs.com/acm-jing/p/4348417.html
Copyright © 2011-2022 走看看