本文主要介绍链表的相关代码实现。如下:
//---------------------------链表综合示例----------------
typedef struct Node
{
int data;
struct Node* next;
}SLIST;
SLIST* Slist_Create()
{
SLIST *pHead = NULL,*pCur = NULL,*PNew = NULL;
pHead = (SLIST*)malloc(sizeof(SLIST));
if(pHead == NULL)
return NULL;
pHead->data = 0;
pHead->next = NULL;
int data = 0;
pCur = pHead;
printf("Please input data:");
scanf("%d",&data);
while(data != -1)
{
PNew = (SLIST*)malloc(sizeof(SLIST));
if(PNew == NULL)
return NULL;
PNew->data = data;
PNew->next = NULL;
pCur->next = PNew;
pCur = PNew;
printf("Please input data:");
scanf("%d",&data);
}
return pHead;
}
int Slist_Print(SLIST* pHead)
{
if(pHead == NULL)
return -1;
SLIST *tmp = pHead->next;
while(tmp)
{
printf("%d ",tmp->data);
tmp = tmp->next;
}
return 0;
}
int Slist_NodeInsert(SLIST* pHead, int x, int y)
{
if(pHead == NULL)
return -1;
SLIST* tmp = (SLIST*)malloc(sizeof(SLIST));
tmp->data = y;
tmp->next = NULL;
SLIST *pPre = pHead;
SLIST *pCur = pHead->next;
while(pCur)
{
if(pCur->data == x)
break;
pPre = pCur;
pCur = pCur->next;
}
tmp->next = pCur;
pPre->next = tmp;
return 0;
}
int Slist_NodeDel(SLIST* pHead, int x)
{
if(pHead == NULL)
return -1;
SLIST *pDel = (SLIST*)malloc(sizeof(SLIST));
if(pDel == NULL)
return -1;
pDel->data = x;
pDel->next = NULL;
SLIST *pPre = pHead;
SLIST *pCur = pHead->next;
while(pCur)
{
if(pCur->data == x)
{
pDel = pCur;
break;
}
pPre = pCur;
pCur = pCur->next;
}
if(pCur == NULL)
{
printf("not find the number of :%d",x);
return -2;
}
pPre->next = pCur->next;
if(pDel != NULL)
free(pDel);
return -1;
}
int Slist_Destory(SLIST* pHead)
{
if(pHead == NULL)
return -1;
SLIST* tmp = pHead;
while(pHead)
{
tmp = pHead->next;
free(pHead);
pHead = tmp;
}
return 0;
}
int Slist_Reverse(SLIST* pHead)
{
if(pHead == NULL || pHead->next == NULL || pHead->next->next == NULL)
return -1;
SLIST *pPre = pHead->next;
SLIST *pCur = pHead->next->next;
pPre->next = NULL;
while(pCur)
{
SLIST *pNext = pCur->next;
pCur->next = pPre;
pPre = pCur;
pCur = pNext;
}
pHead->next = pPre;
return 0;
}
int main()
{
SLIST* myPHead;
int ret = 0;
myPHead = Slist_Create();
if(myPHead == NULL)
return -1;
ret = Slist_Print(myPHead);
printf("\n");
ret = Slist_NodeInsert(myPHead,10,12);
ret = Slist_Print(myPHead);
printf("\n");
ret = Slist_NodeDel(myPHead,12);
ret = Slist_Print(myPHead);
printf("\n");
ret = Slist_Reverse(myPHead);
ret = Slist_Print(myPHead);
printf("\n");
ret = Slist_Destory(myPHead);
return 0;
}
下面是剑指offer中有关链表的处理算法:
//---------------------------链表综合---剑指offer中----------------
//-----------------O(1)时间删除一个链表结点----------------
typedef struct LinkedListNode
{
int m_data;
struct LinkedListNode *m_next;
}LinkedListNode;
void DeleteNode(LinkedListNode **pHead, LinkedListNode *pDelNode)
{
if(pHead == NULL || *pHead == NULL || pDelNode == NULL)
return;
if(pDelNode->m_next != NULL)//删除的结点不是尾结点
{
LinkedListNode *pNext = pDelNode->m_next;
pDelNode->m_data = pNext->m_data;
pDelNode->m_next = pNext->m_next;
delete pDelNode;
pNext = NULL;
}
else if(*pHead == pDelNode)//链表只有一个节点
{
delete pDelNode;
pDelNode = NULL;
*pHead = NULL;
}
else //删除的结点为尾结点
{
LinkedListNode *pNode = *pHead;
while(pNode->m_next != pDelNode)
{
pNode = pNode->m_next;
}
pNode->m_next = NULL;
delete pDelNode;
pNode = NULL;
}
}
//----------------在排序的链表中删除重复的结点(包含该节点)----------------
void DeleteDuplication(LinkedListNode **pHead)
{
if(pHead == NULL || *pHead == NULL)
return;
LinkedListNode *pPre = nullptr;
LinkedListNode *pNode = *pHead;
while(pNode != nullptr)
{
LinkedListNode *pNext = pNode->m_next;
bool flag = false;
if(pNext != nullptr && pNext->m_data == pNode->m_data)
flag = true;
if(!flag)
{
pPre = pNode;
pNode = pNext;
}
else
{
int value = pNode->m_data;
LinkedListNode *pDelNode = pNode;
while(pDelNode != nullptr && pDelNode->m_data == value)
{
pNext = pDelNode->m_next;
delete pDelNode;
pDelNode = nullptr;
pDelNode = pNext;
}
if(pPre == nullptr)
*pHead = pNext;
else
pPre->m_next = pNext;
pNode = pNext;
}
}
}
//----------------在排序的链表中删除重复的结点(保留一个该节点)----------------
void DeleteDuplication2(LinkedListNode **pHead)
{
if(pHead == NULL || *pHead == NULL)
return;
LinkedListNode *pPre = *pHead;//---------
LinkedListNode *pNode = *pHead;
while(pNode != nullptr)
{
LinkedListNode *pNext = pNode->m_next;
bool flag = false;
if(pNext != nullptr && pNext->m_data == pNode->m_data)
flag = true;
if(!flag)
{
pPre = pNode;
pNode = pNext;
}
else
{
int value = pNode->m_data;
LinkedListNode *pDelNode = pNode->m_next;//-----
pre = pNode;//-----
while(pDelNode != nullptr && pDelNode->m_data == value)
{
pNext = pDelNode->m_next;
delete pDelNode;
pDelNode = nullptr;
pDelNode = pNext;
}
//------
pPre->m_next = pNext;
pNode = pNext;
}
}
}
//----------------链表中倒数第k个结点----------------
//方法一
LinkedListNode* FindKthToTail(LinkedListNode *pHead, unsigned int k)
{
if(pHead == NULL || k <=0)
return NULL;
LinkedListNode *pAhead = pHead;
LinkedListNode *pBehind = nullptr;
for(unsigned int i=0;i<k-1;i++)
{
//pAhead = pAhead->m_next;
if(pAhead->m_next != nullptr)
pAhead = pAhead->m_next;
else
return nullptr;
}
pBehind = pHead;
while(pAhead->m_next != nullptr)
{
pAhead = pAhead->m_next;
pBehind = pBehind->m_next;
}
return pBehind;
}
//方法二
LinkedListNode* FindKthToTail2(LinkedListNode *pHead, unsigned int k)
{
if(pHead == NULL || k <=0)
return NULL;
LinkedListNode *pAhead = pHead;
LinkedListNode *pBehind = nullptr;
for(unsigned int i=0; i<k;i++)
{
pAhead = pAhead->m_next;
if(pAhead == nullptr)
return nullptr;
}
pBehind = pHead;
while(pAhead!= nullptr)
{
pAhead = pAhead->m_next;
pBehind = pBehind->m_next;
}
return pBehind;
}
//----------------求链表的中间节点----------------*********************************
//思路:定义两个节点,一个一次走一步,一个一次走两步,当真的快的到末尾时,慢的就为中间节点
LinkedListNode *MidNode(LinkedListNode *pHead)
{
if(pHead == NULL)
return NULL;
LinkedListNode *p1 = pHead;
LinkedListNode *p2 = pHead;
while(p2->m_next != NULL)
{
p1 = p1->m_next;
p2 = p2->m_next;
if(p2->m_next)
p2 = p2->m_next;
}
return p1;
}
//----------------环形链表中环的入口节点----------------
ListNode* MeetingNode(ListNode* pHead)
{
if(pHead == NULL)
return NULL;
ListNode* pSlow = pHead->m_next;
if(pSlow == NULL)
return NULL;
ListNode* pFast = pSlow->m_next;
while(pFast != NULL && pSlow != NULL)
{
if(pFast == pSlow)
return pFast;
pSlow = pSlow->m_next;
pFast = pFast->m_next;
if(pFast!=NULL)
pFast = pFast->m_next;
}
return NULL;
}
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
ListNode* meetNode = MeetingNode(pHead);//1 确定是环
if(meetNode == NULL)
return NULL;
ListNode* pNode = meetNode;
int numLoop = 1;
while(pNode->m_next != meetNode)//2 确定环的节点数目
{
numLoop++;
pNode = pNode->m_next;
}
pNode = pHead;
for(int i=0;i<numLoop;i++)
pNode = pNode->m_next;
ListNode* pNode2 = pHead;
while(pNode != pNode2)
{
pNode = pNode->m_next;
pNode2 = pNode2->m_next;
}
return pNode;
}