基本Link List 用C語言實現

先附上標頭檔

 /**
* @author Chen-Hao Lin
* @email westgate.skater@gmail.com
* @website   https://www.cnblogs.com/ollie-lin
* @link     https://www.cnblogs.com/ollie-lin/p/9927405.html
* @version v1.0
* @ide CodeBlocks 17.12
* @license GUN GCC
* @brief link list template
* @file Linklist.h
*/ #include <stdlib.h>
#include <stdio.h> /**
* @defgroup Link list node
* @brief
* @{
*/ typedef struct node
{
int data;
struct node * next;
}Node; /**
* @brief Create link list.
* @param arr: pointer to integer data array. link list data array to assign link list data.
* @param size: describe data array size
* @retval first link list node.
*/
Node *CreateList(int *arr, int size); /**
* @brief Show all of link list.
* @param *node: print link list form this node.
* @retval None
*/
void PrintList(Node *node); /**
* @brief Release link list space.
* @param *node: Release link list form this node.
* @retval None
*/
void FreeList(Node *node); /**
* @brief Search the specific node.
* @param *node: Search the specific node form this pointer.
* @param data: Search the specific node information.
* @retval find the specific node
*/
Node *SearchNode(Node *node, int data); /**
* @brief Insert node
* @param *node: Insert node after the this param.
* @param item: Search data of specific node to insert node.
* @param data: The data of Insert node.
* @retval None
*/
void InsertNode(Node *node, int item, int data); /**
* @brief Before insert node at first node.
* @param *node: first node
* @param data: The data of Insert node.
* @retval first node
*/
Node *Push_front(Node *node, int data); /**
* @brief Insert node at last
* @param *node: form last node
* @param data: The data of last node.
* @retval None
*/
void Push_back(Node *node, int data);

各項功能實做 Create List

 Node * CreateList(int *arr, int size)
{
if(arr == || size == )
return NULL; Node *previous, *first;
for(int i = ; i < size; i++)
{
Node *current = (Node*)malloc(sizeof(Node));
current->data = arr[i]; if(i == )
first = current;
else{
previous->next = current;
}
current->next = NULL;
previous = current;
}
return first;
}

PrintList

 void PrintList(Node *node)
{
if(node == ){
printf("List is empty.\n");
return;
} Node *current = node;
while(current)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

Release List

 void FreeList(Node *node)
{
Node *current, *temp;
current = node;
while(current)
{
temp = current;
current = current->next;
free(temp);
}
}

Search node

 Node *SearchNode(Node *node, int data)
{
Node *temp = node;
while(temp)
{
if(temp->data == data)
return temp;
else
temp = temp->next;
}
return NULL;
}

Insert node

 void InsertNode(Node *node, int item, int data)
{
Node *current = node;
Node *previous = (Node*)malloc(sizeof(Node));
Node *newNode = (Node*)malloc(sizeof(Node));
while(current)
{
if(current->data == item)
{
newNode->data = data;
previous->next = newNode;
newNode->next = current;
break;
}
else
{
previous = current;
current = current->next;
}
}
}

push front/back node

 Node* Push_front(Node *node, int data)
{
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = node;
node->next = node->next;
return temp;
} void Push_back(Node *node, int data)
{
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
while(node->next)
{
node = node->next;
}
node->next = newNode;
newNode->next = NULL;
}

(C/C++) Link List - C 語言版本的更多相关文章

  1. GO語言基礎教程:序章

    首先自我介紹一下我自己,我是一個coder,目前主要從事B/S程序開發工作,懂點PHP;ASP;JSP;JS;VB;C;DELPHI;JAVA,另外知道幾個數據庫,除此之外別無所長,那麼我為何會選擇學 ...

  2. 幾個步驟輕鬆在windows操作系統上搭建GO語言開發環境

    1. 首先下载官方GO語言安装包: https://code.google.com/p/go/wiki/Downloads?tm=2 2. 设置 GOPATH 在任意磁盘根目录新建一个文件夹,名字随意 ...

  3. GO語言基礎教程:數組,切片,map

    這節課我們來講解數組,切片和map,或許您是從其他語言轉到GO語言這邊的,那麼在其他語言的影響下您可能會不太適應GO語言的數組,因為GO語言把數組給拆分成了array,slice和map,接下來的時間 ...

  4. GO語言基礎教程:流程控制

    在開始一個新的章節之前先來回顧上一篇文章的部份,首先我們來看這段代碼: package main import ( "fmt" ) func main(){ var x,y int ...

  5. GO語言基礎教程:數據類型,變量,常量

    GO類似PHP,每行的結尾要加分號來結束,不同點在於GO對此並不強制,這一點又像javascript,另外GO的語句塊是用一對大括號來包裹的,但是go要求左大括號必須要在語句的結尾處,不能在行首出現左 ...

  6. GO語言基礎教程:Hello world!

    首先簡單地說一下GO語言的環境安裝,從 http://golang.org/dl/ 針對自己的操作系統選擇合適的安裝包,然後下載安裝即可,下載的時候注意別選錯了的操作系統,例如go1.3.1.darw ...

  7. Ubuntu語言支持爲灰色修復方法

    Ubuntu語言支持爲灰色修復方法 在Ubuntu12.04中,在下不知爲何將 語言支持 中 應用到整個系統 和 添加語言 這2個按弄成了灰色,導致ibus不能輸入中文,現在唔將修復方法公告天下: 1 ...

  8. Python 面向導向語言 Object Oriented Programming Language

    Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...

  9. C/C++語言 - 日常算法 - 蛇形填數

    C/C++語言 - 日常算法 - 蛇形填數 日期 : 2019-06-11 問題描述: 在n×n方阵里填入1,2,…,n×n,要求填成蛇形. 例如,n=4时方阵为: 10   11  12  1 9 ...

随机推荐

  1. <转>Linux环境进程间通信--信号灯(四)

    http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/ 一.信号灯概述 信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机 ...

  2. mysql日期获取

    获取当前日期在本周的周一:select subdate(curdate(),date_format(curdate(),'%w')-1) 获取当前日期在本周的周日:select subdate(cur ...

  3. python闭包和装饰器的理解

    闭包: 两个函数的嵌套,外部函数返回内部函数的引⽤,外部函数⼀定有参数 def 外部函数(参数): def 内部函数(): pass return 内部函数 他跟函数之间的区别: 1.格式两个函数嵌套 ...

  4. SkyBox

    [SkyBox] Skyboxes 本质是一个Material,这个Meterial的shader必须设置为ShaderFX/Skybox. SkyBox可以被绑定到摄像机或设置一个全局的SkyBox ...

  5. java基础之JDBC六:DBCP 数据库连接池简介

    我们之前写的代码中的数据库连接每次都是自己创建,用完以后自己close()销毁的,这样是很耗费资源的,所以我们引入DBCP DBCP简介 概述: Data Base Connection Pool, ...

  6. 【更多教程关注公众号全要买】1-2 Disruptor与BlockingQueue压力测试性能对比

    JDK所提供的BlockingQueue替换成Dis

  7. module 'keras.engine.topology' has no attribute 'load_weights_from_hdf5_group_by_name'

    参考: https://blog.csdn.net/heiheiya/article/details/81111932 https://blog.csdn.net/c20081052/article/ ...

  8. Docker学习笔记_网上资源参考

    Docker学习,网上资源参考 1.菜鸟教程:                                                        http://www.runoob.com ...

  9. c语言实践输出某个区间中不是3的倍数的偶数

    OK,先审题,我们最后要输出的那些数是需要满足两个条件的,第一个条件是,这个数不是3的倍数,第二个条件是这个数是偶数.也就是这样的数需要同时满足这两个条件的时候才把这个数输出. 不是3的倍数这个条件在 ...

  10. 嵌入式Qt开发环境的搭建详解

    一.嵌入式Qt开发环境的搭建前奏 1.下载arm-linux-gcc-4.4.3-20100728.tar.gz 2.下载qt-everywhere-opensource-src-4.8.5.tar. ...