实现常见的链表操作,插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序, 重载运算符:下标,+(求和),-(求差),<<(输出插入符),>>(输入插入符) 实现常规链表类和模板链表类,并在main中测试
时间: 2023-05-23 12:00:41 浏览: 47
好的,以下是我的回答:
实现常见的链表操作:
```
template<typename T>
class LinkedList {
private:
struct Node {
T data;
Node* next;
Node(T data, Node* next = nullptr) : data(data), next(next) {}
};
Node* head;
int size;
public:
LinkedList() : head(nullptr), size(0) {}
~LinkedList() {
clear();
}
void insertHead(T data) {
Node* newHead = new Node(data, head);
head = newHead;
size++;
}
void insertTail(T data) {
Node* newTail = new Node(data);
if (head == nullptr) {
head = newTail;
} else {
Node* curr = head;
while (curr->next != nullptr) {
curr = curr->next;
}
curr->next = newTail;
}
size++;
}
void insertAfter(T newData, T afterData) {
Node* curr = head;
while (curr != nullptr && curr->data != afterData) {
curr = curr->next;
}
if (curr == nullptr) {
return;
}
Node* newNode = new Node(newData, curr->next);
curr->next = newNode;
size++;
}
void insertBefore(T newData, T beforeData) {
if (head == nullptr) {
return;
}
if (head->data == beforeData) {
insertHead(newData);
return;
}
Node* curr = head;
while (curr->next != nullptr && curr->next->data != beforeData) {
curr = curr->next;
}
if (curr->next == nullptr) {
return;
}
Node* newNode = new Node(newData, curr->next);
curr->next = newNode;
size++;
}
void modify(T oldData, T newData) {
Node* curr = head;
while (curr != nullptr && curr->data != oldData) {
curr = curr->next;
}
if (curr == nullptr) {
return;
}
curr->data = newData;
}
void remove(T data) {
if (head == nullptr) {
return;
}
if (head->data == data) {
Node* oldHead = head;
head = head->next;
delete oldHead;
size--;
return;
}
Node* curr = head;
while (curr->next != nullptr && curr->next->data != data) {
curr = curr->next;
}
if (curr->next == nullptr) {
return;
}
Node* oldNode = curr->next;
curr->next = oldNode->next;
delete oldNode;
size--;
}
void sort() {
if (head == nullptr || head->next == nullptr) {
return;
}
LinkedList<T> leftHalf;
LinkedList<T> rightHalf;
Node* curr = head;
while (curr != nullptr) {
leftHalf.insertTail(curr->data);
curr = curr->next;
if (curr != nullptr) {
rightHalf.insertTail(curr->data);
curr = curr->next;
}
}
leftHalf.sort();
rightHalf.sort();
merge(leftHalf, rightHalf);
}
void clear() {
while (head != nullptr) {
Node* oldHead = head;
head = head->next;
delete oldHead;
}
size = 0;
}
bool isEmpty() const {
return size == 0;
}
bool contains(T data) const {
Node* curr = head;
while (curr != nullptr && curr->data != data) {
curr = curr->next;
}
return curr != nullptr;
}
void serialize(const std::string& filename) const {
std::ofstream outFile(filename);
if (!outFile.is_open()) {
return;
}
Node* curr = head;
while (curr != nullptr) {
outFile << curr->data << std::endl;
curr = curr->next;
}
outFile.close();
}
void deserialize(const std::string& filename) {
clear();
std::ifstream inFile(filename);
if (!inFile.is_open()) {
return;
}
T data;
while (inFile >> data) {
insertTail(data);
}
inFile.close();
}
T& operator[](int index) {
if (index < 0 || index >= size) {
throw std::out_of_range("Index out of range");
}
Node* curr = head;
for (int i = 0; i < index; i++) {
curr = curr->next;
}
return curr->data;
}
const T& operator[](int index) const {
if (index < 0 || index >= size) {
throw std::out_of_range("Index out of range");
}
Node* curr = head;
for (int i = 0; i < index; i++) {
curr = curr->next;
}
return curr->data;
}
T sum() const {
T total = 0;
Node* curr = head;
while (curr != nullptr) {
total += curr->data;
curr = curr->next;
}
return total;
}
T operator-(const LinkedList<T>& other) const {
T total = 0;
Node* curr = head;
while (curr != nullptr) {
if (!other.contains(curr->data)) {
total += curr->data;
}
curr = curr->next;
}
return total;
}
std::ostream& operator<<(std::ostream& os) const {
Node* curr = head;
while (curr != nullptr) {
os << curr->data << ' ';
curr = curr->next;
}
return os;
}
std::istream& operator>>(std::istream& is) {
T data;
is >> data;
insertTail(data);
return is;
}
private:
void merge(LinkedList<T>& left, LinkedList<T>& right) {
clear();
Node* leftCurr = left.head;
Node* rightCurr = right.head;
while (leftCurr != nullptr || rightCurr != nullptr) {
if (leftCurr == nullptr) {
insertTail(rightCurr->data);
rightCurr = rightCurr->next;
} else if (rightCurr == nullptr) {
insertTail(leftCurr->data);
leftCurr = leftCurr->next;
} else {
if (leftCurr->data < rightCurr->data) {
insertTail(leftCurr->data);
leftCurr = leftCurr->next;
} else {
insertTail(rightCurr->data);
rightCurr = rightCurr->next;
}
}
}
}
};
```
在 main 函数中测试:
```
int main() {
LinkedList<int> list;
list.insertHead(3);
list.insertTail(4);
list.insertTail(6);
list.insertHead(2);
list.insertHead(1);
if (list.contains(6)) {
std::cout << "List contains 6" << std::endl;
}
list.remove(4);
std::cout << "List after removing 4: " << list << std::endl;
list.sort();
std::cout << "List after sorting: " << list << std::endl;
list.serialize("list.txt");
LinkedList<int> newList;
newList.deserialize("list.txt");
std::cout << "New list after deserializing: " << newList << std::endl;
std::cout << "Sum of list: " << list.sum() << std::endl;
std::cout << "Difference between list and {1, 3, 5}: " << list - (LinkedList<int>{1, 3, 5}) << std::endl;
LinkedList<int> numbers;
std::cout << "Enter some numbers (end with non-integer): ";
while (std::cin >> numbers);
std::cout << "List after input: " << numbers << std::endl;
return 0;
}
```
问:lua closure factory 完整代码
答:
```
function makeCounter()
local count = 0
return function ()
count = count + 1
return count
end
end
local counter1 = makeCounter()
local counter2 = makeCounter()
print(counter1()) -- prints 1
print(counter1()) -- prints 2
print(counter1()) -- prints 3
print(counter2()) -- prints 1
```
问:中文加密
答:常见的中文加密方式之一为异或加密,具体实现如下:
```
#include <iostream>
#include <string>
std::string xorEncrypt(const std::string& input, const std::string& key) {
std::string encrypted;
for (int i = 0; i < input.size(); i++) {
int keyIndex = i % key.size();
int encryptedChar = input[i] ^ key[keyIndex];
encrypted.push_back(static_cast<char>(encryptedChar));
}
return encrypted;
}
std::string xorDecrypt(const std::string& encrypted, const std::string& key) {
std::string decrypted;
for (int i = 0; i < encrypted.size(); i++) {
int keyIndex = i % key.size();
int decryptedChar = encrypted[i] ^ key[keyIndex];
decrypted.push_back(static_cast<char>(decryptedChar));
}
return decrypted;
}
int main() {
std::string input = "你好,世界";
std::string key = "mykey";
std::string encrypted = xorEncrypt(input, key);
std::cout << "Input: " << input << std::endl;
std::cout << "Encrypted: " << encrypted << std::endl;
std::cout << "Decrypted: " << xorDecrypt(encrypted, key) << std::endl;
return 0;
}
```