日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

指针应用-----链表二

發(fā)布時(shí)間:2025/7/25 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 指针应用-----链表二 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天繼續(xù)完善自己的鏈表,上次已經(jīng)實(shí)現(xiàn)了鏈表的插入、遍歷、銷毀方法,對(duì)于鏈表的插入,我們上次是在頭結(jié)點(diǎn)進(jìn)行插入的,這次,我們來(lái)實(shí)現(xiàn)一個(gè)在任意結(jié)點(diǎn)進(jìn)行插入的方法。

實(shí)現(xiàn)鏈表的另外一種插入方法----在任意位置進(jìn)行插入:

?在實(shí)現(xiàn)它之前,先實(shí)現(xiàn)獲取任意位置的結(jié)點(diǎn)的函數(shù),為便在實(shí)現(xiàn)任意插入時(shí)會(huì)使用到它,先在頭文件中定義:

list.h:

#ifndef _LIST_H_ #define _LIST_H_#include <assert.h>typedef struct node {int data;struct node* next; } node_t;typedef void (*FUNC)(node_t*);node_t* list_insert_front(node_t* head, int data);void list_for_each(node_t* head, FUNC f);node_t* list_free(node_t* head);node_t* list_get_node(node_t* head, int index);//獲取指定位置的結(jié)點(diǎn)#endif /* _LIST_H_ */

具體實(shí)現(xiàn)list.c:

#include "list.h" #include <stdlib.h>node_t* list_insert_front(node_t* head, int data) {node_t* n = (node_t*)malloc(sizeof(node_t));assert(n != NULL);n->data = data;n->next = NULL;if (head == NULL)head = n;else{n->next = head;head = n;}return head; }void list_for_each(node_t* head, FUNC f) {while (head){f(head);head = head->next;} }node_t* list_free(node_t* head) {node_t* tmp;while (head){tmp = head;head = head->next;free(tmp);}return head; }node_t* list_get_node(node_t* head, int index) {assert(index >= 0);int j = 0;while (head && j < index){head = head->next;j++;}if (j == index)//說(shuō)明已經(jīng)找到結(jié)點(diǎn)return head;return head;//說(shuō)明最終head指向NULL,沒(méi)有找到結(jié)點(diǎn)}

上面的也可簡(jiǎn)寫:

node_t* list_get_node(node_t* head, int index) {assert(index >= 0);int j = 0;while (head && j < index){head = head->next;j++;}return head;}

測(cè)試一下:

main.c:

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_free(head);assert(head == NULL);return 0; }

編譯運(yùn)行:

如果沒(méi)有找到呢?

main.c:

int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 5);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_free(head);assert(head == NULL);return 0; }

編譯運(yùn)行:

好了,接著正式來(lái)定義任意插入的函數(shù):

list.h:

#ifndef _LIST_H_ #define _LIST_H_#include <assert.h>typedef struct node {int data;struct node* next; } node_t;typedef void (*FUNC)(node_t*);node_t* list_insert_front(node_t* head, int data);void list_for_each(node_t* head, FUNC f);node_t* list_free(node_t* head);node_t* list_get_node(node_t* head, int index);node_t* list_insert_at(node_t* head, int data, int index);#endif /* _LIST_H_ */

list.c:

#include "list.h" #include <stdlib.h>node_t* list_insert_front(node_t* head, int data) {node_t* n = (node_t*)malloc(sizeof(node_t));assert(n != NULL);n->data = data;n->next = NULL;if (head == NULL)head = n;else{n->next = head;head = n;}return head; }void list_for_each(node_t* head, FUNC f) {while (head){f(head);head = head->next;} }node_t* list_free(node_t* head) {node_t* tmp;while (head){tmp = head;head = head->next;free(tmp);}return head; }node_t* list_get_node(node_t* head, int index) {assert(index >= 0);int j = 0;while (head && j < index){head = head->next;j++;}return head;}node_t* list_insert_at(node_t* head, int data, int index) {assert(index >= 0);if (index == 0)//等于就是頭插入結(jié)點(diǎn),直接調(diào)用現(xiàn)成的方法return list_insert_front(head, data);node_t* p;p = list_get_node(head, index - 1);if (p == NULL){fprintf(stderr, "error insert pos\n");exit(EXIT_FAILURE);}
  //新建一個(gè)節(jié)點(diǎn)node_t* n = (node_t*)malloc(sizeof(node_t));assert(n != NULL);n->data = data;n->next = NULL;
  //將節(jié)點(diǎn)進(jìn)行鏈接n->next = p->next;p->next = n;return head; }

說(shuō)明:

①fprintf表示向什么地方輸出,如fprintf(stdio,"hello!");等價(jià)于printf("hello!");

②exit()表示程序退出,參數(shù)EXIT_FAILURE代表錯(cuò)誤退出,它還有另外一個(gè)宏定義EXIT_SUCCESS,代表成功退出

程序測(cè)試:

main.c:

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_insert_at(head, 15, 1);list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

編譯:

其中的fprintf需要頭文件:

所以在list.c中加入此頭文件:

再次編譯,運(yùn)行:

如果插入失敗會(huì)怎樣呢?

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_insert_at(head, 15, 5);//這個(gè)位置沒(méi)有元素,應(yīng)該會(huì)插入失敗list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

編譯運(yùn)行:

