编写链表的建立,插入,删除,取数据元素,倒置等算法。
时间: 2023-05-12 13:05:30 浏览: 56
链表的建立、插入、删除、取数据元素和倒置等算法如下:
建立链表:
struct Node {
int data;
Node* next;
};
Node* createLinkedList(int arr[], int n) {
Node* head = new Node();
head->next = nullptr;
Node* p = head;
for (int i = 0; i < n; i++) {
Node* node = new Node();
node->data = arr[i];
node->next = nullptr;
p->next = node;
p = node;
}
return head->next;
}
插入节点:
void insertNode(Node* head, int pos, int data) {
Node* p = head;
for (int i = 0; i < pos - 1; i++) {
p = p->next;
}
Node* node = new Node();
node->data = data;
node->next = p->next;
p->next = node;
}
删除节点:
void deleteNode(Node* head, int pos) {
Node* p = head;
for (int i = 0; i < pos - 1; i++) {
p = p->next;
}
Node* q = p->next;
p->next = q->next;
delete q;
}
取数据元素:
int getData(Node* head, int pos) {
Node* p = head;
for (int i = 0; i < pos; i++) {
p = p->next;
}
return p->data;
}
倒置链表:
Node* reverseLinkedList(Node* head) {
Node* p = head;
Node* q = nullptr;
while (p != nullptr) {
Node* r = p->next;
p->next = q;
q = p;
p = r;
}
return q;
}
lua closure factory 完整代码:
function createClosure(x)
local function closure(y)
return x + y
end
return closure
end
local add5 = createClosure(5)
print(add5(3)) -- 输出 8
中文加密:
中文加密可以使用一些常见的加密算法,比如 AES、DES、RSA 等。其中,AES 是一种对称加密算法,DES 也是一种对称加密算法,RSA 是一种非对称加密算法。这些算法都可以用来加密中文文本。具体实现可以参考相关的加密库或者算法实现。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)