做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. 记录——本地minikube安装ubuntu镜像总是报 Back-off restarting failed container问题 -已解决(更新)

    1.环境介绍 使用本机系统:macX minikube镜像:安装的阿里云提供的镜像(否则总是提示访问google的api,不FQ无法成功) 虚拟机情况:使用Virtual box 的虚拟机环境 min ...

  2. CF993C Careful Maneuvering bitset_枚举

    Code: #include<cstdio> #include<map> #include<iostream> #include<cmath> #inc ...

  3. CF1041F Ray in the tube构造_思维

    不难发现起点必定是一个点. 每次间隔的距离一定是 2k2^k2k,关键就是要判断两点是否在同一跳跃距离上可被同时覆盖. 我们可以对上边进行 x1≡x_{1}\equivx1​≡ x2mod(2∗dx) ...

  4. ansible 定义主机用户和密码

    定义主机组用户和密码 [webservers] ansible[01:04] ansible_ssh_user='root' ansible_ssh_pass='AAbb0101' [root@ftp ...

  5. js实现点击复制网页内容(基于execCommand)

    通过execCommand方法来实现,当一个HTML文档切换到设计模式 designMode时,文档对象暴露 execCommand 方法,该方法允许运行命令来操纵可编辑区域的内容.大多数命令影响文档 ...

  6. 监控Apache计数器

  7. 洛谷 P2243 电路维修

    P2243 电路维修 题目背景 Elf 是来自Gliese 星球的少女,由于偶然的原因漂流到了地球上.在她无依无靠的时候,善良的运输队员Mark 和James 收留了她.Elf 很感谢Mark和Jam ...

  8. iOS UI16_数据持久化

    // // Student.h // UI16_数据持久化 // // Created by dllo on 15/8/19. // Copyright (c) 2015年 zhozhicheng. ...

  9. HDU 3240

    求卡特兰数前N项的和模M. 直接求必定是不可能的,卡特兰数太大了.想了好久,本打算把位数拆成素数相乘,然后记录下各素数的个数计算.可惜,TLE....因为N太大了. 除法必定是要用到逆元的,但分母与M ...

  10. 安卓WebView的使用,在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页

    WebView 在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页. 1.定义一个WebView位置 <?xml version="1.0" encoding=" ...