一、链表简介
struct Node {
int data;
Node* next;
};
class ListNode {
public:
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class LinkedList {
private:
ListNode *head;
public:
LinkedList() {
head = NULL;
}
void addHead(int val) {
ListNode *node = new ListNode(val);
node->next = head;
head = node;
}
void Append(int val) {
if (head == NULL) {
head = new ListNode(val);
return;
}
ListNode *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new ListNode(val);
}
// 其他操作代码
};
class DoublyListNode {
public:
int val;
DoublyListNode *next;
DoublyListNode *prev;
DoublyListNode(int x) : val(x), next(NULL), prev(NULL) {}
};
class DoublyLinkedList {
private:
DoublyListNode *head;
DoublyListNode *tail;
public:
DoublyLinkedList() {
head = NULL;
tail = NULL;
}
void addHead(int val) {
DoublyListNode *node = new DoublyListNode(val);
if (head == NULL) {
head = tail = node;
} else {
node->next = head;
head->prev = node;
head = node;
}
}
// 其他操作
};