zoukankan      html  css  js  c++  java
  • 【数据结构上机练习】考试题目 2

    代码:

    //这个不是我的代码
    
    
    
    //链表表示集合,求两个集合交集 Intersection()
    #include<iostream>
    using namespace std;
    template<class T>
    //链表结点
    struct SLNode{
        T data;//数据
        SLNode<T> *next;//指针
        SLNode(SLNode * nextNode = NULL){next = nextNode;}
        SLNode(const T& item, SLNode * nextNode = NULL){data = item; next = nextNode;}
    };
    template <class T>
    //链表
    class SLList{
    private:
        SLNode<T> * head;//头指针
        SLNode<T> * cur;//当前指针
        SLNode<T> * rear;//尾指针
        int size;
    public:
        SLList();//构造函数,创建一个只有哨位结点的单链表
        SLList(T& item);//构造函数,创建有一个哨位结点和一个表结点的单链表
        ~SLList();//析构函数
        void InsertHead(const T& item);//表头插入
        bool FindCur(T& item)const;
        T& GetCur(){return cur->data;}
        int Search(const T& item);//查找值为x的元素在单链表中出现的位置(是链表中的第几个元素)
        void SetStart(){cur = head;}
        bool Next(){while(cur != rear) {cur = cur->next;return true;}return false;}
        int IsEmpty(){return head == rear;}
    };
    template <class T>
    SLList<T>::SLList(){
        head = rear = cur = new SLNode<T>(NULL);
        size = 0;
    }
    template <class T>
    SLList<T>::SLList(T& item){
        rear = cur = new SLNode<T>(item,NULL);
        head = new SLNode<T>(cur);
    }
    template <class T>
    SLList<T>::~SLList(){
        while(head->next != NULL){
            cur = head->next;
            head->next = cur->next;
            delete cur;
        }
        delete head;
    }
    
    template <class T>
    void SLList<T>::InsertHead(const T& item){
        if(!head->next)
            head->next = cur = rear = new SLNode<T>(item, NULL);
        else{
            if(Search(item) == -1)
                head->next = new SLNode<T>(item, head->next);
        }
        size++;
    }
    template <class T>
    int SLList<T>::Search(const T& item){
        if(head == rear){
            return -1;
        }
        int i = 0;
        cur = head;
        while(cur->next){
            i++;
            if(cur->next->data == item)
                return i;
            cur = cur->next;
        }
        return -1;
    }
    template <class T>
    bool SLList<T>::FindCur(T& item)const{
        if(!cur){
            return false;
        }
        item = cur->data;
        return true;
    }
    template<class T>
    void printlist(SLList<T>* list){
        list->SetStart();
        T item;
        if(list->IsEmpty())
            cout<<"空集";
        else
            while(list->Next()){
                list->FindCur(item);
                cout<<item<<" ";
            }
        cout<<endl;
    }
    template<class T>
    void Intersection(SLList<T> *p,SLList<T> *q,SLList<T> *r){
        p->SetStart();
        while(p->Next()){
            if(q->Search(p->GetCur()) > -1)    r->InsertHead(p->GetCur());
        }
    }
    int main(){
        int n,item;
        SLList<int> *p = new SLList<int>;
        SLList<int> *q = new SLList<int>;
        SLList<int> *r = new SLList<int>;
        cout<<"请输入链表元素个数:";
        cin>>n;
        cout<<"请输入链表元素,链表元素为整型:"<<endl;
        for(int i = 0; i < n; i++){
            cin>>item;
            p->InsertHead(item);
        }
    
        cout<<"请输入链表元素个数:";
        cin>>n;
        cout<<"请输入链表元素,链表元素为整型:"<<endl;
        for(int i = 0; i < n; i++){
            cin>>item;
            q->InsertHead(item);
        }
        cout<<"集合 p 为:"<<endl<<endl;
        printlist(p);
        cout<<endl;
        cout<<"集合 q 为:"<<endl<<endl;
        printlist(q);
        cout<<endl;
        cout<<"集合 p 与集合 q 的交集 r 为:"<<endl<<endl;
        Intersection(p,q,r);
        printlist(r);
        cout<<endl;
        return 0;
    }

    运行结果:

    转载文章请注明出处: http://www.cnblogs.com/menglei/
  • 相关阅读:
    ios swift 支持cocoaPods
    iOS 国际化
    ios storyboard全解析 (二)
    ios storyboard全解析 (一)
    UML类图的几个关系自我总结,(入门级)
    crypt 病毒
    js思维导向图
    关于索引的使用
    SQL Server 索引结构及其使用
    关于js数组的那些事
  • 原文地址:https://www.cnblogs.com/menglei/p/2810900.html
Copyright © 2011-2022 走看看