您好,登錄后才能下訂單哦!
小編給大家分享一下C語言鏈表的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
頭文件:link_0505.h
/* 鏈表演示 */ #ifndef __LINK_0505 #define __LINK_0505 typedef struct node{ int num; struct node* p_next; }node; typedef struct { node head,tail; }link; //鏈表的初始化函數 void link_init(link *); //鏈表的清理函數 void link_deinit(link *); //判斷鏈表是否空的函數 int link_empty(link *); //判斷鏈表是否滿的函數 int link_full(link *); //統計有效數字個數的函數 int link_size(link *); //在最前邊插入數字的函數 int link_add_head(link *, int ); //在最后邊插入新的數字的函數 int link_append(link *, int ); //把數字按照順序插入到鏈表的函數 int link_insert(link *, int); //刪除最前面數字的函數 int link_remove_head(link *); //刪除最后一個有效數字 int link_remove_tail(link *); //刪除某個給定數字的函數 int link_remove(link *, int ); //獲得第一個有效數字的函數 int link_get_head(link *, int *); //獲得最后一個有效數字的函數 int link_get_tail(link *, int *); //獲得指定編號數字的函數 int link_get(link *, int *, int ); #endif
實現代碼: link_0505.cpp
/* 鏈表演示 */ #include "stdlib.h" #include "link_0505.h" //鏈表的初始化函數 void link_init(link *p_link) { p_link->head.p_next = &(p_link->tail); } //鏈表的清理函數 void link_deinit(link *p_link) { while(p_link->head.p_next != &(p_link->tail)) { node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_last; free(p_mid); p_mid = NULL; } } //判斷鏈表是否空的函數 int link_empty(link *p_link) { return p_link->head.p_next == &(p_link->tail); } //判斷鏈表是否滿的函數 int link_full(link *p_link) { return 0; } //統計有效數字個數的函數 int link_size(link *p_link) { int cnt = 0; node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail)) { cnt++; } } return cnt; } //在最前邊插入數字的函數 int link_add_head(link *p_link, int num) { node *p_temp = (node *)malloc(sizeof(node)); if (!p_temp) { return 0; } p_temp->num = num; node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_temp; p_temp->p_next = p_mid; return 1; } //在最后邊插入新的數字的函數 int link_append(link *p_link, int num) { node *p_tmp = (node *)malloc(sizeof(node)); node *p_node = NULL; if (!p_tmp) { return 0; } p_tmp->num = num; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid == &(p_link->tail)) { p_first->p_next = p_tmp; p_tmp->p_next = p_mid; break; } } return 1; } //把數字按照順序插入到鏈表的函數 int link_insert(link *p_link, int num) { node* p_temp = (node *)malloc(sizeof(node)); node* p_node = NULL; if (!p_temp) { return 0; } p_temp->num = num; p_temp->p_next = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num) { p_first->p_next = p_temp; p_temp->p_next = p_mid; break; } } return 0; } //刪除最前面數字的函數 int link_remove_head(link *p_link) { node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_link->head.p_next == &(p_link->tail)) { return 0; } p_first->p_next = p_last; free(p_mid); p_mid = NULL; } //刪除最后一個有效數字 int link_remove_tail(link *p_link) { node *p_node = NULL; for (p_node = &(p_link->head);p_node !=&(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_last == &(p_link->tail)) { p_first->p_next = p_last; free(p_mid); p_mid = NULL; return 1; } } return 0; } //刪除某個給定數字的函數 int link_remove(link *p_link, int num) { node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail) && p_mid->num == num) { p_first->p_next = p_last; free(p_mid); p_mid = NULL; return 1; } } return 0; } //獲得第一個有效數字的函數 int link_get_head(link *p_link, int *p_num) { if (p_link->head.p_next == &(p_link->tail)) { return 0; } node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_last; *p_num = p_mid->num; return 1; } //獲得最后一個有效數字的函數 int link_get_tail(link *p_link, int *p_num) { node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_last == &(p_link->tail)) { *p_num = p_mid->num; return 1; } } return 0; } //獲得指定編號數字的函數 int link_get(link *p_link, int *p_num, int num) { int cnt = 0; node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail) && cnt == num) { *p_num = p_mid->num; return 1; } cnt++; } return 0; }
測試函數:
/* * 鏈表測試 * */ #include <stdio.h> #include "link_0505.h" int main() { int size = 0, num = 0, val = 0; link lnk = {0}; link_init(&lnk); link_add_head(&lnk, 30); link_add_head(&lnk, 20); link_append(&lnk, 90); link_append(&lnk, 100); link_insert(&lnk, 50); link_insert(&lnk, 60); link_insert(&lnk, 40); link_insert(&lnk, 80); link_insert(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf("%d ", val); } printf("\n"); printf("------------------"); link_remove_head(&lnk); link_remove_tail(&lnk); link_remove(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf("%d ", val); } printf("\n"); link_get_head(&lnk, &val); printf("最前面的數字是%d\n", val); link_get_tail(&lnk, &val); printf("最后面的數字是%d\n", val); link_deinit(&lnk); return 0; }
以上是“C語言鏈表的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。