链表的增删改查功能,用于调用维护链表。
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class myList {
ListNode* head;
ListNode* tail;
int count;
public:
myList();
~myList();
int append(int value);
int append(ListNode& newNode);
int insert(int value, int index);
int insert(ListNode& newNode, int index);
int& operator[](int i) const;
int remove(int index);
int find(int value) const;
void print() const; // 打印链表
};
class solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0);
ListNode* current = &dummy;
while (list1 != nullptr && list2 != nullptr) {
if (list1->val < list2->val) {
current->next = list1;
list1 = list1->next;
}
else {
current->next = list2;
list2 = list2->next;
}
current = current->next;
}
if (list1 != nullptr) {
current->next = list1;
}
else {
current->next = list2;
}
return dummy.next;
}
};
void printlist(ListNode* node) {
while (node != nullptr) {
cout << node->val << " ";
node = node->next;
}
cout << endl;
}
int main() {
ListNode* list1 = new ListNode(1);
list1->next = new ListNode(2);
list1->next->next = new ListNode(3);
ListNode* list2 = new ListNode(1);
list2->next = new ListNode(3);
list2->next->next = new ListNode(4);
solution sol;
ListNode* list3 = sol.mergeTwoLists(list1, list2);
printlist(list3);
return 0;
}
myList::myList() : head(nullptr), tail(nullptr), count(0) {}
myList::~myList() {
ListNode* current = head;
while (current != nullptr) {
ListNode* nextNode = current->next;
delete current;
current = nextNode;
}
}
int myList::append(int value) {
ListNode* temp = new ListNode(value);
return append(*temp);
}
int myList::append(ListNode& newNode) {
if (head == nullptr) {
head = &newNode;
tail = head;
}
else {
tail->next = &newNode;
tail = tail->next;
}
count++;
return count;
}
int myList::insert(int value, int index) {
ListNode* temp = new ListNode(value);
return insert(*temp, index);
}
int myList::insert(ListNode& newNode, int index) {
if (index < 0 || index > count) {
return -1;
}
if (index == 0) {
newNode.next = head;
head = &newNode;
if (tail == nullptr) {
tail = head;
}
}
else {
ListNode* current = head;
for (int i = 1; i < index; ++i) {
current = current->next;
}
newNode.next = current->next;
current->next = &newNode;
if (newNode.next == nullptr) {
tail = &newNode;
}
}
count++;
return count;
}
int& myList::operator[](int i) const {
if (i < 0 || i >= count) {
throw out_of_range("Index out of range");
}
ListNode* current = head;
for (int j = 0; j < i; ++j) {
current = current->next;
}
return current->val;
}
int myList::remove(int index) {
if (index < 0 || index >= count) {
return -1;
}
ListNode* temp;
if (index == 0) {
temp = head;
head = head->next;
if (head == nullptr) {
tail = nullptr;
}
}
else {
ListNode* current = head;
for (int i = 1; i < index; ++i) {
current = current->next;
}
temp = current->next;
current->next = temp->next;
if (temp->next == nullptr) {
tail = current;
}
}
delete temp;
count--;
return count;
}
int myList::find(int value) const {
ListNode* current = head;
int index = 0;
while (current != nullptr) {
if (current->val == value) {
return index;
}
current = current->next;
index++;
}
return -1; // 如果找不到,返回-1
}
void myList::print() const {
ListNode* current = head;
while (current != nullptr) {
cout << current->val << " ";
current = current->next;
}
cout << endl;
}