在不改变功能的情况下,改变代码中的部分命名并整理 #include<iostream> #include<cstdlib> using namespace std;…
- User
在不改变功能的情况下,改变代码中的部分命名并整理 #include #include
using namespace std;
template struct LinkNode//单链表结点类型 { T data; LinkNode*next; LinkNode():next(NULL){} LinkNode(T d):data(d),next(NULL){} }; template class LinkList{
public: LinkNode<T> *head; LinkList():head(NULL){} ~LinkList() { // 析构函数 if (head) { LinkNode<T>* v = head->next; while (v != head) { LinkNode<T>* temp = v; v = v->next; delete temp; } delete head; } } void DispList() { // 输出链表元素 if (head) { LinkNode<T>* v = head; do { cout << v->data << " "; v = v->next; } while (v != head); cout << endl; } } void CreateListR(T a[], int n) { // 创建链表 if (a && n > 0) { head = new LinkNode<T>(a[0]); LinkNode<T>* tail = head; for (int i = 1; i < n; i++) { LinkNode<T>* newNode = new LinkNode<T>(a[i]); tail->next = newNode; tail = newNode; } tail->next = head; // 形成循环 } } void ConnectLists(LinkList<T>& list2) { // 连接两个链表 if (list2.head) { if (!head) { head = list2.head; } else { LinkNode<T>* tail1 = head; while (tail1->next != head) { tail1 = tail1->next; } LinkNode<T>* tail2 = list2.head; while (tail2->next != list2.head) { tail2 = tail2->next; } tail1->next = list2.head; tail2->next = head; list2.head = NULL; } } } };//主函数 int main() { LinkList LinkList1; cout<<"输入第一个循环单链表的结点个数:"; int no1,no2; cin>>no1; int arr1[no1]; cout<<"输入第一个循环单链表各个结点的值:"; for(int i=0;i<no1;++i){ cin>>arr1[i];
} LinkList1.CreateListR(arr1,no1); cout<<"输入第一个循环单链表各个结点的值是:"; LinkList1.DispList();
LinkList LinkList2; cout<<"输入第二个循环单链表的结点个数:"; cin>>no2; int arr2[no2]; cout<<"输入第二个循环单链表各个结点的值:"; for(int i=0;i<no2;++i) { cin>>arr2[i]; } LinkList2.CreateListR(arr2,no2); cout<<"输入第二个循环单链表的值为:"; LinkList2.DispList();
LinkList1.ConnectLists(LinkList2); cout<<"合并后的循环单链表是:"; LinkList1.DispList();
cout<<"23 胡博文"; return 0;
}
- Assistant
#include #include
using namespace std;
template struct ListNode { T data; ListNode* next; ListNode() : next(NULL) {} ListNode(T d) : data(d), next(NULL) {} };
template class LinkedList{
public:
- User
在不改变功能的情况下整理该代码 #include #include
using namespace std;
template struct LinkNode//单链表结点类型 { T data; LinkNode*next; LinkNode():next(NULL){} LinkNode(T d):data(d),next(NULL){} }; template class LinkList{
- Assistant
#include #include
using namespace std;
template struct ListNode { T data; ListNode* next; ListNode() : next(NULL) {} ListNode(T d) : data(d), next(NULL) {} };
template class LinkedList{
public: