C語(yǔ)言之單向鏈表詳解及實(shí)例代碼
1,單向鏈簡(jiǎn)潔。
單向鏈表(單鏈表)是鏈表的一種,其特點(diǎn)是鏈表的鏈接方向是單向的,對(duì)鏈表的訪問(wèn)要通過(guò)順序讀取從頭部開(kāi)始;鏈表是使用指針進(jìn)行構(gòu)造的列表;又稱為結(jié)點(diǎn)列表,因?yàn)殒湵硎怯梢粋€(gè)個(gè)結(jié)點(diǎn)組裝起來(lái)的;其中每個(gè)結(jié)點(diǎn)都有指針成員變量指列表中的下一個(gè)結(jié)點(diǎn);列表是由結(jié)點(diǎn)構(gòu)成,由head指針指向第一個(gè)成為表頭的結(jié)點(diǎn)而終止于最后一個(gè)指向nuLL的指針;
2,例子要求:
根據(jù)示例代碼中的例子,完成單向鏈表(single linked list)中的以字符串為數(shù)據(jù)的鏈表的插入、刪除以及查找,并支持單向鏈表的反轉(zhuǎn);
3,代碼實(shí)現(xiàn)。
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <memory.h>
#include <malloc.h>
//節(jié)點(diǎn)的定義
typedef struct Node {
void *data; //數(shù)據(jù)域 //鏈域
struct Node *next;
} NodeStruct, *pNode;
pNode head = NULL;
typedef char (*pCompareFunc)(void *a, void *b);
typedef void* (*pChar)(void *p);
// 字符串判斷
int str_compare(void *a, void *b) {
char *pa = (char*)a;
char *pb = (char*)b;
return strcmp(pa , pb);
}
// 分配一個(gè)節(jié)點(diǎn)
pNode allocate_node(void *data, pChar char_func) {
pNode node = allocate();
node->data = char_func(data);
return node;
}
// 創(chuàng)建節(jié)點(diǎn)
pNode allocate() {
void *p = malloc(sizeof(NodeStruct));
pNode node = (pNode)p;
node->next = NULL;
node->data = NULL;
return node;
}
// 添加一個(gè)節(jié)點(diǎn)
void insertNode(pNode node){
if (head == null){
head=node;
}
else {
node->next = head;
head = node;
}
}
void* char_char(void *p) {
char* pa = (char*)malloc(sizeof(char));
memcpy(pa, p, sizeof(char));
return pa;
}
// 初始化節(jié)點(diǎn)
pNode allocate_node(void *data, pChar char_func) {
pNode node = allocate();
node->data = char_func(data);
return node;
}
// 釋放資源
void free_list(pNode node) {
pNode next = node;
while (next != NULL) {
if (next->data != NULL)
free(next->data);
pNode temp = next;
next = next->next;
free(temp);
}
}
// 1.1 添加一個(gè)節(jié)點(diǎn)
void insert(pNode node) {
if (head == NULL)
head = node;
else {
node->next = head;
head = node;
}
}
//1.2查找
int str_search(void* data,pCompareFunc compare){
pNode next = head;
pNode prev = NULL;
while (next != NULL ) {
if (compare(data, next->data) == 0) {
// 如果查找到了,就退出返回1
return 1;
break;
}
prev = next;
next = next->next;
}
// 如果一直查找不到,就返回0
return 0;
}
// 1.3刪除節(jié)點(diǎn)
void remove(void* data,pCompareFunc compare) {
pNode next = head;
pNode prev = NULL;
while (next != NULL) {
if (compare(data, next->data) == 0) {
if (prev == NULL) {
head = next->next;
next->next = NULL;
free_list(next);
} else {
prev->next = next->next;
next->next = NULL;
free_list(next);
}
break;
}
prev = next;
next = next->next;
}
}
//1.4反轉(zhuǎn)
void invert_order()
{
node *This,*prev;
p=head.next;
This=NULL;
while(p) {
prev=This;
This=p;
p=p->next;
This->next=prev;
}
head.next=This;
}
void main(){
// 1單向鏈表
char a1[] = 'aaa1';
char a2[] = 'aaa2';
char a3[] = 'aaa3';
// 1.1添加
insertNode(allocate_node(a1, init_char));
insertNode(allocate_node(a2, init_char));
insertNode(allocate_node(a3, init_char));
// 1.2查找
int flag = 0;
flag = str_search(&a2,str_compare);
// 1.3刪除
remove(&a2,str_compare);
// 1.4反轉(zhuǎn)
invert_order();
}
以上就是對(duì) C語(yǔ)言單向聯(lián)表的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
上一篇:c++中數(shù)字與字符串之間的轉(zhuǎn)換方法(推薦)
欄 目:C語(yǔ)言
下一篇:詳解C語(yǔ)言gets()函數(shù)與它的替代者fgets()函數(shù)
本文標(biāo)題:C語(yǔ)言之單向鏈表詳解及實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2028.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


