C语言实现链表的相关操作
来源:互联网 发布:mac 环境变量 编辑:程序博客网 时间:2024/05/21 11:19
自己动手写一遍链表的的一些常见的操作有利于自己对链表这种数据结构的掌握。
一下是我自己码的,进作为参考。
/* 链表 */struct note {int count;struct note *next;}; #include <stdio.h>#include <stdlib.h>#define NUM 10void creat_list_head(struct note *); /* 创建一个单链表(前插) */void creat_list_foot(struct note *); /* 创建一个单链表(后插)*/void print_list(struct note *); /* 打印一个单链表 */int search_order(struct note *, int); /* 按序号查找单链表 */struct note *search_value(struct note *, int); /* 按值查找单链表 */void delete_list(struct note *, int); /* 删除单链表某个元素 */void insert_list(struct note *, int, int); /* 单链表中插入某个元素 */void total_delete(struct note *); /* 删除整个个链表 */bool circle_list(struct note *); /* 判断一个链中是否有环 */int main(){struct note *head;head = (struct note *)(malloc(sizeof(struct note)));head->count = NUM; /* 建立一个带头结点的空链表 */head->next = NULL;//creat_list_head(head); /* 建立链表 */creat_list_foot(head);//delete_list(head, 4); /* 删除第4个元素 */insert_list(head, 4, 22);total_delete(head);print_list(head); /* 打印链表 *///printf ("%d", search_order(head, 4)); /* 查找链表第4个元素 */getchar();return 0;}/* * 创建一个单链表 前插法 */void creat_list_head(struct note *head){int i;struct note *p;for (i = NUM; i > 0; i--) {p = (struct note *)(malloc(sizeof(struct note)));p->count = i-1;p->next = head->next;head->next = p;}}/* * 创建一个单链表 后插法 */void creat_list_foot(struct note *head){int i;struct note *p, *foot;foot = head;for (i = NUM; i > 0; i--) {p = (struct note *)(malloc(sizeof(struct note)));p->count = i-1;p->next = NULL;foot->next = p; /* 新加入的地址给上一个的尾指针 */foot = p; /* 尾指针指向最后一个 */}}/* * 打印单链表 */void print_list(struct note *head){struct note *p = head->next;while (p) {printf ("%d\n", p->count);p = p->next;}}/* * 链表的查找 序号查找 查找第 i 个元素 */int search_order(struct note *head, int i){int j = 1;struct note *p = head->next;while (p && j++ < i) {p = p->next;}return !p ? -1 : p->count; /* 如果 p 是空指针 */}/* * 链表的查找 按值查找 查找值 n */struct note *search_value(struct note *head, int n){struct note *p;p = head->next;while (p && p->count != n)p = p->next;return p;}/* * 链表的删除 删除第 i 个元素 注意: 首先要找到第 i-1 个元素 */void delete_list(struct note *head, int i){int j = 1, temp_count;struct note *p, *temp;p = head; /* 与查找不同的是 p 直接指向 head */while (p && j++ < i) /* 先找到第 i-1 个元素 */p = p->next; /* p 存储的是第 j 个元素的首地址 */if (!p) /* p 指向的是第 i-1 个元素的首地址 */printf("Error"); temp = p->next; /* 保存指向第 i 个元素的指针、数据以备释放 */ temp_count = temp->count; p->next = temp->next; free (temp);}/* * 链表的插入 将 n 插到第 i-1 个元素的后面 注意: 首先要找到第 i-1 个元素 */void insert_list(struct note *head, int i, int n){int j = 1;struct note *p, *new_note;p = head;new_note = (struct note *)(malloc(sizeof(struct note))); /* 新建一个节点 */new_note->count = n;new_note->next = NULL;while (p && j++ < i)p = p->next;if (!p)printf ("Error");new_note->next = p->next;p->next = new_note;}/* * 删除整个链表 */void total_delete(struct note *head){struct note *p, *temp;p = head->next;head->next = NULL; /* 将表头置为空表 */while (p) {temp = p->next;free(p);p = temp;}}/* * 判断一个链表中是否有环 */bool circle_list(struct note *head){struct note *p, *temp1, *temp2;p = head->next;if (p = NULL) /* 无节点 */return false;if (p->next == p) /* 自环 */return true;temp1 = p; /* 定义两个指针,一个运动快,一个慢,如果慢的能赶上快的,那就是有环 */temp2 = p->next;while (temp1 != temp2 && temp2 != NULL && temp2->next != NULL) {temp1 = temp1->next;temp2 = temp2->next->next;}if (temp1 == temp2)return true;return false;}
以上只是单链表的操作,我觉得其他的链表都差不多就没有再花时间去边,以后遇到了在慢慢摸索,其实原理都差不多。
希望互相学习,共同进步。
0 0
- c语言实现链表的相关的操作
- C语言实现链表的相关操作
- 双向链表的相关操作--C语言实现
- C语言实现二叉排序树的相关操作
- C语言:静态顺序表的实现和相关操作
- C语言实现链表插入,删除相关操作
- 【数据结构】链表及相关操作(C语言实现)
- C语言链队的相关操作
- C语言中单向链表的相关操作
- C语言描述:单向链表的相关操作
- C语言 链表相关 操作
- 单链表顺序存储相关操作的c语言实现
- 数据结构单链表的相关操作(linux下实现)C语言
- C语言实现单链表相关操作
- 链表的实现与操作(C语言实现)
- 静态链表的实现与操作(C语言实现)
- 循环链表的实现与操作(C语言实现)
- 双向链表的实现与操作(C语言实现)
- 【大数问题】 HDOJ 1316 How Many Fibs?
- 【BZOJ】【P1038】【ZJOI2008】【瞭望塔】【题解】【二分+模拟退火】
- Linq to XML 增删改查
- 手机游戏软件大爆炸时代
- 认识hasLayout——IE浏览器css bug的一大罪恶根源
- C语言实现链表的相关操作
- Codeforces Round #264 (Div. 2)
- Cocos2dx引擎优化 自定义资源包系统
- memset函数实现
- 多线程编程4------面试题使用资源互斥类AtomicInteger
- 在自遮挡下的单目图像3D姿态估计 Monocular Image 3D Human Pose Estimation under Self-Occlusion (ICCV 13)
- wrong # args: should be "set varName ?newValue?"
- C判断点是否在多边形内部
- C#中XmlTextWriter读写xml文件详细介绍