链表(list)--c实现
做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人。o(^▽^)o
to better myself.
#include <stdio.h>
#include <stdlib.h> typedef struct node
{
int data;
struct node* next;
}Node; typedef Node* List; void pInitList(List*);
List initList();
void deleteList(List);
int isNull(List);
void insertNode(Node*, int);
void deleteNode(List, Node*);
Node* findLast(List);
Node* findValue(List, int);
void printList(List); void printList(List list)
{
if(list == NULL)
{
printf("empty list. printf nothing.\n");
return;
}
Node* curNode = list->next; while(curNode != NULL )
{
if(curNode->next != NULL)
{
printf("%d ->", curNode->data);
}else{
printf("%d \n", curNode->data); }
curNode = curNode->next;
}
} Node* findValue(List list, int tarVal)
{
if(list == NULL)
{
printf("empty list.\n");
return NULL;
}
Node* curNode = list->next;
while((curNode->data != tarVal )
&&(curNode != NULL))
{
curNode = curNode->next;
}
if(curNode == NULL) {
printf("not find the value %d\n", tarVal);
free(curNode);
return NULL;
}
printf(" find the value %d\n", tarVal);
return curNode;
}
Node* findLast(List list)
{
if(list == NULL)
{
printf("empty list.\n");
return NULL;
}
Node* curNode = list->next;
while(curNode->next != NULL)
{
curNode = curNode->next;
}
printf("find the last node value is %d.\n", curNode->data);
return curNode; }
void deleteNode(List list, Node* delNode)
{
Node* curNode = (Node*)malloc(sizeof(Node));
Node* freeNode = NULL; if(curNode == NULL)
{
printf("malloc error at line %d.", __LINE__);
return;
}
curNode = list->next;
if(curNode->data == delNode->data)
{
list->next = NULL;
free(curNode);
return ;
}
while((curNode != NULL)
&& (curNode->next->data != delNode->data)
&& (curNode->next != NULL))
{
curNode=curNode->next;
} if(curNode->next == NULL || (curNode ==NULL))
{
printf("can not find the given node.\n");
return ;
}else{
freeNode = curNode->next;
curNode->next = freeNode->next;
free(freeNode);
} } void insertNode(Node* curNode, int newValue)
{
Node *newNode = (Node*) malloc(sizeof(Node));
if(newNode == NULL)
{
printf("malloc error at line %d.", __LINE__);
return;
}
newNode->data = newValue;
newNode->next = curNode->next;
curNode->next = newNode; } /*return 0 means is not NULL*/
int isNull(List list )
{
//printf("next value %d\n", list->next->data);
return (list->next == NULL);
}
#if 1
void pInitList(List* list)
{
//*list = (Node*) malloc(sizeof(Node));
(*list)->next = NULL;
return;
}
#endif
List initList()
{
List list;
list = (Node*) malloc(sizeof(Node));
list->next = NULL;
return list;
} void deleteList(List list)
{
Node* freeNode = NULL;
if(list == NULL) return;
while(list->next != NULL)
{
//get the free node
freeNode = list->next;
list->next = freeNode->next;
free(freeNode);
}
return ; } int main()
{
printf("hello.\n");
#if 0
List newList = NULL;
newList = initList();
#else
List *list = NULL;
//这里我第一次就没分配1级指针的内存,导致出错,感谢帮助我指出的人。@2016-07-26 00:54:22
list = (List*)malloc(sizeof(List));
*list = (Node*)malloc(sizeof(Node));
pInitList(list);
insertNode((*list), 1);
insertNode((*list)->next, 2);
insertNode((*list)->next->next, 3); findValue(*list, 2);
findLast(*list); printList(*list); Node *delNode = (Node*)malloc(sizeof(Node));
delNode->data = 2;
deleteNode(*list, delNode);
printList(*list); if(isNull(*list)){
printf("list is empty.\n");
}else{
printf("list is not empty.\n"); }
#endif return 0;
}
链表(list)--c实现的更多相关文章
- Redis链表实现
链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- 排序算法----基数排序(RadixSort(L))单链表智能版本
转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...
- 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结
防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- C语言之链表list
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- 用JavaScript来实现链表LinkedList
本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...
- 数据结构:队列 链表,顺序表和循环顺序表实现(python版)
链表实现队列: 尾部 添加数据,效率为0(1) 头部 元素的删除和查看,效率也为0(1) 顺序表实现队列: 头部 添加数据,效率为0(n) 尾部 元素的删除和查看,效率也为0(1) 循环顺序表实现队列 ...
随机推荐
- 使用C++部署Keras或TensorFlow模型
本文介绍如何在C++环境中部署Keras或TensorFlow模型. 一.对于Keras, 第一步,使用Keras搭建.训练.保存模型. model.save('./your_keras_model. ...
- IOS - No provisioning profiles with a valid signing identity 一种解决方法
1.删除原有“钥匙串访问”中疑是过期的的证书: 2.在Member Center中Certificate中删除疑是有问题的Certificate,重新添加新的Certificate: 3.在“钥匙串访 ...
- 使用命令:ssh-add 时,出现 “Could not open a connection to your authentication agent.”
为 GitHub 账号设置 SSH Key时, 使用命令:ssh-add,出现“Could not open a connection to your authentication agent”,解决 ...
- BZOJ 1878 HH的项链 (树状数组+离线)
题目大意:给你一个序列,求某区间出现不同的数的个数. 貌似离线树状数组是最好的解法 先把所有询问挂在它们询问的右端点上 然后从头到尾遍历这个序列,记录这个位置的值上一次出现的位置 那么,当遍历到第i位 ...
- 归档 SCP SFTP RSYNC(数据同步)
tar 选项 目标文件 源文件(1 2 3) tar cf **.tar file1 file2 file3 (默认情况下 cf选项只有归档没有压缩) tar xf 从归档中提取 创建tar的存档 ...
- CF735E Ostap and Tree
比较毒瘤的树形DP,子状态难想.这是主要是搬运一篇题解. 用\(f[i][j]\)表示\(i\)的子树中离\(i\)最近黑点的距离为\(j\),且距离超过\(j\)的点都被满足的方案数.转移时新建一个 ...
- poi操作excel2007(读取、生成、编辑)
因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直 ...
- BTrace介绍和生产环境样例
BTrace latest realese: release-1.2.5.1 BTrace guide(1.2-20101020): http://kenai.com/projects/btrace/ ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- TexturePacker 算法
代码 预览