C語言實現(xiàn)單鏈表的示例詳解
更新時間:2023年09月19日 10:40:58 作者:gnip
給需要考研的同學(xué)一個參考,單鏈表作為常見數(shù)據(jù)結(jié)構(gòu)的一種,這里記錄C語言實現(xiàn)單鏈表,文章通過代碼示例介紹的非常詳細,具有一頂?shù)膮⒖純r值,需要的朋友可以參考下
概述
給需要考研的同學(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)單鏈表的示例詳解的詳細內(nèi)容,更多關(guān)于C語言實現(xiàn)單鏈表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談C語言中include""與include<>的區(qū)別
C語言中包含文件有兩種包含符號,一個是<>尖括號,另一個是""雙引號。那么這兩個有什么區(qū)別呢?本文就詳細的介紹一下,感興趣的可以了解一下2021-06-06
C++ 中"emplace_back" 與 "push_back" 的區(qū)別
這篇文章主要介紹了C++ 中"emplace_back" 與 "push_back" 的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-04-04
使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下2015-08-08
C++?Boost?CircularBuffer算法超詳細精講
Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱2022-11-11

