数据结构之单链表(C实现)
list.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <stdio.h>
#include <malloc.h>
//定义单链表结点
typedef struct _node
{
int data; //数据
struct _node *next; //指向下一结点
}node,*pNode;
//创建链表
node *create_list();
//从链表头后插入数据
int insert_listHead(pNode head,int data);
//从链表尾后插入数据
int insert_listTail(pNode head, int data);
//遍历链表,并显示每个结点的数据
int show_listData(pNode head);
//求链表中的结点个数 (不包括链表头,指真正存放数据的结点数目)
int getNodeNum(pNode head);
//根据索引查找结点,找到返回所找结点 index=0,对应头结点(不存放数据) index=1,对应第一个数据结点
pNode find_nodeByIndex(pNode head,int index);
//根据数据查找第一个符合结点(后面也可能有结点的数据为所找数据),找到返回所找结点
pNode find_nodeByData(pNode head,int data);
//指定位置插入结点数据
int insert_dataInPos(pNode head,int data,int index);
//从链表中删除第index个结点 index范围为1-nodeNum
int delete_listIndex(pNode head,int index);
//删除整个链表
int delete_listAll(node **head);
//根据索引查找结点,找到修改其值
int modify_nodeByIndex(pNode head,int index,int data);
//将链表排序 ==0,升序 ==1,降序
int sort_list(pNode head,int sortType);
#endif // LIST_H
list.c
#include "list.h"
/*************单链表测试程序************/
//创建链表
node *create_list()
{
pNode head = (pNode)malloc(sizeof(node));
if(!head)
{
printf("malloc error!\n");
// exit(-1); //在iostream中定义
return NULL;
}
head->data = ;
head->next = NULL; //不用指向head,由于不是循环链表且是单链表的尾结点的next腰指向NULL
return head;
}
//从链表头后插入结点数据
int insert_listHead(pNode head,int data)
{
//检测链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
pNode tmpNode = (pNode)malloc(sizeof(node)); //存放插入数据的结点
if(!tmpNode)
{
printf("malloc error!\n");
;
}
//将插入数据存入要插入链表的结点
tmpNode->data = data;
//将tmpNode插入链表头后
tmpNode->next = head->next;
head->next = tmpNode;
;
}
//从链表尾后插入结点数据
int insert_listTail(pNode head, int data)
{
//检测链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
pNode tmpNode = (pNode)malloc(sizeof(node)); //存放插入数据的结点
if(!tmpNode)
{
printf("malloc error!\n");
;
}
//将插入数据存入要插入链表的结点
tmpNode->data = data;
//将tmpNode插入链表尾后
tmpNode->next = NULL; //插入链表插入链表尾后,成为新的尾结点,next要指向NULL
pNode cur = head;
//找到链表尾结点
while(cur->next) //尾结点的next指向NULL,跳出时的cur即为尾结点
{
cur = cur->next;
}
cur->next = tmpNode; //原先尾结点的next指向插入结点
;
}
//遍历链表,并显示每个结点的数据
int show_listData(pNode head)
{
//检测链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
//检测链表是否为空 (“为空”与“不存在”不同,为空说明只有一个结点,刚创建还未插入任何结点)
if(!head->next)
{
printf("list isnot exist!\n");
;
}
pNode cur = head->next; //头结点数据域不保存插入数据,第二个结点才是第一个插入结点
while(cur)
{
printf("%d ",cur->data);
cur = cur->next;
}
printf("\n");
;
}
//求链表中的结点个数 (不包括链表头,指真正存放数据的结点数目)
int getNodeNum(pNode head)
{
; //结点数目
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
;
}
pNode cur = head->next;
while(cur)
{
nodeNum++;
cur = cur->next;
}
return nodeNum;
}
//根据索引查找结点,找到返回所找结点 index=0,对应头结点(不存放数据) index=1,对应第一个数据结点
pNode find_nodeByIndex(pNode head,int index)
{
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
return NULL;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
return NULL;
}
//------
int nodeNum = getNodeNum(head);
|| index > nodeNum) //索引必须在允许范围 (1-nodeNum)
{
printf("pos out of range!\n");
return NULL;
}
;
pNode cur = head;
while(cur)
{
i++;
cur = cur->next;
if(i==index)
return cur;
}
return NULL; //不返回,有警告
}
//根据数据查找第一个符合结点(后面也可能有结点的数据为所找数据),找到返回所找结点
pNode find_nodeByData(pNode head,int data)
{
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
return NULL;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
return NULL;
}
pNode cur = head->next;
while(cur)
{
if(cur->data == data)
return cur;
cur = cur->next;
}
return NULL;
}
//指定位置插入结点数据
int insert_dataInPos(pNode head,int data,int index)
{
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
;
}
//------
int nodeNum = getNodeNum(head);
|| index > nodeNum) //指定插入位置必须在允许范围
{
printf("pos out of range!\n");
;
}
//由于find_nodeByIndex的参数2范围限制为1-nodeNum,所以index在那里不能为1,要>=2
) //插入到位置1,相当于插入头结点后面
{
insert_listHead(head,data);
}
else //index范围为2-nodeNum
{
pNode tmpNode = (pNode)malloc(sizeof(node));
if(!tmpNode)
{
printf("malloc error!\n");
;
}
tmpNode->data = data;
//获得插入位置的前一个结点-insertFrontNode(新数据结点插入到该结点后面)
pNode insertFrontNode = find_nodeByIndex(head,index-);
if(!insertFrontNode) //未找到要被插入的结点
{
printf("no find insertFrontNode!\n");
;
}
//若index=2,相当于新数据结点插入到原数据结点1后面,所以需要找到原数据结点1
tmpNode->next = insertFrontNode->next;
insertFrontNode->next = tmpNode;
}
;
}
//从链表中删除第index个结点 index范围为2-nodeNum
int delete_listIndex(pNode head,int index)
{
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
;
}
//------
int nodeNum = getNodeNum(head);
|| index > nodeNum) //index必须在允许范围
{
printf("pos out of range!\n");
;
}
pNode front = find_nodeByIndex(head,index-); //找到要删除结点的前一个结点
if(!front)
{
printf("no find!\n");
;
}
//---------
pNode cur = find_nodeByIndex(head,index); //找到要删除结点
if(!cur)
{
printf("no find!\n");
;
}
//删除该结点
front->next = cur->next;
free(cur);
cur = NULL;
;
}
//删除整个链表
int delete_listAll(node **head)
{
if(*head == NULL)
{
printf("list isnot exist!\n");
;
}
node *cur = (*head)->next;
while(cur)
{
node *tmp = cur;
//在free(tmp);后面,那时cur==tmp已经被释放,cur = cur->next不能正常执行了
cur = cur->next;
free(tmp);
tmp = NULL;
}
free(*head);
(*head) = NULL;
;
}
//根据索引查找结点,找到修改其值
int modify_nodeByIndex(pNode head,int index,int data)
{
pNode modifyNode = find_nodeByIndex(head,index); //找到要修改的结点
modifyNode->data = data;
; //不返回,有警告
}
//将链表排序 ==0,升序 ==1,降序
int sort_list(pNode head,int sortType)
{
//判断链表是否存在
if(!head)
{
printf("list isnot exist!\n");
;
}
//------
//判断链表是否为空
if(!head->next)
{
printf("list is NULL!\n");
;
}
int nodeNum = getNodeNum(head);
int i,j;
; i < nodeNum - ; i++) //冒泡排序法
{
; j < nodeNum - - i; j++)
{
//第一次获取0,1结点 第二次获取1,0结点
node *t_first = find_nodeByIndex(head,j+);
node *t_second = find_nodeByIndex(head,j+);
) //升序
{
if(t_first->data > t_second->data)
{
//交换2个结点的值
int tmp = t_first->data;
t_first->data = t_second->data;
t_second->data = tmp;
}
}
) //降序
{
if(t_first->data < t_second->data)
{
int tmp = t_first->data;
t_first->data = t_second->data;
t_second->data = tmp;
}
}
else
{
printf("parame error!\n");
;
}
}
}
;
}
main.c
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include "list.h"
using namespace std;
pNode tmpNode;
int main(void)
{
//创建链表
tmpNode = create_list();
if(!tmpNode)
{
printf("malloc error!\n");
;
}
//插入1,3,5,7,9到链表中
;i<;i++)
{
insert_listHead(tmpNode,++i);
}
//插入结点数据
insert_dataInPos(tmpNode,,);
//获取结点数目
printf("nodeNum=%d\n",getNodeNum(tmpNode));
//遍历显示结点数据
printf("showList: ");
show_listData(tmpNode);
//根据索引,返回第3个数据结点 index范围为1-nodeNum
pNode t_node = find_nodeByIndex(tmpNode,);
if(!t_node) //有可能未找到,则返回NULL,那执行t_node->data会造成程序崩溃
printf("no find!\n");
else
printf("t_data:data=%d\n",t_node->data);
//返回数据为1的结点
pNode t_node1 = find_nodeByData(tmpNode,);
if(!t_node1)
printf("no find!\n");
else
printf("t_data:data=%d\n",t_node1->data);
//从链表中删除第1个结点 index范围为2-nodeNum
delete_listIndex(tmpNode,);
printf("showList: ");
show_listData(tmpNode);
//升序后显示结点数据
sort_list(tmpNode,);
printf("sortUp showList: ");
show_listData(tmpNode);
//降序后显示结点数据
sort_list(tmpNode,);
printf("sortUp showList: ");
show_listData(tmpNode);
//删除所有结点
delete_listAll(&tmpNode);
printf("showList: ");
show_listData(tmpNode);
}
结果运行图:

