說明1:以下**在vs2017中編譯通過,讀者使用時可以直接將標頭檔案(linklist.h),原始檔(linklist.c),主檔案(main.c)中的內容直接拷貝過去,即可編譯執行!
說明2:這是帶頭結點的單向鍊錶,因為帶頭結點時,更好維護!
說明3:圖示
**標頭檔案:linklist.h(函式的宣告)
#pragma once
#include
#include
#include
#include
#define elemtype int
//結點型別
typedef
struct listnode
listnode,
*plistnode;
//鍊錶型別
typedef
struct linklist
linklist;
//void
initlist
(linklist* list)
;listnode*
createnode
(elemtype data)
;void
push_back
(linklist* list, elemtype data)
;void
push_front
(linklist* list, elemtype data)
;void
showlist
(linklist* list)
;void
pop_back
(linklist* list)
;void
pop_front
(linklist* list)
;bool empty
(linklist* list)
;void
insert_by_value
(linklist* list, elemtype data)
;plistnode find_data
(linklist* list, elemtype key)
;int
length
(linklist* list)
;void
delete_by_value
(linklist* list, elemtype key)
;void
delete_by_value_traditaonal
(linklist* list, elemtype key)
;void
sort
(linklist* list)
;void
reverse
(linklist* list)
;//資料反轉
void
clear
(linklist* list)
;void
destory
(linklist* list)
;plistnode find
(linklist* list, elemtype key)
;plistnode prior
(linklist* list,elemtype posdata)
;//求posdata結點的前驅
plistnode next
(linklist* list,elemtype posdata)
;//求posdata結點的後繼
標頭檔案:linklist.c(函式的定義)
#include
"linklist.h"
void
initlist
(linklist* list)
listnode*
createnode
(elemtype data)
void
push_back
(linklist * list, elemtype data)
void
push_front
(linklist * list, elemtype data)
void
showlist
(linklist * list)
printf
(".nul\n");
}void
pop_back
(linklist * list)
free
(list->tail)
; list->tail = pmove;
list->tail->next =
null
; list->size--;}
void
pop_front
(linklist * list)
bool empty
(linklist * list)
//有序插入(公升序)
void
insert_by_value
(linklist * list, elemtype data)
if(pmove->next ==
null
)//插在最後面,因為data大於所有結點,需要更改list->tail的指向【尾部插入,更改tail指向】
newnode->next = pmove->next;
pmove->next = newnode;
list->size++;}
plistnode find_data
(linklist * list, elemtype key)
return pmove;
}int
length
(linklist * list)
void
delete_by_value
(linklist * list, elemtype key)
if(pfind == list->tail)
//最後乙個結點
else
if(pfind->next == list->tail)
//倒數第二個結點(必須單獨考慮,list->tail指向要發生改變)
else
}void
delete_by_value_traditaonal
(linklist * list, elemtype key)
if(pfind == list->head->next)
//刪除的是第乙個結點
pop_front
(list)
;else
if(pfind == list->tail)
//刪除的是最後乙個結點
pop_back
(list)
;else
}void
sort
(linklist * list)
if(pmove->next ==
null
) pmove1->next = pmove->next;
pmove->next = pmove1;}}
void
reverse
(linklist * list)
}//只保留頭結點,其他結點進行釋放
void
clear
(linklist * list)
list->tail = list->head;
list->size =0;
}void
destory
(linklist * list)
plistnode find
(linklist * list, elemtype key)
return pmove;
}plistnode prior
(linklist * list, elemtype posdata)
plistnode next
(linklist * list, elemtype posdata)
標頭檔案:main.c(測試檔案)**
#include
"linklist.h"
intmain()
單向迴圈鍊錶的定義與實現
說明1 以下 在vs2017中編譯通過,讀者使用時可以直接將標頭檔案 clinklist.h 原始檔 clinklist.c 主檔案 main.c 中的內容直接拷貝過去,即可編譯執行!說明2 圖示 標頭檔案 clinklist.h 函式的宣告 ifndef clinklist h define cl...
實現單向鍊錶
鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...
實現單向鍊錶
鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...