基本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. Python基础学习四 文件操作(二)

    ####读取文件#### with open('goods_info.txt', 'r', encoding='utf-8') as f: f.seek(0) # 注意指针位置 goods_info ...

  2. pandas中DataFrame相关

    1.创建 1.1  标准格式创建 DataFrame创建方法有很多,常用基本格式是:DataFrame 构造器参数:DataFrame(data=[],index=[],coloumns=[]) In ...

  3. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 4_Linear Regression with Multiple Variables 多变量线性回归

    Lecture 4 Linear Regression with Multiple Variables 多变量线性回归 4.1 多维特征 Multiple Features4.2 多变量梯度下降 Gr ...

  4. 通过helloworld来认识下backbone

    Backbone主要涉及3部分:model,collection和view.而这个框架的优势在于:数据与视图分离,通过操作model来自动更新view. 根据我的个人经验,直接写个简单的例子是最最直观 ...

  5. IP地址分类和子网划分

    IP地址: 地址范围                                  网络地址规律 子网掩码             私有地址       保留地址 A类地址:从1.0.0.0 到1 ...

  6. MSSQL列记录合并

    创建表及插入数据 If OBJECT_ID(N'Demo') Is Not Null Begin Drop Table Demo End Else Begin Create Table Demo( A ...

  7. FZU2282 Wand

    题意 n个数字,要求至少k个数字位置不变,其余进行错排的方案数 分析 错排公式: D(n)=(n-1)[D(n-2)+D(n-1)]  如果n个数字,i个数字位置不变,其余进行错排的的方案数是C(n, ...

  8. Hadoop完全分别式环境搭建

    为学习大数据,需搭建Hadoop大数据环境,在此记录,以备以后查阅,同时分享出来,供需要者参考. 这里分几部分进行整理. 提纲: 一.说明和准备 二.设置免密登陆 分段网址:https://www.c ...

  9. 面试题:基础数据类型 包装类 int Integer

    因为在学习集合时知道集合里存放的对象都是Object类型,取出的时候需要强制类型转换为目标类型(使用泛型集合不需要),如int a = (Integer)arrayList.get(0):然后我们就会 ...

  10. SQL 批量插入有标识列的数据

    代码:  SET IDENTITY_INSERT 表名 ON  SET IDENTITY_INSERT 表名 OFF