【資料結構】單鏈表-----基本操作
阿新 • • 發佈:2018-12-12
刪除指定位置的節點
void Erase(pList * pplist, pNode pos) { assert(pplist != NULL); assert(pos != NULL); if (*pplist == pos)//如果指向第一個節點 { pNode del = pos; *pplist = (*pplist)->next; free(del); del = NULL; } else { pNode cur = *pplist; while (cur && cur->next != pos)//此處要找到pos的前一個節點,由於要將pos節點free前, //必須將pos的前一節點和後一節點連起來,所以此處不用cur!=pos作為條件 { cur = cur->next; } if (cur != NULL) { cur->next = pos->next; free(pos); pos = NULL; } } } void TestErase() { Node* plist = NULL;//指向第一個節點的指標 pNode pos = NULL; PushFront(&plist, 1); PushFront(&plist, 2); PushFront(&plist, 3); PushFront(&plist, 4); PrintLinkList(plist); pos = Find(plist, 2); if (pos != NULL) { Erase(&plist, pos); } PrintLinkList(plist); DestroyLinkList(&plist); }
刪除一個不是尾節點的節點
void EraseNotTailNode(pNode pos) { pNode del = NULL; assert(pos); assert(pos->next); // del = pos->next; pos->data = pos->next->data; // pos->next = del->next; free(del); del = NULL; } void TestEraseNotNode() { Node* plist = NULL;//指向第一個節點的指標 pNode pos = NULL; PushFront(&plist, 1); PushFront(&plist, 2); PushFront(&plist, 3); PushFront(&plist, 4); PrintLinkList(plist); pos = Find(plist, 3); if (pos != NULL) { EraseNotTailNode(pos); } PrintLinkList(plist); DestroyLinkList(&plist); }
刪除指定值的節點
void Remove(pList* pplist, DataType d) { pNode cur = NULL; pNode prev = NULL; assert(pplist); cur = *pplist; while (cur) { if (cur->data == d) { //第一個節點 if (*pplist == cur) { { *pplist = cur->next; free(cur); cur = NULL; } } else { prev->next = cur->next; free(cur); cur = NULL; } } else { prev = cur; cur = cur->next; } } } void TestRemove() { Node* plist = NULL;//指向第一個節點的指標 pNode pos = NULL; PushBack(&plist, 3); PushBack(&plist, 1); PushBack(&plist, 3); PushBack(&plist, 3); PrintLinkList(plist); Remove(&plist, 1); PrintLinkList(plist); }
刪除所有的指定值
void RemoveAll(pList * pplist, DataType d)
{
pNode cur = NULL;
pNode prev = NULL;
cur = *pplist;
assert(pplist != NULL);
while (cur)
{
if (cur->data == d)//找到了
{
if (*pplist == cur)//第一個節點
{
*pplist = cur->next;
free(cur);
cur = *pplist;
}
else//不是第一個節點
{
prev->next = cur->next;
free(cur);
cur = prev;
}
}
else
{
prev = cur;
cur = cur->next;
}
}
}
在指定位置之前插入一個數值
void Insert(pList * pplist, pNode pos, DataType d)
{
pNode newNode = NULL;
assert(pplist != NULL);
assert(*pplist != NULL);
assert(pos != NULL);
if (pos == *pplist)
{
newNode = BuyNode(d);
newNode->next = *pplist;
*pplist = newNode;
}
else
{
pNode cur = *pplist;
while (cur && cur->next != pos)
{
cur = cur->next;
}
if (cur != NULL)
{
newNode = BuyNode(d);
newNode->next = pos;
cur->next = newNode;
}
}
}
void TestInsert()
{
Node* plist = NULL;//指向第一個節點的指標
pNode pos = NULL;
PushBack(&plist, 1);
PushBack(&plist, 2);
PushBack(&plist, 3);
PrintLinkList(plist);
pos = Find(plist, 2);
if (pos != NULL)
{
Insert(&plist, pos, 8);
}
PrintLinkList(plist);
DestroyLinkList(&plist);
}
由尾至頭的列印節點
void PrintTailToHead(pList plist)
{
pNode tail = NULL;
pNode cur = plist;
pNode prev = NULL;
if (plist == NULL)
{
return;
}
while (cur)
{
tail = cur;
cur = cur->next;
}
while (tail != plist)
{
cur = plist;
while (cur != tail)
{
prev = cur;
cur = cur->next;
}
printf("%d->", tail->data);
tail = prev;
}
printf("%d->NULL\n", tail->data);
}