在不改变功能的情况下,改变代码中的部分命名#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* head;
codeLinkList(): 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();
code - 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: ListNode* head;
code - User
整理此代码
- 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: ListNode* head;
code - User
整理此代码#include using namespace std; //定义 const int Max = 100;
//单链表节点类型 template struct LinkNode { T data; LinkNode* next; LinkNode():next(NULL){} LinkNode(T d):data(d),next(NULL){} };
//模板 template class LinkList { public: LinkNode* head;
- Assistant