C語(yǔ)言實(shí)現(xiàn)單鏈表的示例詳解
概述
給需要考研的同學(xué)一個(gè)參考,單鏈表作為常見(jiàn)數(shù)據(jù)結(jié)構(gòu)的一種,這里記錄C語(yǔ)言實(shí)現(xiàn)單鏈表。
代碼
函數(shù)原型和結(jié)構(gòu)體聲明
#include<stdio.h> #include<assert.h> #include<stdlib.h> #define true 1 #define false 0 #define bool char //鏈表數(shù)據(jù)類型 typedef int ElementType; //鏈表節(jié)點(diǎn)結(jié)構(gòu)體 typedef struct LNode { ElementType data; struct LNode* next; } LNode; bool intLNode(LNode** head); bool insertLNodeFromPos(LNode* head, int pos, ElementType data); bool inserteEndLNodeFromTargetValue(LNode* head, ElementType target, ElementType data); bool inserteBeforeLNodeFromTargetValue(LNode* head, ElementType target, ElementType data); bool deleteFromPos(LNode* head, int pos); //鏈表按值刪除(帶頭節(jié)點(diǎn)) bool deleteFromTargetValue(LNode* head, LNode* node); LNode* query(LNode* head, ElementType target);
main函數(shù)
int main() { LNode *head ; intLNode(&head); insertLNodeFromPos(head, 1, 1); insertLNodeFromPos(head, 2, 2); insertLNodeFromPos(head, 1, 101); inserteEndLNodeFromTargetValue(head, 2, 221); inserteBeforeLNodeFromTargetValue(head, 2, 333); //deleteFromPos(head, 3); deleteFromTargetValue(head, head->next->next->next); printf("%d",head->next->next->next->data); //printf("%d", query(head, 101)->data); return 0; }
初始化(帶頭節(jié)點(diǎn))
bool intLNode(LNode** head) { *head = (LNode*)malloc(sizeof(LNode)); if (*head == NULL) { return false; } (*head)->data = 0; (*head)->next = NULL; return true; }
鏈表按位置插入(帶頭節(jié)點(diǎn))
bool insertLNodeFromPos(LNode* head,int pos, ElementType data) { //邊界值判斷 if (pos < 1) { return false; } LNode* current = head; int index = 0; while (current!=NULL) { if (index == pos - 1) { break; } current = current->next; index++; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return false; } //創(chuàng)建新節(jié)點(diǎn) LNode* newNode = (LNode*)malloc(sizeof(LNode)); if (newNode == NULL) { return false; } //更改指針指向 newNode->data = data; newNode->next = current->next; current->next = newNode; return true; }
鏈表按數(shù)據(jù)值后插入(帶頭節(jié)點(diǎn))
bool inserteEndLNodeFromTargetValue(LNode* head, ElementType target, ElementType data) { LNode* current = head; while (current != NULL) { if (current->data == target) { break; } current = current->next; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return false; } //創(chuàng)建新節(jié)點(diǎn) LNode* newNode = (LNode*)malloc(sizeof(LNode)); if (newNode == NULL) { return false; } //更改指針指向 newNode->data = data; newNode->next = current->next; current->next = newNode; return true; }
鏈表按數(shù)據(jù)值前插入(帶頭節(jié)點(diǎn))
bool inserteBeforeLNodeFromTargetValue(LNode* head, ElementType target, ElementType data) { LNode* current = head; while (current != NULL) { if (current->data == target) { break; } current = current->next; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return false; } //創(chuàng)建新節(jié)點(diǎn) LNode* newNode = (LNode*)malloc(sizeof(LNode)); if (newNode == NULL) { return false; } //更改指針指向 newNode->data = current->data; newNode->next = current->next; current->next = newNode; current->data = data; return true; }
鏈表按位置刪除(帶頭節(jié)點(diǎn))
bool deleteFromPos(LNode* head, int pos) { if (pos < 1) { return false; } LNode* current = head; int index = 0; while (current != NULL) { if (index==pos-1) { break; } current = current->next; index++; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return false; } //更改指針指向 LNode* temp = current->next; current->next = current->next->next; //釋放斷開(kāi)節(jié)點(diǎn)的內(nèi)存空間 free(temp); return true; }
鏈表按指定節(jié)點(diǎn)刪除(帶頭節(jié)點(diǎn))
bool deleteFromTargetValue(LNode* head,LNode *node) { LNode* current = head; while (current != NULL) { //找到需要?jiǎng)h除的節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) if (current->next == node) { break; } current = current->next; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return false; } //更改指針指向 LNode* temp = current->next; current->next = current->next->next; free(temp); return true; }
鏈表查找對(duì)應(yīng)的節(jié)點(diǎn)
LNode* query(LNode* head, ElementType target) { LNode* current = head; while (current != NULL) { if (current->data == target) { break; } current = current->next; } //確保當(dāng)前沒(méi)有超出范圍 if (current == NULL) { return NULL; } return current; }
以上就是C語(yǔ)言實(shí)現(xiàn)單鏈表的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言實(shí)現(xiàn)單鏈表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言之棧和堆(Stack && Heap)的優(yōu)缺點(diǎn)及其使用區(qū)別
本篇文章主要介紹了什么是棧(Stack) 、什么是堆( Heap),以及棧和堆的優(yōu)缺點(diǎn),同時(shí)介紹了應(yīng)該什么時(shí)候使用堆和棧,有需要的朋友可以參考下2015-07-07C++實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)校人員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++教程之進(jìn)制轉(zhuǎn)換的實(shí)現(xiàn)方法
在C++中進(jìn)行進(jìn)制轉(zhuǎn)換可以通過(guò)標(biāo)準(zhǔn)庫(kù)函數(shù)或自定義算法實(shí)現(xiàn),本文主要為大家整理了兩種常見(jiàn)場(chǎng)景的轉(zhuǎn)換方法及示例代碼,有需要的小伙伴可以根據(jù)需求進(jìn)行選擇2025-04-04用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī)
這篇文章主要介紹了用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī),其中棧數(shù)組的部分非常值得學(xué)習(xí),需要的朋友可以參考下2015-07-07C/C++: Inline function, calloc 對(duì)比 malloc
以下是對(duì)c/c++中的malloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下2016-07-07C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒(méi)有參數(shù),通過(guò)清晰的實(shí)例源代碼,詳細(xì)解釋了它們?cè)诤瘮?shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01C語(yǔ)言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01