单向链表:
#include <iostream>
#define int long long
using namespace std;
const int N = 1e6 + 5;
struct linkedNode {
int data;
linkedNode* next;
};
struct linkedList {
public:
int size() { return listSize; } // 输出链表长度,默认是0
bool empty() { return listSize == 0; } // 输出链表是否为空
linkedNode* getIdx(int pos); // 得到一个在第pos个位置的linkedNode类型的指针
void insert(int pos, int theElement); // 在链表的第pos个位置插入元素theElement
void erase(int pos); // 擦除第pos个位置的元素
private:
unsigned int listSize = 0;
linkedNode* head;
};
linkedNode* linkedList::getIdx(int pos) {
if (pos < 0 || pos > listSize)
return nullptr;
linkedNode* p = head;
for (int i = 0; i < pos - 1; ++i)
p = p->next;
return p;
}
void linkedList::insert(int pos, int theElement) {
if (pos < 0 || pos > listSize)
return;
linkedNode* insertNode = new linkedNode();
insertNode->data = theElement;
if (pos == 0) {
head = insertNode;
++listSize;
return;
}
linkedNode* p = getIdx(pos);
insertNode->next = p->next;
p->next = insertNode;
++listSize;
return;
}
void linkedList::erase(int pos) {
if (pos < 0 || pos > listSize)
return;
linkedNode* deleteNode;
if (pos == 0) {
deleteNode = head;
head = head->next;
} else {
linkedNode* p = getIdx(pos);
deleteNode = p->next;
p->next = p->next->next;
delete deleteNode;
--listSize;
}
return;
}
signed main() {
ios::sync_with_stdio(false);
linkedList list;
// You Code Here...
return 0;
}
双向链表:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
struct doubleLinkedNode {
int data;
doubleLinkedNode* prev;
doubleLinkedNode* next;
};
struct doubleLinkedList {
public:
int size() { return listSize; } // 输出链表长度,默认是0
bool empty() { return listSize == 0; } // 输出链表是否为空
doubleLinkedNode* getIdx(int pos); // 得到一个在第pos个位置的doubleLinkedNode类型的指针
void insert(int pos, int theElement); // 在链表的第pos个位置插入元素theElement
void erase(int pos); // 擦除第pos个位置的元素
private:
unsigned int listSize = 0;
doubleLinkedNode* head;
};
doubleLinkedNode* doubleLinkedList::getIdx(int pos) {
if (pos < 0 || pos > listSize)
return nullptr;
doubleLinkedNode* p = head;
for (int i = 0; i < pos - 1; ++i)
p = p->next;
return p;
}
void doubleLinkedList::insert(int pos, int theElement) {
if (pos < 0 || pos > listSize)
return;
doubleLinkedNode* newNode = new doubleLinkedNode();
newNode->data = theElement;
if (pos == 0) {
newNode->next = head;
if (head != nullptr)
head->prev = newNode;
head = newNode;
++listSize;
return;
}
doubleLinkedNode* curNode = getIdx(pos);
newNode->next = curNode->next;
if (curNode->next != nullptr)
curNode->next->prev = newNode;
curNode->next = newNode;
newNode->prev = curNode;
++listSize;
return;
}
void doubleLinkedList::erase(int pos) {
if (pos < 0 || pos > listSize)
return;
doubleLinkedNode* deleteNode = nullptr;
if (pos == 0) {
deleteNode = head;
if (head->next != nullptr)
head->next->prev = nullptr;
head = head->next;
} else {
doubleLinkedNode* curNode = getIdx(pos);
deleteNode = curNode->next;
if (deleteNode->next != nullptr)
deleteNode->next->prev = curNode;
curNode->next = deleteNode->next;
}
--listSize;
delete deleteNode;
return;
}
signed main() {
ios::sync_with_stdio(false);
doubleLinkedList list;
// You Code Here...
return 0;
}