說明1:以下**在vs2017中編譯通過,讀者使用時可以直接將標頭檔案(clinklist.h),原始檔(clinklist.c),主檔案(main.c)中的內容直接拷貝過去,即可編譯執行!
說明2:圖示
標頭檔案:clinklist.h(函式的宣告)
#ifndef _clinklist_h_
#define _clinklist_h_
#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結點的後繼
#endif
// ! _clinklist_h_
原始檔:clinklist.c(函式的定義)
#include
"clinklist.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 = list->head;
list->size--;}
void
pop_front
(linklist* list)
bool empty
(linklist* list)
void
insert_by_value
(linklist* list, elemtype data)
if(pmove->next == list->tail && pmove->next->data < data)
//最後面插入
else
}plistnode find_data
(linklist* list, elemtype key)
if(pmove == list->head)
return
null
;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 == list->tail && pmove->next->data < pmove1->data)
else}}
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
"clinklist.h"
intmain()
單向鍊錶的定義與實現
說明1 以下 在vs2017中編譯通過,讀者使用時可以直接將標頭檔案 linklist.h 原始檔 linklist.c 主檔案 main.c 中的內容直接拷貝過去,即可編譯執行!說明2 這是帶頭結點的單向鍊錶,因為帶頭結點時,更好維護!說明3 圖示 標頭檔案 linklist.h 函式的宣告 pr...
單向迴圈鍊錶
單向迴圈鍊錶.cpp 定義控制台應用程式的入口點。include stdafx.h include include clinklist.h using namespace std int tmain int argc,tchar argv int n 5 測試空鍊錶 clinklistclist a...
迴圈單向鍊錶
typedef struct list list 初始化乙個迴圈單向鍊錶 void list init list head 判斷鍊錶是否為空 int is list empty list head 往迴圈單向鍊錶中插入乙個元素 prev 在prev元素後面插入 void list insert li...