链表的基本操作
链表的基本操作
1.链表的建立
注意:std::string是C++类型,建立/删除时最好使用new/delete而不是malloc/free.
点击展开/收起
typedef struct Node{
string name;
int size;
int weight;
struct Node* next;
}Node;
Node* addNode(Node* head,string name,int size,int weight){
Node* newNode = new Node;
newNode->name = name;
newNode->size = size;
newNode->weight = weight;
newNode->next = NULL;
if(head == NULL){
head = newNode;
return head;
}
Node* now = head;
while(now->next != NULL){
now = now->next;
}
now->next = newNode;
return head;
}
2.链表的删除
点击展开/收起
Node* removeNode(Node* head,string name){
if(head->name == name){
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* now = head;
while(now->next != NULL && now->next->name != name){
now = now->next;
}
Node* temp = now->next;
now->next = now->next->next;
delete temp;
return head;
}
Node* deleteList(Node* head){
Node* now = head;
while(now != NULL){
Node* temp = now;
now = now->next;
delete temp;
}
return head;
}
3.链表的排序
1.插入排序
基本思路:将链表中的每个元素插入已经排好序的序列之中。
点击展开/收起
bool comp(Node* a,Node* b){
if(a->size < b->size) return true;
if(a->size == b->size && a->weight > b->weight) return true;
if(a->size == b->size && a->weight == b->weight && a->name < b->name) return true;
return false;
}
Node* sortList(Node* head){
if(head == NULL || head->next == NULL) return head;
Node* dummy = new Node;
Node* now = head;
while(now){
Node* temp = now->next;
Node* pos = dummy;
while(pos->next != NULL && comp(pos->next,now)){
pos = pos->next;
}
now->next = pos->next;
pos->next = now;
now = temp;
}
head = dummy->next;
free(dummy);
return head;
}
2.冒泡排序
点击展开/收起
void sortContainers() {
if (head->next == NULL || head->next->next == NULL) return;
int swapped;
Container *ptr1;
Container *lptr = NULL;
do {
swapped = 0;
ptr1 = head->next;
while (ptr1->next != lptr) {
if (compare(ptr1, ptr1->next) > 0) {
// 交换两个节点的数据(简单方式)
char tempName[100];
int tempSize, tempWeight;
strcpy(tempName, ptr1->name);
tempSize = ptr1->size;
tempWeight = ptr1->weight;
strcpy(ptr1->name, ptr1->next->name);
ptr1->size = ptr1->next->size;
ptr1->weight = ptr1->next->weight;
strcpy(ptr1->next->name, tempName);
ptr1->next->size = tempSize;
ptr1->next->weight = tempWeight;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
3.归并排序
核心:运用快/慢指针模拟数组中的二分法。
点击展开/收起
Node* merge(Node* left, Node* right) {
Node dummy;
Node* tail = &dummy;
while (left != NULL && right != NULL) {
if (comp(left, right)) {
tail->next = left;
left = left->next;
} else {
tail->next = right;
right = right->next;
}
tail = tail->next;
}
tail->next = (left != NULL) ? left : right;
return dummy.next;
}
Node* mergeSort(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
// 找中间节点
Node* slow = head;
Node* fast = head->next;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
Node* mid = slow->next;
slow->next = NULL;
// 递归排序
Node* left = mergeSort(head);
Node* right = mergeSort(mid);
return merge(left,right);
}
4.链表的反转
基本思路:将原先的链表按从头到尾的顺序依次接到新链表(初始为空)的前面。
点击展开/收起
Node* reverseList(Node* head){
Node* ans = NULL;
Node* now = head;
while(now != NULL){
Node* temp = now->next;
now->next = ans;
ans = now;
now = temp;
}
return ans;
}

浙公网安备 33010602011771号