数据结构之单链表(C实现)的更多相关文章
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- javascript数据结构之单链表
下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...
- 数据结构之单链表的实现-java
一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...
- python 数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- 数据结构(一) 单链表的实现-JAVA
数据结构还是很重要的,就算不是那种很牛逼的,但起码得知道基础的东西,这一系列就算是复习一下以前学过的数据结构和填补自己在这一块的知识的空缺.加油.珍惜校园中自由学习的时光.按照链表.栈.队列.排序.数 ...
- 数据结构 - 静态单链表的实行(C语言)
静态单链表的实现 1 静态链表定义 静态链表存储结构的定义如下: /* 线性表的静态链表存储结构 */ #define MAXSIZE 1000 /* 假设链表的最大长度是1000 */ typede ...
- 数据结构 - 动态单链表的实行(C语言)
动态单链表的实现 1 单链表存储结构代码描述 若链表没有头结点,则头指针是指向第一个结点的指针. 若链表有头结点,则头指针是指向头结点的指针. 空链表的示意图: 带有头结点的单链表: 不带头结点的单链 ...
- 【数据结构】单链表介绍及leetcode206题反转单链表python实现
题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...
- 数据结构(2):单链表学习使用java实现
单链表是单向链表,它指向一个位置: 单链表常用使用场景:根据序号排序,然后存储起来. 代码Demo: package com.Exercise.DataStructure_Algorithm.Sing ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
随机推荐
- 可并堆试水--BZOJ1367: [Baltic2004]sequence
n<=1e6个数,把他们修改成递增序列需把每个数增加或减少的总量最小是多少? 方法一:可以证明最后修改的每个数一定是原序列中的数!于是$n^2$DP(逃) 方法二:把$A_i$改成$A_i-i$ ...
- codevs1128 导弹拦截
题目描述 Description 经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0 时,则能够拦截与它位置恰好相同的导弹 ...
- 网易2018校招 合唱 DP
时间限制:2秒 空间限制:131072K 小Q和牛博士合唱一首歌曲,这首歌曲由n个音调组成,每个音调由一个正整数表示.对于每个音调要么由小Q演唱要么由牛博士演唱,对于一系列音调演唱的难度等于所有相 ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- list循环删除单个元素
摘自https://www.cnblogs.com/pcheng/p/5336903.html JAVA中循环删除list中元素的方法总结 JAVA中循环遍历list有三种方式for循环.增强for循 ...
- poj3511--A Simple Problem with Integers(线段树求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60441 ...
- td里面嵌套img标签后如何消除图片间隔
td里面嵌套image标签后如何消除图片间隔 CreateTime--2018年3月7日16:18:12 Author:Marydon 情景还原: <body> <div sty ...
- Python开发【1.4数据类型】
1.数字 数字数据类型用于存储数值. 他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象. # 创建对象 var1 = 1 var2 = 2 # 删除对象 del var1 del ...
- HTTP要点概述:七,编码,压缩传输,分块传输
一,编码: HTTP 在传输数据时可以按照数据原貌直接传输,但也可以在传输过程中通过编码提升传输速率.通过在传输时编码,能有效地处理大量的访问请求.但是,编码的操作需要计算机来完成,因此会消耗更多的 ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...