歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

C++實現鏈表的基本操作及測試用例

今天實現了下鏈表的基本操作,包括節點的創建,頭插尾插,頭刪尾刪,一次遍歷尋找鏈表的中間節點,尋找鏈表的倒數第x個節點,刪除無頭鏈表的非尾節點,鏈表的逆置,代碼如下:

#include "SLinkList.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
void PrintList(SListNode* pHead)//從指針位置打印鏈表
{
while (pHead)
{
printf("->%d", pHead->data);
pHead = pHead->next;
}
printf("\n");
}
static SListNode* _BuyNode(DataType x)//創建新的節點
{
SListNode*ret = (SListNode*)malloc(sizeof(SListNode));
ret->next = NULL;
ret->data = x;
if (ret)
{
return ret;
}
printf("創建節點失敗\n");//創建失敗給出提示
return NULL;
}
void PushBack(SListNode* & pHead, DataType x)//尾插
{
if (pHead == NULL)
{
pHead = _BuyNode(x);
}
else
{
SListNode*tail = pHead;
while (tail->next)
{
tail = tail->next;
}
tail->next = _BuyNode(x);
}
}
void PopBack(SListNode* & pHead)//尾刪
{
if (pHead == NULL)
{
return;
}
else if (pHead->next->next == NULL)
{
free(pHead->next);
pHead = NULL;
}
else
{
SListNode* tail = pHead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void PushFront(SListNode* & pHead, DataType x)//頭插
{
if (pHead == NULL)
{
pHead = _BuyNode(x);
}
else
{
SListNode*tmp = _BuyNode(x);
tmp->next = pHead;
pHead = tmp;
}
}
void PopFront(SListNode* & pHead)//t頭刪
{
if (pHead == NULL)
{
return;
}
else
{
SListNode*tail = pHead;
pHead = pHead->next;
tail->next = NULL;
free(tail);
}
}
SListNode* Find(SListNode *pHead, DataType x)//以x查找節點,若存在返回給節點,若不存在返回空
{
if (pHead == NULL)
{
return NULL;
}
SListNode* tail = pHead;
while (tail)
{
if (tail->data == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
void Insert(SListNode* & pos, DataType x)//所給位置後插入一節點
{
SListNode*tmp = _BuyNode(x);
if (pos == NULL)
{
pos = tmp;
}
else
{
tmp->next = pos->next;
pos->next = tmp;
}
}
void Erase(SListNode * &pos)//刪除無頭鏈表的非尾節點
{
if (pos == NULL)
{
return;
}
else if (pos->next == NULL)
{
printf("該節點為尾節點,無法刪除\n");
}
else
{
SListNode*tmp = pos->next;
pos->data = pos->next->data;
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}
}
void ReveseList(SListNode * &pHead)//鏈表的逆置
{
SListNode*tail = pHead;
while (tail->next)
{
SListNode*tmp=NULL;
tmp = tail->next;
tail->next = tail->next->next;
tmp->next = pHead;
pHead = tmp;
}
}
SListNode* FindminList(SListNode*pHead)//一次遍歷,尋找鏈表的中間節點
{
assert(pHead);
SListNode *quick=pHead;
SListNode *slow=pHead;
while (quick)
{
slow = slow->next;
if (quick->next)
quick = quick->next->next;
else
return slow;
}
return slow;
}
SListNode* FindListPosList(SListNode*pHead, int lastpos)//尋找鏈表的倒數第lastpos個節點
{
SListNode *quick = pHead;
SListNode *slow = pHead;
while (quick&&lastpos--)
{
quick = quick->next;
}
if (quick == NULL)
{
return slow;
}
while (quick)
{
quick = quick->next;
slow = slow->next;
}
return slow;
}

    測試用例如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 #include <stdio.h>
#include <stdlib.h>
#include "SLinkList.h"
void Test1()//PushBack,PrintList,PopBack
{
SListNode*pHead=NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
PushBack(pHead, 4);
PushBack(pHead, 5);
PrintList(pHead);
PopBack(pHead);
PopBack(pHead);
PrintList(pHead);
PopBack(pHead);
PopBack(pHead);
PopBack(pHead);
PrintList(pHead);
}
void Test2()//PushFront,Popfront
{
SListNode*pHead = NULL;
PushFront(pHead, 5);
PushFront(pHead, 4);
PushFront(pHead, 3);
PushFront(pHead, 2);
PushFront(pHead, 1);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
PopFront(pHead);
PrintList(pHead);
}
void Test3()//Find,Insert
{
SListNode*pHead = NULL;
PushFront(pHead, 5);
PushFront(pHead, 4);
PushFront(pHead, 3);
PushFront(pHead, 2);
PushFront(pHead, 1);
Insert(pHead, 0);
pHead = Find(pHead, 3);
PrintList(pHead);
Insert(pHead, 4);
PrintList(pHead);
}
void Test4()//Erase
{
SListNode*pHead = NULL;
SListNode*tmp = NULL;
PushFront(pHead, 5);
PushFront(pHead, 4);
PushFront(pHead, 3);
PushFront(pHead, 2);
PushFront(pHead, 1);
tmp = Find(pHead, 5);
Erase(tmp);
PrintList(pHead);
}
void Test5()//ReveseList
{
SListNode*pHead = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
PushBack(pHead, 4);
PushBack(pHead, 5);
    ReveseList(pHead);
PrintList(pHead);
}
void Test6()//FindLastposList
{
SListNode*pHead = NULL;
SListNode*ret = NULL;
PushBack(pHead, 1);
PushBack(pHead, 2);
PushBack(pHead, 3);
PushBack(pHead, 4);
PushBack(pHead, 5);
    ret=FindListPosList(pHead, 2);
printf("%d\n", ret->data);
ret = FindListPosList(pHead, 6);
printf("%d\n", ret->data);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
system("pause");
return 0;
}

如有不足希望指正,有疑問希望提出

Copyright © Linux教程網 All Rights Reserved