链表的创建、删除、反向(C语言)
2014-08-30 16:39
447 查看
好久没接触C了,今天温习一下链表操作。
链表反向用的方式是新建一个链表,将原链表元素依次插入至新链表头部,实现反向。
链表删除是参考了linus的方法,用一个二级指针,省去了教科书中方式关于链表头部的判断。
链表反向用的方式是新建一个链表,将原链表元素依次插入至新链表头部,实现反向。
链表删除是参考了linus的方法,用一个二级指针,省去了教科书中方式关于链表头部的判断。
#include <stdio.h> #include <malloc.h> typedef struct Node_s{ int value; struct Node_s *next; }Node_t; void InsertNode(Node_t **List,int value) { Node_t *node = (Node_t *)malloc(sizeof(Node_t)); node->value = value; if(*List != NULL){ Node_t *current = *List; Node_t *pre = *List; while(current != NULL && value > current->value){ pre = current; current = current->next; } node->next = current; pre->next = node; }else{ node->next = NULL; *List = node; } } void RemoveNode(Node_t **List,int value) { Node_t **current = List; while(*current != NULL){ if(IsNode(*current,value)){ current = &((*current)->next); }else{ Node_t *node = *current; *current = (*current)->next; free(node); break; } if((*current)->value >value){ //因为是有序链表,所以超过范围可以判断此链表中无删除元素 break; } } } void InsertHead(Node_t **List,int value) { Node_t *node = (Node_t *)malloc(sizeof(Node_t)); node->value = value; node->next = *List; *List = node; } void ReverseList(Node_t **List) { Node_t *RList = NULL; //新链表 Node_t *current = *List; while(current){ InsertHead(&RList,current->value); //将元素依次插入至新链表 Node_t *node = current; current = current->next; free(node); } *List = RList; } int IsNode(Node_t *Node,int value) { return ((Node->value != value)); //是否符合条件 } int main(int argc,char **argv) { Node_t *List = NULL; InsertNode(&List,2); InsertNode(&List,5); InsertNode(&List,3); ReverseList(&List); if(List != NULL){ printf("%d\n",List->value); //这里可以加断点进行调试信息查看 } return 0; }
相关文章推荐
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 用c语言完成一个双向链表的创建,插入,删除
- 单链表的基本操作(创建,删除,增加,反向)
- 最详细的线性和表单链表删除创建新建的C语言实现
- C语言--单链表创建、求长度、查找前驱结点、删除结点
- C语言 链表的创建--打印--逆置--新增--删除--排序--释放
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- C语言 链表的创建,以及节点的增加和删除
- c语言链表基本操作(带有创建链表 删除 打印 插入)
- C语言各种链表操作(创建、打印、删除、插入、反转)
- c语言:单链表的实现(一) 创建,插入,删除,查找
- C语言中,链表的创建,插入,删除,遍历,求链表长度,排序等
- 双向链表 C语言 (创建,插入,删除,判空,返回链表长度)
- C语言单链表创建,插入,删除
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- [转]C语言之单向链表的创建插入删除等功能
- c语言链表的创建、插入、删除、排序
- 链表基本操作(创建,插入,查找,删除)-C语言
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- (C语言)链表的创建、遍历、插入和删除