链表之节点插入、查找删除、遍历打印、遍历释放
2018-01-13 12:13
561 查看
1 /* FILE: p40_linkOperate_main.c */ 2 3 #include "p140_common.h" 4 5 int main(int argc, char *argv[]) 6 { 7 struct node *p; 8 int i; 9 10 printf("insert\n"); 11 for(i=1; i<8; i++) 12 insert(i); 13 print(); 14 15 printf("delete\n"); 16 del(5, &(p)); 17 printf("the being deleted node: %d\n", p->val); 18 print(); 19 20 printf("destroy\n"); 21 destroy(); 22 23 return 0; 24 }
1 /* FILE: p140_common.h */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 struct node{ 7 int val; 8 struct node *next; 9 }; 10 11 extern struct node *head; 12 13 extern int insert(int val); 14 extern int del(int val, struct node **res); 15 extern void print(); 16 extern void destroy();
1 /* FILE: p140_linkOperate_list.c */ 2 3 #include "p140_common.h" 4 5 struct node *head; 6 7 int insert(int val) 8 { 9 struct node *p, *q; 10 11 p = head; 12 q = (struct node *)malloc(sizeof(struct node)); 13 if(q == NULL) 14 return -1; 15 q->val = val; 16 q->next = NULL; 17 18 if(p == NULL) 19 { 20 head = q; 21 } 22 else 23 { 24 while(p->next != NULL) 25 p = p->next; 26 p->next = q; 27 } 28 return 0; 29 } 30 31 /* 形参:要删除节点的数值;修改二级指针指向被删除的节点 */ 32 int del(int val, struct node **res) 33 { 34 struct node *p, *q; 35 36 p = head; 37 if(p == NULL) 38 return -1; 39 if(p->val == val) 40 { 41 *res = p; 42 head = p->next; 43 return 0; 44 } 45 else if(p->next == NULL) 46 return -1; 47 q = p; 48 p = p->next; 49 while(p != NULL) 50 { 51 if(p->val != val) 52 { 53 q = p; 54 p = p->next; 55 } 56 else 57 { 58 *res = p; 59 q->next = p->next; 60 p->next = NULL; 61 return 1; 62 } 63 } 64 return -1; 65 } 66 67 void print() 68 { 69 struct node *p; 70 p = head; 71 while(p!=NULL) 72 { 73 printf("%d ", p->val); 74 p = p->next; 75 } 76 printf("\n"); 77 } 78 79 void destroy() 80 { 81 struct node *p = head; 82 while(p != NULL) 83 { 84 struct node *q; 85 q = p; 86 p = p->next; 87 free(q); 88 } 89 head = NULL; 90 }
1 # FILE: Makefile 2 3 OBJECTS = p140_linkOperate_list.o p140_linkOperate_main.o 4 5 build: $(OBJECTS) 6 gcc -o build $(OBJECTS) 7 p140_linkOperate_list.o: p140_linkOperate_list.c p140_common.h 8 gcc -c p140_linkOperate_list.c 9 p140_linkOperate_main.o: p140_linkOperate_main.c p140_common.h 10 gcc -c p140_linkOperate_main.c 11 12 .PHONY: clean 13 clean: 14 rm build $(OBJECTS)
相关文章推荐
- 链表面试题逆序打印,删除无头非尾节点,非头位置插入,约瑟夫环,查找中间和倒数第k个节点
- 单链表的初始化、查找、删除、插入、释放
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 链表面试题(一):逆序打印链表、无头链表删除插入节点、约瑟夫环、逆置单链表
- 单链表的创建,表长,插入,查找,逆置,中间元素,删除节点,打印
- 单链表的游标实现:插入、删除、查找、打印等功能
- 数据结构:单链表(一)的创建、插入、删除、遍历,打印
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 单向链表的创建、遍历、求长、判存、判空、插入、删除、查找(按位置或元素)、合并
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 建立一个带附加头结点的单链表.实现测长/打印/删除结点/插入结点/逆置/查找中间节点/查找倒数第k个节点/判断是否有环
- 单链表很类似于栈,先插入的后遍历,删除也是删除最后插进来的。但增加的指定查找和指定删除的方法增加了灵活性,可以删除、查找指定key
- 二叉树的遍历 插入 查找 删除 最大值 最小值 前驱 后继节点的查找
- 链表中头和尾节点/指针的常规用法:插入、删除、遍历
- 13.3 链表-按顺序插入和查找删除节点
- malloc的链表,创建链表,遍历链表,插入节点,删除节点
- 带头节点链表的操作(链表的创建、删除、查找、逆转、打印等等)
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 双向链表的功能实现:初始化,插入,按位置插入,按位置删除,按值删除,遍历打印等