日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/7/25 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 指针应用-----链表二 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天繼續完善自己的鏈表,上次已經實現了鏈表的插入、遍歷、銷毀方法,對于鏈表的插入,我們上次是在頭結點進行插入的,這次,我們來實現一個在任意結點進行插入的方法。

實現鏈表的另外一種插入方法----在任意位置進行插入:

?在實現它之前,先實現獲取任意位置的結點的函數,為便在實現任意插入時會使用到它,先在頭文件中定義:

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);//獲取指定位置的結點#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++;}if (j == index)//說明已經找到結點return head;return head;//說明最終head指向NULL,沒有找到結點}

上面的也可簡寫:

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;}

測試一下:

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; }

編譯運行:

如果沒有找到呢?

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; }

編譯運行:

好了,接著正式來定義任意插入的函數:

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)//等于就是頭插入結點,直接調用現成的方法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; }

說明:

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

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

程序測試:

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中加入此頭文件:

再次編譯,運行:

如果插入失敗會怎樣呢?

#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);//這個位置沒有元素,應該會插入失敗list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

編譯運行:

實現鏈表的刪除方法:

先定義刪除方法:

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_ */

?

具體實現,在實現前,先用一個簡單的圖來解釋下:

具體實現如下list.c:

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; }

測試一下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; }

編譯運行:

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

#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);//這個元素不存在,移除時會出錯list_for_each(head, print_node);putchar('\n');head = list_free(head);assert(head == NULL);return 0; }

運行:

實現鏈表的查找方法:

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代表找到的鏈表的索引位置,返回值代表找到的鏈表結點#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; }

測試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; }

編譯,運行:

好了,今天的學習到這,關于鏈表,下次會再次進行完善,下次見。

?

轉載于:https://www.cnblogs.com/webor2006/p/3484812.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。