数据结构-单链表(C语言代码)
數(shù)據(jù)結(jié)構(gòu)純屬新手,小白一枚,歡迎批評(píng)指正!
直接上代碼OVO!
定義結(jié)構(gòu)體
typedef struct Node {int data; //數(shù)據(jù)域struct Node* next; //指針域 }Node;單鏈表只有數(shù)據(jù)域和指向下一個(gè)結(jié)點(diǎn)的指針域。
創(chuàng)建鏈表
頭插法
//頭插法,從頭插入類似于棧 void headInsert(Node* L,int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;L->data++;//插入后鏈表長(zhǎng)度+1 }尾插法
//尾插法 void tailInsert(Node* L, int data) {Node* node =L;for (int i = 0; i < L-> data; i++) {node = node->next;}Node* n = (Node*)malloc(sizeof(Node));n->data = data;n->next = NULL;node->next = n;L->data++;//插入后鏈表長(zhǎng)度+1 }刪除結(jié)點(diǎn)
//刪除鏈表結(jié)點(diǎn) int deleteList(Node* L, int data) {Node* preNode = L;Node* node = L->next;while (node) {if (node->data == data) {preNode->next = node->next;free(node);L->data--;//刪除后鏈表長(zhǎng)度-1return TRUE;}preNode = node;node = node->next;}return FALSE; }遍歷鏈表
//遍歷鏈表 void printList(Node* L) {Node* node = L->next;while (node){printf("node=%d\n",node->data);node = node->next;} }全部代碼:
#include <stdio.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0typedef struct Node {int data; //數(shù)據(jù)域struct Node* next; //指針域 }Node;//創(chuàng)建鏈表 Node* initList() {Node* L = (Node*)malloc(sizeof(Node)); //創(chuàng)建頭指針L->data = 0;L->next = NULL;//剛創(chuàng)建,頭指針指向空!return L; }//頭插法,從頭插入類似于棧 void headInsert(Node* L,int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;L->data++;//插入后鏈表長(zhǎng)度+1 }//尾插法 void tailInsert(Node* L, int data) {Node* node =L;for (int i = 0; i < L-> data; i++) {node = node->next;}Node* n = (Node*)malloc(sizeof(Node));n->data = data;n->next = NULL;node->next = n;L->data++;//插入后鏈表長(zhǎng)度+1 }//刪除鏈表結(jié)點(diǎn) int deleteList(Node* L, int data) {Node* preNode = L;Node* node = L->next;while (node) {if (node->data == data) {preNode->next = node->next;free(node);L->data--;//刪除后鏈表長(zhǎng)度-1return TRUE;}preNode = node;node = node->next;}return FALSE; }//遍歷鏈表 void printList(Node* L) {Node* node = L->next;while (node){printf("node=%d\n",node->data);node = node->next;} }int main(void) {Node* L = initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);tailInsert(L, 4);tailInsert(L, 5);tailInsert(L, 6);printList(L);//測(cè)試刪除不存在的結(jié)點(diǎn)if (deleteList(L, 10)) {printf("success delete 10\n");}else {printf("fail delete 10\n");}printf("表長(zhǎng):%d\n", L->data);//測(cè)試刪除存在的結(jié)點(diǎn)if (deleteList(L, 6)) {printf("success delete 6\n");}else {printf("fail delete 6\n");}printf("表長(zhǎng):%d\n", L->data);printList(L);return 0; }運(yùn)行截圖:
總結(jié)
線性表的鏈?zhǔn)酱鎯?chǔ)相比于順序存儲(chǔ),有兩大優(yōu)勢(shì):
1.鏈?zhǔn)酱鎯?chǔ)的數(shù)據(jù)元素在物理結(jié)構(gòu)沒有限制,當(dāng)內(nèi)存空間中沒有足夠大的連續(xù)的內(nèi)存空間供順序表使用時(shí),可能使用鏈表能解決問題。(鏈表每次申請(qǐng)的都是單個(gè)數(shù)據(jù)元素的存儲(chǔ)空間,可以利用上一些內(nèi)存碎片)
2.鏈表中結(jié)點(diǎn)之間采用指針進(jìn)行鏈接,當(dāng)對(duì)鏈表中的數(shù)據(jù)元素實(shí)行插入或者刪除操作時(shí),只需要改變指針的指向,無需像順序表那樣移動(dòng)插入或刪除位置的后續(xù)元素,簡(jiǎn)單快捷。
鏈表和順序表相比,不足之處在于,當(dāng)做遍歷操作時(shí),由于鏈表中結(jié)點(diǎn)的物理位置不相鄰,使得計(jì)算機(jī)查找起來相比較順序表,速度要慢。
總結(jié)
以上是生活随笔為你收集整理的数据结构-单链表(C语言代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重写Java Object类中的equa
- 下一篇: 数据结构-单循环链表(C语言代码)