链表(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) 循环顺序表实现队列 ...
随机推荐
- Pyhton学习——Day28
#上下文协议:文件操作时使用with执行# with open('a.txt','w',encoding='utf-8') as f1:# with语句,为了让一个对象兼容with语句,必须在这个对象 ...
- centos7下安装pyspark
1.安装python 2.安装jdk 3.下载spark:http://spark.apache.org/downloads.html, 下载新版(spark-2.3.1-bin-hadoop2.7. ...
- php字符处理
1.strstr 截取某个字符后的字符: echo strstr("123456789","5");//输出:6789
- ios兼容 input输入时弹出键盘框 页面整体上移键盘框消失后在ios上页面不能回弹的问题
前端h5混合开发手机端ios 当有input输入时,手机下方弹出键盘使页面上移,当输入完成,键盘消失后页面显示回到原位,但实际不能点击(可点击上方区域,有反应),也就是说实际是没有回弹. 解决办法: ...
- 博客模板更新CSS
采用了作者#a的模板BlueSky进行了些许修改 在原有基础上加了三个样式,使页面显示风格统一些 #home{ background-color:#fff; } #main{ background-c ...
- JS 将有父子关系的数组转换成树形结构数据
将类似如下数据转换成树形的数据 [{ id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1', ...
- UVALive-8077 Brick Walls 找规律
题目链接:https://cn.vjudge.net/problem/UVALive-8077 题意 有一个用砖头磊起来的墙,现在又有一只蚂蚁,想沿着砖缝从起点跑到终点. 问最短路长度. 思路 找规律 ...
- Python发行版本Anaconda的安装说明:基于Anaconda2-4.3.1-Windows-x86_64
Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.因为包含了大量的科学包,Anaconda 的下载文件比较大(约 531 MB),如果 ...
- 如何在 Linux 上安装 Nginx (源码安装)
如何在 Linux( CentOS ) 上安装 Nginx 1.下载 nginx 链接 : https://pan.baidu.com/s/1sll0Hrf 密码 : xnem 2.安装 gcc ( ...
- 练练脑,继续过Hard题目
http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧. # Title Editorial Acceptance Diffi ...