C語言之單向鏈表詳解及實(shí)例代碼
1,單向鏈簡潔。
單向鏈表(單鏈表)是鏈表的一種,其特點(diǎn)是鏈表的鏈接方向是單向的,對(duì)鏈表的訪問要通過順序讀取從頭部開始;鏈表是使用指針進(jìn)行構(gòu)造的列表;又稱為結(jié)點(diǎn)列表,因?yàn)殒湵硎怯梢粋€(gè)個(gè)結(jié)點(diǎn)組裝起來的;其中每個(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語言單向聯(lián)表的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(96.獨(dú)一無二的二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(96.獨(dú)一無二的二叉搜索樹),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
使用代碼驗(yàn)證linux子進(jìn)程與父進(jìn)程的關(guān)系
Linux下父進(jìn)程可以使用fork 函數(shù)創(chuàng)建子進(jìn)程,但是當(dāng)父進(jìn)程先退出后,子進(jìn)程會(huì)不會(huì)也退出呢?通過下面這個(gè)小實(shí)驗(yàn),我們能夠很好的看出來2014-02-02
C語言實(shí)現(xiàn)奇數(shù)階魔方陣的方法
這篇文章主要介紹了C語言實(shí)現(xiàn)奇數(shù)階魔方陣的方法,涉及數(shù)組及相關(guān)數(shù)學(xué)函數(shù)的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
詳解C++ STL模擬實(shí)現(xiàn)forward_list
forward_list是C++ 11新增的容器,它支持從容器中的任何位置快速插入和移除元素的容器,不支持快速隨機(jī)訪問。本文將模擬實(shí)現(xiàn)forward_list,感興趣的可以了解一下2023-01-01
C++實(shí)現(xiàn)洗牌發(fā)牌排序功能的示例代碼
本篇文章主要介紹了C++實(shí)現(xiàn)洗牌發(fā)牌排序功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