實(shí)現(xiàn)鏈表的刪除方法:

先定義刪除方法:

list.h:

?

#ifndef _LIST_H_ #define _LIST_H_#include <assert.h>typedef struct node {int data;struct node* next; } node_t;typedef void (*FUNC)(node_t*);node_t* list_insert_front(node_t* head, int data);void list_for_each(node_t* head, FUNC f);node_t* list_free(node_t* head);node_t* list_get_node(node_t* head, int index);node_t* list_insert_at(node_t* head, int data, int index);node_t* list_remove_at(node_t* head, int index);#endif /* _LIST_H_ */

?

具體實(shí)現(xiàn),在實(shí)現(xiàn)前,先用一個(gè)簡(jiǎn)單的圖來(lái)解釋下:

具體實(shí)現(xiàn)如下list.c:

node_t* list_remove_at(node_t* head, int index) {assert(index >= 0);node_t* n;if (index == 0)//這是移除首結(jié)點(diǎn){n = head;head = head->next;free(n);}else{node_t* p = list_get_node(head, index - 1);if (p == NULL || p->next == NULL){fprintf(stderr, "error remove pos\n");exit(EXIT_FAILURE);}n = p->next;p->next = n->next;free(n);}return head; }

測(cè)試一下main.c:

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_insert_at(head, 15, 1);list_for_each(head, print_node);putchar('\n');head = list_remove_at(head, 1);list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

編譯運(yùn)行:

如果移除的元素不存在呢?

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_insert_at(head, 15, 1);list_for_each(head, print_node);putchar('\n');head = list_remove_at(head, 5);//這個(gè)元素不存在,移除時(shí)會(huì)出錯(cuò)list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

運(yùn)行:

實(shí)現(xiàn)鏈表的查找方法:

list.h:

?

#ifndef _LIST_H_ #define _LIST_H_#include <assert.h>typedef struct node {int data;struct node* next; } node_t;typedef void (*FUNC)(node_t*);node_t* list_insert_front(node_t* head, int data);void list_for_each(node_t* head, FUNC f);node_t* list_free(node_t* head);node_t* list_get_node(node_t* head, int index);node_t* list_insert_at(node_t* head, int data, int index);node_t* list_remove_at(node_t* head, int index);node_t* list_find(node_t* head, int data, int* ret);//ret代表找到的鏈表的索引位置,返回值代表找到的鏈表結(jié)點(diǎn)#endif /* _LIST_H_ */

?

list.c:

#include "list.h" #include <stdlib.h> #include <stdio.h>node_t* list_insert_front(node_t* head, int data) {node_t* n = (node_t*)malloc(sizeof(node_t));assert(n != NULL);n->data = data;n->next = NULL;if (head == NULL)head = n;else{n->next = head;head = n;}return head; }void list_for_each(node_t* head, FUNC f) {while (head){f(head);head = head->next;} }node_t* list_free(node_t* head) {node_t* tmp;while (head){tmp = head;head = head->next;free(tmp);}return head; }node_t* list_get_node(node_t* head, int index) {assert(index >= 0);int j = 0;while (head && j < index){head = head->next;j++;}return head;}node_t* list_insert_at(node_t* head, int data, int index) {assert(index >= 0);if (index == 0)return list_insert_front(head, data);node_t* p;p = list_get_node(head, index - 1);if (p == NULL){fprintf(stderr, "error insert pos\n");exit(EXIT_FAILURE);}node_t* n = (node_t*)malloc(sizeof(node_t));assert(n != NULL);n->data = data;n->next = NULL;n->next = p->next;p->next = n;return head; }node_t* list_remove_at(node_t* head, int index) {assert(index >= 0);node_t* n;if (index == 0){n = head;head = head->next;free(n);}else{node_t* p = list_get_node(head, index - 1);if (p == NULL || p->next == NULL){fprintf(stderr, "error remove pos\n");exit(EXIT_FAILURE);}n = p->next;p->next = n->next;free(n);}return head; }node_t* list_find(node_t* head, int data, int* ret) {*ret = -1;int i = 0;while (head){if (head->data == data){*ret = i;break;}head = head->next;i++;}return head; }

測(cè)試main.c:

#include "list.h" #include <stdio.h>void print_node(node_t* n) {printf("data=%d ", n->data); }int main(void) {node_t* head = NULL;head = list_insert_front(head, 30);head = list_insert_front(head, 20);head = list_insert_front(head, 10);list_for_each(head, print_node);putchar('\n');node_t* n = NULL;n = list_get_node(head, 1);if (n != NULL)printf("data = %d\n", n->data);elseprintf("not found\n");head = list_insert_at(head, 15, 1);list_for_each(head, print_node);putchar('\n');head = list_remove_at(head, 1);list_for_each(head, print_node);putchar('\n');int ret;n = list_find(head, 20, &ret);if (n != NULL)printf("data = %d index = %d\n", n->data, ret);elseprintf("not found\n");head = list_free(head);assert(head == NULL);return 0; }

編譯,運(yùn)行:

好了,今天的學(xué)習(xí)到這,關(guān)于鏈表,下次會(huì)再次進(jìn)行完善,下次見(jiàn)。

?

轉(zhuǎn)載于:https://www.cnblogs.com/webor2006/p/3484812.html

總結(jié)

以上是生活随笔為你收集整理的指针应用-----链表二的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。