您的位置:首页 > 编程语言 > C语言/C++

C语言实现的单向链表

2011-08-22 13:41 513 查看
/**
* @file GM_SLink.h
* @brief 提供了常用的单向链表操作接口
* @author Don Hao
* @date 2011-8-21 9:38:03
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>hao.limin@gmail.com</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#ifndef _GM_SLINK_H
#define _GM_SLINK_H
#include <stdlib.h>

#ifdef __cplusplus
extern"C"
{
#endif /**< __cplusplus */

    /** 
    * @brief GM_SLink_Add_Head 
    * 
    * 在链表头部添加数据.
    * @param[in] value 
    * @return int  
    */
    int GM_SLink_Add_Head(int value);

    /** 
    * @brief GM_SLink_Add_Tail 
    * 
    * 在链表尾部添加数据.
    * @param[in] value 
    * @return int  
    */
    int GM_SLink_Add_Tail(int value);

    /** 
    * @brief GM_SLink_Get_Value 
    * 
    * 获取第i个节点的数据.
    * @param[in] index 
    * @param[out] pValue 
    * @return int  -1:失败;+1成功
    */
    int GM_SLink_Get_Value(int index, int* pValue);

    /** 
    * @brief GM_SLink_Get_Length 
    * 
    * 获取链表长度.
    * @return int  
    */
    int GM_SLink_Get_Length();

    /** 
    * @brief GM_SLink_Remove_Head 
    * 
    * 删除第一个元素.
    * @return int  
    */
    int GM_SLink_Remove_Head();

    /** 
    * @brief GM_SLink_Remove_Tail 
    * 
    * 删除最后一个元素.
    * @return int  
    */
    int GM_SLink_Remove_Tail();

    /** 
    * @brief GM_SLink_Remove 
    * 
    * 删除第i个元素.
    * @param[in] index 
    * @return int  
    */
    int GM_SLink_Remove(int index);

    /** 
    * @brief GM_SLink_Remove_All 
    * 
    * 删除所有元素.
    * @return int  
    */
    int GM_SLink_Remove_All();

#ifdef __cplusplus
}
#endif /**< __cplusplus */

#endif /**< _GM_LINK_H */




/**
* @file Link.c
* @brief 
* @author Don Hao
* @date 2011-8-22 9:38:59
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>hao.limin@gmail.com</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#include "GM_SLink.h"

typedef struct Link
{
    int vlaue;
    struct Link* next;
}Link_Struct;

static Link_Struct* head = NULL;
static int count         = 0;

static Link_Struct* createNode(int value)
{
    Link_Struct* node = (Link_Struct*)malloc(sizeof(Link_Struct));

    node->vlaue = value;
    node->next = NULL;

    return node;
}

int GM_SLink_Get_Length()
{
    return count;
}

int GM_SLink_Add_Head( int value )
{
    Link_Struct* node = createNode(value);

    if (NULL == node)
    {
        return -1;
    }

    if (NULL == head)
    {
        head = node;
    }
    else
    {
        node->next = head;
        head = node;
    }

    ++count;
    return 1;
}

int GM_SLink_Add_Tail( int value )
{
    Link_Struct* node = head;
    Link_Struct* newNode = NULL;

    if (NULL == head)
    {
        return GM_SLink_Add_Head(value);
    }

    while (NULL != node->next)
    {
        node = node->next;
    }

    newNode = createNode(value);

    if (NULL == newNode)
    {
        return -1;
    }

    node->next = newNode;

    ++count;

    return 1;
}

int GM_SLink_Get_Value( int index, int* pValue)
{
    int i = 0;
    Link_Struct* node = NULL;

    if ((index >= count) || (NULL == pValue))
    {
        return -1;
    }

    for (i = 0; i <= index; ++i)
    {
        if(0 == i)
        {
            node = head;
        }

        if (NULL == node)
        {
            return -1;
        }

        if (i == index)
        {
            *pValue = node->vlaue;

            return 1;
        }

        node = node->next;
    }

    return 1;
}

int GM_SLink_Remove_Head()
{
    Link_Struct* node = head;

    if (NULL == head)
    {
        return 1;
    }

    node = head;
    head = node->next;
    free(node);
    node = NULL;

    --count;

    return 1;
}

int GM_SLink_Remove_Tail()
{
    Link_Struct* node = head;
    Link_Struct* preNode = NULL;

    if ((1 >= count) || (NULL == head))
    {
        return 1;
    }

    while (NULL != node->next)
    {
        preNode = node;
        node = node->next;
    }

    preNode->next = NULL;
    free(node);

    node = NULL;

    --count;

    return 1;
}

int GM_SLink_Remove( int index )
{
    Link_Struct* node = NULL;
    Link_Struct* preNode = NULL;
    int i = 0;

    if (index >= count)
    {
        return -1;
    }

    for (i = 0; i <= index; ++i)
    {
        if (0 == i)
        {
            node = head;
        }

        if (NULL == node)
        {
            return -1;
        }

        if (i == index)
        {
            if (NULL == preNode)
            {
                head = node->next;
            }
            else
            {
                preNode->next = node->next;
            }

            free(node);
            node = NULL;
            --count;
            return 1;
        }

        preNode = node;
        node = node->next;
    }

    return 1;
}

int GM_SLink_Remove_All()
{
    while (count > 0)
    {
        GM_SLink_Remove_Head();
    }

    return 1;
}

/* For Test
int main()
{
    int i = 0;
    for (i = 0; i < 10; ++i)
    {
        GM_SLink_Add_Tail(i);
    }

    GM_SLink_Get_Value(5, &i);

    GM_SLink_Remove_Head();
    GM_SLink_Remove_Tail();
    GM_SLink_Remove_Head();
    GM_SLink_Remove_Tail();
    GM_SLink_Remove(0);
    GM_SLink_Remove(1);
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: