做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实现的更多相关文章

  1. Redis链表实现

    链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...

  2. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  3. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  4. 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结

    防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...

  5. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  6. C语言之链表list

    #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...

  7. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  8. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  9. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  10. 数据结构:队列 链表,顺序表和循环顺序表实现(python版)

    链表实现队列: 尾部 添加数据,效率为0(1) 头部 元素的删除和查看,效率也为0(1) 顺序表实现队列: 头部 添加数据,效率为0(n) 尾部 元素的删除和查看,效率也为0(1) 循环顺序表实现队列 ...

随机推荐

  1. luoguP4921 情侣?给我烧了! 组合数_容斥原理_计数问题

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  2. mmap详解

    共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式, 因为进程可以直接读写内存,而不需要任何 数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存 ...

  3. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport

    Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...

  4. How Google Backs Up The Internet Along With Exabytes Of Other Data

    出处:http://highscalability.com/blog/2014/2/3/how-google-backs-up-the-internet-along-with-exabytes-of- ...

  5. 判断浏览器是PC设备还是移动设备

    var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ...

  6. 小程序(Wepy)--生成海报图片

    对于小程序的分享, 除了分享给朋友, 好友群,是可以直接做到的, 但是要想扩大推广范围, 通过生成海报图片, 将自己小程序码带进去,应该是目前我所知的好办法了. 但是海报也不是那么好搞.之前自己手写出 ...

  7. Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架

    Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...

  8. Android-Universal-Image-Loader学习笔记(3)--内存缓存

    前面的两篇博客写了文件缓存.如今说说Android-Universal-Image-Loader的内存缓存.该内存缓存涉及到的类如图所看到的 这些类的继承关系例如以下图所看到的: 如同文件缓存一样,内 ...

  9. 多本Web前端深度修炼书籍(提供网盘下载链接)

    书籍介绍:这本书涵盖了html5新增标签和功能,而且提供了jquerymobile,Phonegap,Sencha Touch框架的介绍和应用,最后还带了一个移动web应用的样例,绝对是移动web开发 ...

  10. 【BZOJ3270】博物馆 概率DP 高斯消元

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...