链表(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) 循环顺序表实现队列 ...
随机推荐
- my.cnf配置样例
[mysql] no-auto-rehash port = socket = /data/mysql/data/mysqld.sock [mysqld] user = mysql port = bas ...
- 省选模板_STL
目录: 1. multiset 2. reverse 1.multiset namespace STL{ int main(){ multiset<int>::iterator s; mu ...
- 密信(MeSince),将取代传统电子邮件
电子邮件发展至今已经有几十年的历史,但仍然是最重要的现代互联网应用之一.在全球范围内,每小时发送的非垃圾邮件数量超过30亿封,从工作场景的使用到个人生活,电子邮件都扮演着不可或缺的角色.但是由于明文电 ...
- 普通页面使用vue.js心得
在写本文之前要问自己几个问题,来说明为什么要这么做: 为什么在html中使用vue.js? vue.js已经趋于成熟,个人感觉比jquery要好用的多,但是在node环境下使用vue.js不用使用SS ...
- MySQL的读写分离的几种选择
MySQL的读写分离的几种选择 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 原址如下: http://heylinux.com/archives/1004. ...
- NoReferencedTableError: Foreign key associated with column ** with which to generate a foreign key to target column 'id'
1.使用 python flask 框架做项目时,在实体类中配置了 映射关系, message: id = db.Column(db.Integer, primary_key=True)message ...
- angular-代码段
重复代码 <div ng-app="" ng-init="names=['Jani','Hege','Kai']"> <p>使用 ng- ...
- Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误
Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误 后来发现是POM文件写错了,修改为正确的pom,就可以了: POM ...
- CURL库的宏定义列表
列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...
- poj3061 Subsequence ,尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...