typedef char LinkType
typedef struct LinkNode {
LinkType data;
struct LinkNode* next;
} LinkNode;
1)逆序打印单链表
使用低轨思想,递归出口 head == NULL
void LinkListReversePrint(LinkNode* head){
if(head == NULL){
return;
}
LinkListReversePrint(head->next);
printf(" [%c|%p] ",head->data,head);
}
2)不允许遍历链表,在pos之前插入
void LinkListInsertBefore(LinkNode** head, LinkNode* pos, LinkType value){
if(head == NULL){
return;
}
if(*head == NULL){
return;
}
if(pos == NULL){
return;
}
if(pos == *head){
LinkListPushFront(head,value);
return;
}
LinkNode* cur = *head;
for(;cur != NULL;cur = cur->next){
if(cur->next == pos){
LinkNode* new_node = CreatNode(value);
new_node->next = cur->next;
cur->next = new_node;
return;
}
}
}
3)约瑟夫环
LinkNode* LinkListJosephCircle(LinkNode** head,size_t value){
if(head == NULL){
//非法输入
return NULL;
}
if(*head == NULL){
//空链表
return NULL;
}
size_t i = 0;
LinkNode* cur = *head;
while(cur->next!=cur){
for(i=1;i<value;++i){
cur = cur->next;
}
printf("要删除的节点是[%c]\n",cur->data);
LinkNode* to_delete = cur->next;
cur->data = to_delete->data;
cur->next = to_delete->next;
DestoryNode(to_delete);
}
return cur;
}
找到每次需要删除的元素,同样使用移花接木的思想,就不用去找当前元素的前节点位置
4)单链表逆置
void LinkListReverse(LinkNode** head){
if(head == NULL){
//非法输入
return;
}
if(*head == NULL){
//空链表
return;
}
LinkNode* pre = *head;
LinkNode* cur = pre->next;
while(cur!=NULL){
LinkNode* tail = cur->next;
cur->next = *head;
pre->next = tail;
*head = cur;
cur = tail;
}
}
5)单链表的冒泡排序
void LinkListBubblesort(LinkNode* head){
if(head == NULL){
return;
}
if(head->next == NULL){
return;
}
LinkNode* tail = head;
while(tail->next!=NULL){
tail = tail->next;
}
LinkNode* cur = head;
for(;cur->next!=NULL;cur = cur->next){
LinkNode* n = head;
for(;n->next!=tail;n=n->next){
if(n->data > n->next->data){
LinkType tmp = n->data;
n->data = n->next->data;
n->next->data = tmp;
}
}
if(n->data > tail->data){
LinkType tmp = n->data;
n->data = tail->data;
tail->data = tmp;
}
tail = n;
}
}
6)将两个有序链表合并为一个有序链表
LinkNode* LinkListMerge(LinkNode* head1, LinkNode* head2){
if(head1 == NULL && head2 == NULL){
return NULL;
}
LinkNode* cur1 = head1;
LinkNode* cur2 = head2;
LinkNode* head;
LinkNode* tail;
if(head1->data < head2->data){
head = cur1;
tail = cur1;
cur1=cur1->next;
}else{
head = cur2;
tail = cur2;
cur2=cur2->next;
}
while(cur1!=NULL && cur2!=NULL){
if(cur1->data <cur2->data){
tail->next = cur1;
cur1 = cur1->next;
tail = tail->next;
}else{
tail->next = cur2;
cur2 = cur2->next;
tail = tail->next;
}
}
if(cur1 == NULL && cur2 != NULL){
tail = cur2;
}else if(cur1 != NULL && cur2 == NULL){
tail = cur1;
}
return head;
}
7)找到链表中间的节点
LinkNode* FindMidNode(LinkNode* head){
if(head == NULL){
return NULL;
}
if(head->next == NULL){
return head;
}
LinkNode* fast = head;
LinkNode* slow = head;
// size_t count = 0;
// while(fast!=NULL){
// count++;
// fast = fast->next;
// }
// size_t m = count/2;
// while(m--){
// slow = slow->next;
// }
// return slow;
while(slow->next != NULL & fast->next->next != NULL){
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
快慢指针的思想,快指针走两步,慢指针走一步,快指针走到链表结尾,慢指针刚好走到中间
8)找到倒数第K个节点
LinkNode* FindLastKNode(LinkNode* head, size_t k){
if(head == NULL){
return NULL;
}
if(head->next == NULL){
return head;
}
LinkNode* cur = head;
size_t count = 0;
while(cur!=NULL){
++count;
cur = cur->next;
}
if(k<=0 && k>count){
return NULL;
}
LinkNode* fast = head;
LinkNode* slow = head;
k = k-1;
while(k--){
fast = fast->next;
}
while(fast->next!=NULL){
fast = fast->next;
slow = slow->next;
}
// printf("[%c]\n",fast->data);
return slow;
}
快慢指针,快指针走k步之后两个指针在同时走,这样快指针到了结尾,慢指针指到k个节点
9)删除倒数第K个节点
void EraseLastKNode(LinkNode** head, size_t k){
if(head == NULL){
return;
}
if(*head == NULL){
return;
}
LinkNode* cur = *head;
size_t count = 0;
while(cur!=NULL){
++count;
cur = cur->next;
}
if(k<=0 && k>count)
return;
// if(head->next == NULL){
// LinkListPopFront(head);
// }
printf("%lu\n",k);
if(k==count){
LinkListPopFront(head);
return;
}
LinkNode* fast = *head;
LinkNode* slow = *head;
while(k--){
fast = fast->next;
}
while(fast->next!=NULL){
fast = fast->next;
slow = slow->next;
}
LinkNode* to_delete = slow->next;
slow->next = to_delete->next;
DestoryNode(to_delete);
// printf("[%c]\n",slow->data);
// printf("[%c]\n",fast->data);
}