一、刪除單鏈表的非尾節點ide
解題思路:按照通常的思路單鏈表刪除節點是必須知道其前驅節點才能刪除,而在本題中不知道前驅節點,因此轉換思路,先將須要刪除的節點跟其後繼節點的數據域交換,而後再刪除既可。排序
void EraseNotTail(pLinkNode pos) { assert(pos); pLinkNode del = NULL;//刪除的節點 del = pos->next; pos->data = pos->next->data; pos->next = pos->next->next; free(del); del = NULL; }
二、冒泡排序單鏈表it
void BubbleSort(pList * pHead) { pLinkNode cur = *pHead; pLinkNode end = NULL; int change = 1; assert(pHead); if (*pHead == NULL) { return; } else if (cur->next == NULL) { return; } while ((cur != end)&&(change))//控制排序幾輪 { change = 0; while ((cur)&&(cur->next != end))//排序 { change = 1; if (cur->data > cur->next->data) { DataType tmp = cur->data; cur->data = cur->next->data; cur->next->data = tmp; } cur = cur->next; } end = cur; cur = *pHead;//使得單鏈表從頭開始 } }
三、在當前節點前插入一個數據xclass
思路解析:按照通常的思路其應該須要找到節點的前驅節點,才能插入,可是能夠換一個思路,能夠先把節點插入到其後方,而後再將兩個節點的數據域進行交換便可很好的解決該問題。List
void InsertFrontNode(pLinkNode pos, DataType x) { pLinkNode newNode = BuyNode(x); assert(pos); newNode->next = pos->next; pos->next = newNode; DataType tmp = pos->data; pos->data = pos->next->data; pos->next->data = tmp; }