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

