简单数据结构——单向链表
![](https://s1.51cto.com/attachment/201008/213308414.jpg)
- /*FILE***********************************************************************
- 文件名:onewaylist.h
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 定义单向链表的头文件
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #ifndef ONEWAYLIST_H
- #define ONEWAYLIST_H
- #include <stdio.h>
- #include <stdlib.h>
- /*STR************************************************************************
- 结构名称:oneWayList
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:定义单向链表结构
- 属性:大小size,指向头节点指针,指向尾节点指针。
- 修改日志:
- ************************************************************************STR*/
- typedef struct oneWayList{
- int size;
- struct Node *head;
- struct Node *tail;
- }oneWayList;
- /*STR************************************************************************
- 结构名称:Node
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:定义单向链表结构
- 属性:数据element,指向下一个节点的指针。
- 修改日志:
- ************************************************************************STR*/
- typedef struct Node{
- int element;
- struct Node *next;
- }Node;
- void initList(struct oneWayList *);
- int getSize(struct oneWayList *);
- int empty(struct oneWayList *);
- void push_back(struct oneWayList *, struct Node *);
- void pop_back(struct oneWayList *, struct Node *);
- void print_List(struct oneWayList *);
- void list_remove(struct oneWayList *, int);
- #endif
- /*FILE***********************************************************************
- 文件名:onewaylist.h
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 实现单向链表的c文件
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #include "onewaylist.h"
- /*FUN************************************************************************
- 函数名称:void initList(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:初始化单向链表
- 输入:指向单向链表结构体的指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void initList(struct oneWayList *list)
- {
- list->size = 0;
- list->head = NULL;
- list->tail = NULL;
- }
- /*FUN************************************************************************
- 函数名称:int getSize(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:获得链表的大小
- 输入:指向单向链表结构体的指针
- 输出:链表大小
- 修改日志:
- ************************************************************************FUN*/
- int getSize(struct oneWayList *list)
- {
- return list->size;
- }
- /*FUN************************************************************************
- 函数名称:int empty(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:判断链表是否为空
- 输入:指向单向链表结构体的指针
- 输出:链表为空为返回1,链表不空返回0
- 修改日志:
- ************************************************************************FUN*/
- int empty(struct oneWayList *list)
- {
- if( 0 == list->size)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- /*FUN************************************************************************
- 函数名称:void push_back(struct oneWayList *list, struct Node *node)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:向链表表尾加入一个节点
- 输入:指向单向链表结构体的指针,需要加入的节点指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void push_back(struct oneWayList *list, struct Node *node)
- {
- list->size++;
- node->next = NULL;
- if( NULL == list->head)
- {
- list->head = node;
- list->tail = node;
- }
- else
- {
- list->tail->next = node;
- list->tail = node;
- }
- }
- /*FUN************************************************************************
- 函数名称:void pop_back(struct oneWayList *list,struct Node *node)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:向链表表尾弹出一个节点
- 输入:指向单向链表结构体的指针,需要弹出的节点指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void pop_back(struct oneWayList *list,struct Node *node)
- {
- struct Node *tempNode = (struct Node *)malloc(sizeof(struct Node));
- if ( empty(list) )
- {
- printf("List is empty!!\n");
- }
- else
- {
- list->size--;
- tempNode = list->head;
- node = list->tail;
- while(tempNode->next->next != NULL )
- {
- tempNode = tempNode->next;
- }
- tempNode->next = NULL;
- list->tail = tempNode;
- }
- }
- /*FUN************************************************************************
- 函数名称:void print_List(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:打印链表
- 输入:指向单向链表结构体的指针
- 输出:屏幕输出链表
- 修改日志:
- ************************************************************************FUN*/
- void print_List(struct oneWayList *list)
- {
- struct Node *node;
- node = list->head;
- while(node->next)
- {
- printf("%d ", node->element);
- node = node->next;
- }
- printf("%d\n",node->element);
- }
- /*FUN************************************************************************
- 函数名称:void list_remove(struct oneWayList *list, int element)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:删除指定元素
- 输入:指向单向链表结构体的指针,指定元素
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void list_remove(struct oneWayList *list, int element)
- {
- struct Node *tempNode;
- tempNode = list->head;
- while( tempNode->next )
- {
- if( element == tempNode->next->element )
- {
- list->size--;
- tempNode->next = tempNode->next->next;
- break;
- }
- tempNode = tempNode->next;
- }
- printf("No element of the list\n");
- }
- /*FILE***********************************************************************
- 文件名:onelistmain.c
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 测试单向链表
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #include "onewaylist.h"
- int main(int argv, char argc[])
- {
- int temp;
- struct oneWayList list;
- struct Node *tempNode;
- initList(&list);
- /*测试空链表是弹出是否会错误*/
- pop_back(&list, tempNode);
- /*向链表加入10个节点*/
- for(temp = 0; temp < 10; temp++)
- {
- tempNode = (struct Node*) malloc(sizeof(struct Node));
- tempNode->element = temp;
- push_back(&list, tempNode);
- }
- /*打印链表*/
- print_List(&list);
- /*看一看链表长度*/
- printf("%d\n",getSize(&list));
- /*测试一下弹出函数*/
- pop_back(&list, tempNode);
- printf("%d\n", tempNode->element);
- /*打印是否链表正确*/
- print_List(&list);
- /*测试remove*/
- list_remove(&list,4);
- print_List(&list);
- return 0;
- }
转载于:https://blog.51cto.com/2274594/472342
简单数据结构——单向链表相关推荐
- 【单向链表】数据结构——单向链表的介绍与代码实现笔记
从今天开始将修炼数据结构专栏,将持续更新,分模块学习. 数据结构--单向链表 一.数据结构 1.什么是数据结构? 2.逻辑结构和物理结构 二.链表--线性结构 1.首先介绍下链表和数组的区别 2.链表 ...
- 数据结构-单向链表解决学生录入问题
数据结构-单向链表 单向链表实现学生录入程序 通过main函数实现一切功能 通过调用函数实现 传送门结束 单向链表实现学生录入程序 将用户输入的不定个数的学生成绩按顺序编号并保存,以用户输入0作为录入 ...
- 数据结构单向链表线性结构_线性数据结构链表为何以及如何解释
数据结构单向链表线性结构 Imagine you have gone to a crowded place, say to a k-pop concert with your friends and ...
- c语言编程切片stl1005无标题,C语言实现简单的单向链表(创建、插入、删除)及等效STL实现代码...
实现个算法,懒得手写链表,于是用C++的forward_list,没有next()方法感觉很不好使,比如一个对单向链表的最简单功能要求: input: 1 2 5 3 4 output: 1-> ...
- 数据结构—单向链表(详解)
一.链表基础 链表是一种常见的数据结构,其中运用到了结构体指针,链表可以实现动态存储分配,换而言之,链表是一个功能强大的数组,可以在某个节点定义多种数据类型,可以实现任意的添加,删除,插入节点等.链表 ...
- 数据结构 | 单向链表学习总结
单向链表学习总结 简介 总结 链表介绍 链表定义 链表函数 计算链表的长度 将列表转换为链表 LeetCode:剑指 Offer 22. 链表中倒数第k个节点 双向链表学习总结:python | 双向 ...
- 数据结构--单向链表
单向链表的一种Go语言实现 package mainimport "fmt"type Node struct {no intname stringnext *Node }//实现尾 ...
- 数据结构——单向链表
链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成. 结点API设计 类名 N ...
- 数据结构——单向链表-双向链表
1.单向链表按位置修改 //按位置修改 int updata_pos(linklist *L,int pos,datatype new_e) {if(NULL==L||empty(L)||pos< ...
最新文章
- centos yum mysql-devel 5.5_CentOS 6.5下yum安装 MySQL-5.5全过程图文教程
- 二值网络--Structured Binary Neural Networks for Accurate Image Classification and Semantic Segmentation
- 关于isset的一点说明
- python爬虫scrapy步骤mac系统_Mac中Python 3环境下安装scrapy的方法教程
- HDU4825 Xor Sum —— Trie树
- XXX管理平台系统——概要
- Mysql分析排序和锁阅读总结
- 使用Servlet上传多张图片——Dao层(BaseDao.java)
- java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置
- 电脑主页面上的计算机没了,电脑界面上的internet explorer 没有了
- 【读书笔记《Android游戏编程之从零开始》】14.游戏开发基础(Bitmap 位图的渲染与操作)
- 用xLang写Timer事件
- 批量文件转码工具(支持GBK,UTF-8转换)
- 监控视频中的主码流和子码流是什么意思?
- 完全用Linux:半年来,Linux高效完成我所有的工作!(转贴)
- [java]深入剖析Java性能监控调优视频教程
- 喷泉设备中的水幕效果
- 操作系统文件管理之FCB
- NLP顶会近三年小众研究领域
- grep -v grep的作用
热门文章
- centos php日志分析,Centos日志查看分析
- Posted content length of 26789546 exceeds limit of 10485760
- MYSQL中时间毫秒数转换及日期格式化
- WINDOWS操作系统32位(x86)和64位(x64)的区别
- 企业基础管理薄弱,激励机制不健全怎么办?
- 计算机中那些事儿(四):我眼中的虚拟技术
- 人人能上手:OpenAI发射初学者友好的强化学习教程 | 代码简约易懂
- 亚马逊AI惹众怒:一个没有意识的程序,竟然自己学会了“重男轻女”
- 提到强化学习只知道AlphaGo?其实,RL在工业界还有这些应用
- 孩子春节猛吃零食怎么办?专家支招:先用蔬菜填饱肚子