下面为单链表实现代码,代码仅供参考
#pragma once
typedef int DataType;
#include<malloc.h>
#include<assert.h>
typedef struct SListNode
{
struct SListNode* _next;
DataType _data;
}SListNode;
SListNode* BuySListNode(DataType x);//创建节点
void SListPushFront(SListNode** ppHead, DataType x);//头插节点
void SListPopFront(SListNode** ppHead);//头删
void SListPushBack(SListNode** ppHead, DataType x);//尾插节点
void SListPopBack(SListNode** ppHead);//尾删
SListNode* SListFind(SListNode* pHead, DataType x);//查找节点
bool SListInsest(SListNode** ppHead, SListNode* pos, DataType x);//插入节点
bool SListErase(SListNode** ppHead, SListNode* pos);//删除节点
void SListDestory(SListNode** ppHead);//销毁链表
SListNode* BuySListNode(DataType x)//创建节点
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
assert(node);
node->_data = x;
node->_next = NULL;
return node;
}
void SListPushFront(SListNode** ppHead, DataType x)//头插节点
{
if (*ppHead == NULL){
*ppHead = BuySListNode(x);
}
else{
SListNode* cur = *ppHead;
*ppHead = BuySListNode(x);
(*ppHead)->_next = cur;
}
}
void SListPopFront(SListNode** ppHead)//头删节点
{
if (*ppHead == NULL){
return;
}
else{
SListNode* cur = *ppHead;
*ppHead = cur->_next;
free(cur);
}
}
void SListPushBack(SListNode** ppHead, DataType x)//尾插节点
{
if (*ppHead == NULL){
*ppHead = BuySListNode(x);
}
else{
SListNode* cur = *ppHead;
while (cur->_next){
cur = cur->_next;
}
cur->_next = BuySListNode(x);
}
}
void SListPopBack(SListNode** ppHead)//尾删
{
if (*ppHead == NULL){
return;
}
else {
if ((*ppHead)->_next == NULL) {
free(*ppHead);
*ppHead = NULL;
}
else {
SListNode* cur = *ppHead;
while (cur->_next->_next) {
cur = cur->_next;
}
free(cur->_next);
cur->_next = NULL;
}
}
}
SListNode* SListFind(SListNode* pHead, DataType x)//查找节点
{
if (pHead == NULL){
return NULL;
}
else{
SListNode* cur = pHead;
while (cur){
if (cur->_data == x){
return cur;
}
else{
cur = cur->_next;
}
}
return NULL;
}
}
bool SListInsest(SListNode** ppHead, SListNode* pos, DataType x)//插入节点
{
SListNode* newnode = BuySListNode(x);
if (*ppHead == pos){
newnode->_next = pos;
*ppHead = newnode;
}
else{
SListNode* cur = *ppHead;
while (cur){
if (cur->_next == pos){
cur->_next = newnode;
newnode->_next = pos;
return true;
}
else{
cur = cur->_next;
}
}
}
return false;
}
bool SListErase(SListNode** ppHead, SListNode* pos)//删除节点
{
if (pos == NULL || ppHead == NULL) {
return false;
}
if (*ppHead == pos){
SListNode* cur = *ppHead;
*ppHead = cur->_next;
free(cur);
return true;
}
else
{
SListNode* cur = *ppHead;
while (cur->_next){
if (cur->_next == pos){
cur->_next = pos->_next;
free(pos);
return true;
}
else{
cur = cur->_next;
}
}
}
return false;
}
void SListDestory(SListNode** ppHead)//销毁链表
{
SListNode* cur = *ppHead;
while (cur)
{
SListNode* tmp = cur->_next;
free(cur);
cur = tmp;
}
*ppHead = NULL;
}
链表经典问题,代码仅供参考
void SLitsPrintTailToHead(SListNode* pHead)//从尾到头打印单链表(非递归)
{
SListNode* cur = pHead;
SListNode* tmp = NULL;
while (tmp != pHead){
cur = pHead;
while (cur->_next != tmp){
cur = cur->_next;
}
printf("%d ", cur->_data);
tmp = cur;
}
}
void SListPrintTailToHeadR(SListNode* pHead)//从尾到头打印单链表(递归)
{
if (pHead == NULL){
return;
}
SListPrintTailToHeadR(pHead->_next);
printf("%d ", pHead->_data);
}
void SListDelNonTailNode(SListNode* pos)//删除一个无头单链表的非尾节点(不能遍历链表)
{
SListNode* cur = pos->_next;
pos->_next = cur->_next;
pos->_data = cur->_data;
free(cur);
}
void SListInsertFrontNode(SListNode* pos, DataType x)//在无头单链表的一个节点前插入一个节点(不能遍历链表)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->_next = pos->_next;
pos->_next = node;
node->_data = pos->_data;
pos->_data = x;
}
SListNode* SListJosephCircle(SListNode* pHead, size_t k)//单链表实现约瑟夫环(JosephCircle)
{
SListNode* sub = pHead;
while (sub->_next){
sub = sub->_next;
}
sub->_next = pHead;
SListNode* cur = pHead;
while (cur->_next != cur){
size_t count = k;
while (count--){
cur = cur->_next;
}
SListNode* next = cur->_next;
cur->_next = next->_next;
cur->_data = next->_data;
free(next);
}
return cur;
}
SListNode* SListReverse(SListNode* list)//逆置/反转单链表
{
if (list == NULL || list->_next == NULL) {
return list;
}
SListNode* pve = list;
SListNode* cur = list->_next;
pve = NULL;
while (cur->_next){
SListNode* next = cur->_next;
cur->_next = pve;
pve = cur;
cur = next;
}
cur->_next = pve;
return cur;
}
void SListBubbleSort(SListNode* list)//单链表排序(冒泡排序)
{
SListNode* end = list;
while (end){
end = end->_next;
}
while (list != end){
SListNode* cur = list;
int count = 0;
while (cur->_next != end){
if (cur->_data > cur->_next->_data){
int tmp = cur->_data;
cur->_data = cur->_next->_data;
cur->_next->_data = tmp;
++count;
}
cur = cur->_next;
}
if (count == 0) {
break;
}
end = cur;
}
}
SListNode* SListMerge(SListNode* list1, SListNode* list2)//合并两个有序链表,合并后依然有序
{
SListNode* L1 = list1;
SListNode* L2 = list2;
SListNode* L3 = NULL;
while (L1 && L2){
if (L1 && L2 == NULL) {
SListPushBack(&L3, L1->_data);
L1 = L1->_next;
}
else if(L1 == NULL && L2)
{
SListPushBack(&L3, L2->_data);
L2 = L2->_next;
}
else if (L1->_data <= L2->_data){
SListPushBack(&L3, L1->_data);
L1 = L1->_next;
}
else{
SListPushBack(&L3, L2->_data);
L2 = L2->_next;
}
}
return L3;
}
SListNode* SListFindMidNode(SListNode* list)//查找单链表的中间节点,要求只能遍历一次链表(快慢指针法)
{
SListNode* slow = list;
SListNode* quick = list;
while (quick->_next->_next){
slow = slow->_next;
quick = quick->_next->_next;
if (quick->_next == NULL){
break;
}
}
return slow;
}
SListNode* SListFindTailKNode(SListNode* list, size_t k)//删除链表的倒数第K个结点
{
SListNode* cur = list;
while (cur->_next != NULL){
cur = cur->_next;
}
while (k){
SListNode* sub = list;
while (sub->_next != cur){
sub = sub->_next;
}
if (k == 1){
sub->_next = cur->_next;
free(cur);
}
else{
cur = sub;
}
k--;
}
return list;
}
int SListCycleLen(SListNode* meetNode)//链表环长度
{
SListNode* slow = meetNode;
SListNode* quick = meetNode->_next;
int length = 0;
while (quick == slow){
slow = slow->_next;
quick = quick->_next->_next;
++length;
}
return length + 1;
}
SListNode* SListEntryNode(SListNode* list, SListNode* meetNode)//返回链表环入口
{
SListNode* begain = list;
while (begain){
meetNode = meetNode->_next;
if (begain == meetNode){
return begain;
break;
}
begain = begain->_next;
}
return NULL;
}
size_t Slenth(SListNode* list)//带环链表的非环链长度
{
size_t lenth = 0;
while (list->_next != SListIsCycle(list)) {
++lenth;
list = list->_next;
}
return lenth;
}
size_t SListlslenth(SListNode* list, SListNode* Entry)//带环链表长度
{
size_t lenth = 0;
while (list->_next == Entry) {
++lenth;
list = list->_next;
}
return lenth + SListCycleLen(Entry);
}
SListNode* SListIsCrossNode(SListNode* list1, SListNode* list2)//判断两个链表是否相交,若相交,求交点。(假设链表不带环)
{
assert(list1 == list2);
SListNode* L1 = list1->_next;
SListNode* L2 = list2->_next;
while (L1)
{
while (L2)
{
if (L1 == L2)
{
return L2;
}
L2 = L2->_next;
}
L1 = L1->_next;
}
return NULL;
}
SListNode* SListCrossNode(SListNode* list1, SListNode* list2)//判断两个链表是否相交,若相交,求交点。(假设链表可能带环)
{
SListNode* L1 = list1;
SListNode* L2 = list2;
size_t count = 0;
size_t lastcount = 0;
if (SListlslenth(L1, SListIsCycle(list1)) >= SListlslenth(L2, SListIsCycle(list2))){
count = SListlslenth(L1, SListIsCycle(list1)) - SListlslenth(L2, SListIsCycle(list2));
lastcount = SListlslenth(L1, SListIsCycle(list1));
}
else{
count = SListlslenth(L2, SListIsCycle(list2)) - SListlslenth(L1, SListIsCycle(list1));
lastcount = SListlslenth(L2, SListIsCycle(list2));
}
while (count){
if (SListlslenth(L1, SListIsCycle(list1)) >= SListlslenth(L2, SListIsCycle(list2))){
L1 = L1->_next;
--count;
}
else{
L2 = L2->_next;
--count;
}
}
size_t lenth = 0;
while (lastcount){
++lenth;
if (L1 == L2 && lenth < Slenth(list1)){
return L1;
}
else{
return SListEntryNode(list2, SListIsCycle(list2));
}
L1 = L1->_next;
L2 = L2->_next;
lastcount--;
}
return NULL;
}