C語言實現單鏈表實現方法
C語言實現單鏈表實現方法
鏈表和我們之前實現過的順序表一樣,都是簡單的數據結構,鏈表分為單向鏈表、雙向鏈表、循環(huán)鏈表。而單向鏈表又分為兩種實現方法,一種為帶頭節(jié)點的單鏈表,一種為不帶頭節(jié)點的單鏈表。我們來具體看看不帶頭節(jié)點的單鏈表的實現
單鏈表:它是一種鏈式存儲的線性表,用一組地址任意的存儲單元存放線性表的數據元素,稱存儲單元為一個節(jié)點。
今天我們來實現一些單鏈表的簡單接口
先看看單鏈表的結構: (為了通用性,我們將類型重命名為DataType)
typedef int DataType;
//鏈表
typedef struct Node
{
DataType *data;
struct Node *next;
}Node, *pNode, *pList;
接下來看看我們要實現的接口:
void InitLinkList(pList *pplist);//初始化鏈表 pNode BuyNode(DataType d);//創(chuàng)建鏈表節(jié)點 void PushBack(pList *pplist, DataType d);//尾插 void PopBack(pList *pplist);//尾刪 void PushFront(pList *pplist, DataType d);//頭插 void PopFront(pList *pplist);//頭刪 void PrintList(pList plist);//打印鏈表 pNode Find(pList plist, DataType d);//查找指定元素 void Remove(pList *pplist, DataType d);//刪除指定的一個元素 void RemoveAll(pList *pplist, DataType d);//刪除指定的所有元素 void Insert(pList *pplist, pNode pos, DataType d);//指定位置的后面插入 void Erase(pList *pplist, pNode pos);//指定位置刪除 void DestroyList(pList *pplist);//銷毀鏈表
來看看每個接口的具體實現:
pNode BuyNode(DataType d)
{
pNode newNode = (pNode)malloc(sizeof(Node));
if (newNode == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
newNode->data = d;
newNode->next = NULL;
return newNode;
}
void InitLinkList(pList *pplist)
{
assert(pplist);
*pplist = NULL;
}
void PushBack(pList *pplist, DataType d)
{
assert(pplist);
pNode newNode = BuyNode(d);
pNode cur = *pplist;
//鏈表沒有節(jié)點
if (*pplist == NULL)
{
*pplist = newNode;
return;
}
//鏈表有節(jié)點
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
void PopBack(pList *pplist)
{
pNode cur = *pplist;
pNode prev = NULL;
assert(pplist);
//鏈表沒有節(jié)點
if (*pplist == NULL)
{
return;
}
//鏈表有一個節(jié)點
if (cur->next == NULL)
{
free(*pplist);
*pplist = NULL;
return;
}
//鏈表有兩個及兩個以上節(jié)點
while (cur->next != NULL)
{
prev = cur;//prev中保存的是cur之前的那個節(jié)點
cur = cur->next;
}
prev->next = NULL;
free(cur);
}
void PushFront(pList *pplist, DataType d)
{
pNode newNode = BuyNode(d);
//pNode cur = *pplist;
assert(pplist);
////鏈表沒有節(jié)點
//if (*pplist == NULL)
//{
// *pplist = newNode;
//}
////鏈表有節(jié)點
newNode->next = *pplist;
*pplist = newNode;
}
void PopFront(pList *pplist)
{
pNode cur = *pplist;
assert(pplist);
//鏈表為空
if (*pplist == NULL)
{
return;
}
*pplist = cur->next;
free(cur);
cur = NULL;
}
void PrintList(pList plist)
{
pNode cur = plist;
while (cur)
{
printf("%d-->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
pNode Find(pList plist, DataType d)
{
pNode cur = plist;
while (cur)
{
if (cur->data == d)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Remove(pList *pplist, DataType d)
{
pNode cur = *pplist;
pNode prev = NULL;
assert(pplist);
if (cur == NULL)
{
return;
}
while (cur)
{
if (cur->data == d)
{
pNode del = cur;
if (cur == *pplist)
{
*pplist = cur->next;
}
prev->next = cur->next;
free(del);
del = NULL;
return;
}
else
{
prev = cur;
cur = cur->next;
}
}
}
void RemoveAll(pList *pplist, DataType d)
{
pNode cur = *pplist;
pNode prev = NULL;
assert(pplist);
if (*pplist == NULL)
{
return;
}
while (cur)
{
if (cur->data == d)
{
pNode del = cur;
if (cur == *pplist)
{
*pplist = cur->next;
}
else
{
prev->next = cur->next;
}
cur = cur->next;
free(del);
del = NULL;
}
else
{
prev = cur;
cur = cur->next;
}
}
}
//在pos后面插入一個元素
void Insert(pList *pplist, pNode pos, DataType d)
{
pNode newNode = BuyNode(d);
assert(pplist);
assert(pos);
if (*pplist == NULL)
{
PushFront(pplist, d);
return;
}
newNode->next = pos->next;
pos->next = newNode;
}
void Erase(pList *pplist, pNode pos)
{
assert(pplist);
assert(pos);
//要刪除的是尾節(jié)點
if (pos->next == NULL)
{
PopBack(pplist);
}
//刪除的是非尾節(jié)點
else
{
pNode del = pos->next;
pos->data = pos->next->data;
pos->next = pos->next->next;
free(del);
del = NULL;
}
}
void DestroyList(pList *pplist)
{
assert(pplist);
pNode cur = *pplist;
while (cur)
{
pNode del = cur;
cur = cur->next;
printf("del:%d\n", del->data);
free(del);
del = NULL;
}
}
由于這些接口都較為簡單,所以不進行具體的測試展示,讀者可以自行測試
以上就是C語言實現單鏈表實現方法,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:使用Libmicrohttpd搭建內嵌(本地)服務器的方法
欄 目:C語言
下一篇:詳談c++跨平臺編碼的問題
本文標題:C語言實現單鏈表實現方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1264.html
您可能感興趣的文章
- 04-02c語言函數調用后清空內存 c語言調用函數刪除字符
- 04-02c語言的正則匹配函數 c語言正則表達式函數庫
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言中對數函數的表達式 c語言中對數怎么表達
- 04-02c語言用函數寫分段 用c語言表示分段函數
- 04-02c語言編寫函數冒泡排序 c語言冒泡排序法函數
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段函數
- 04-02C語言中怎么打出三角函數 c語言中怎么打出三角函數的值
- 04-02c語言調用函數求fibo C語言調用函數求階乘


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法


