c++循环单链表类成员函数

CirSinglyList<T> operator+(SinglyList<T> &list)返回复制*this并尾插入list的链表,并集

你的链表数据结构的定义呢?是怎么写的?
 

#include<iostream>
using namespace std;
template<class T>
class Node {
public:
    T date;
    Node<T>* next;
    Node() {
        this->date='\0';
        this->next = NULL;
    }
    Node(T date, Node<T>* next = NULL) {
        this->date = date;
        this->next = next;
    }
};

 

 

#include<iostream>
using namespace std;
#include"Node.h"
template<class T>
class CirSinglyList {
private:
    Node<T>* head;
    Node<T>* rear;
public:
    CirSinglyList();
    CirSinglyList(T values[], int n);
    ~CirSinglyList();
    friend ostream& operator<<<>(ostream&, CirSinglyList<T>&);
    void insert(T x);
    CirSinglyList<T> operator*(const CirSinglyList<T> & list);
    CirSinglyList(const CirSinglyList<T>& list);
};
template<class T>
CirSinglyList<T>::CirSinglyList() {
    this->head = new Node<T>();
    this->head->next = this->head;
    this->rear = this->head;
}
template<class T>
CirSinglyList<T>::CirSinglyList(T values[], int n) {
    this->head = new Node<T>();
    this->head->next = this->head;
    this->rear = this->head;
    for (int i = 0; i < n; i++) {
        this->rear->next = new Node<T>(values[i]);
        this->rear = this->rear->next;
    }
    this->rear->next = this->head;
}

template<class T>
CirSinglyList<T>::~CirSinglyList() {
    this->rear->next = NULL;
    Node<T>* front;
    while (this->head) {
        front = this->head->next;
        delete head;
        this->head = front;
    }
}
template<class T>
ostream& operator<<<>(ostream& out, CirSinglyList<T>& list) {
    out << "(";
    for (Node<T>* p = list.head->next;p != list.head;p = p->next) {
        out << p->date;
        if (p->next != list.head) {
            out << ",";
        }
    }
    out << ")\n";
    return out;
}
template<class T>
void CirSinglyList<T>::insert(T x) {
    Node<T>* front = new Node<T>(x, this->head);
    this->rear->next = front;
    this->rear = this->rear->next;
}
template<class T>
CirSinglyList<T>::CirSinglyList(const CirSinglyList<T>& list) {
    this->head = new Node<T>();
    this->rear = this->head;
    for (Node<T>* p = list.head->next;p != list.head;p = p->next)
    {
        this->rear->next = new Node<T>(p->date);
        this->rear = this->rear->next;
    }
    this->rear->next = this->head;

}

template<class T>
CirSinglyList<T> CirSinglyList<T>:: operator*(const CirSinglyList<T>& list) {
    CirSinglyList<T> cir;
    Node<T>* p1 = this->head->next;
    Node<T>* p2 = list.head->next;
    Node<T>* front = cir.head;
    cir.head->next = NULL;
    while (p1 != this->head) {
        while (p2 != list.head) {
            if (p1->date == p2->date) {
                front->next = new Node<T>(p1->date);
                front = front->next;
                break;
            }
            p2 = p2->next;
        }
        p2 = list.head->next;
        p1 = p1->next;
    }
    front->next = cir.head;
    cir.rear = front;
    return cir;
}

C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632