重新教自己学算法之单向链表(九)

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
using namespace std;

#define STATUS int
#define TRUE 1
#define FALSE 0

typedef struct _LINK_NODE
{
    int data;
    struct _LINK_NODE* next;
}LINK_NODE;

LINK_NODE* alloca_node(int value)
{
    LINK_NODE* pLinkNode = NULL;
    pLinkNode = (LINK_NODE*)malloc(sizeof(LINK_NODE));

    pLinkNode->data = value;
    pLinkNode->next = NULL;
} 

//删除整个链表
void delete_node(LINK_NODE** pNode)
{
    LINK_NODE** pNext;
    if(NULL == pNode || NULL == *pNode)
        return ;
    pNext = &((*pNode)->next);
    free(*pNode);
    delete_node(pNext);
}

STATUS _add_data(LINK_NODE** pNode, LINK_NODE* pDataNode)
{
    if(NULL == *pNode)
    {
        *pNode = pDataNode;
        return TRUE;
    }
    return _add_data(&(*pNode)->next, pDataNode);
}
STATUS add_data( LINK_NODE** pNode, int value)
{
    LINK_NODE* pDataNode;
    if(NULL == *pNode)
        return FALSE;

    pDataNode = alloca_node(value);
    assert(NULL != pDataNode);
    return _add_data((LINK_NODE**)pNode, pDataNode);
}

STATUS _delete_data(LINK_NODE** pNode, int value)
{
    LINK_NODE* pLinkNode;
    if(NULL == (*pNode)->next)
        return FALSE;

    pLinkNode = (*pNode)->next;
    if(value == pLinkNode->data)
    {
        (*pNode)->next = pLinkNode->next;
        free(pLinkNode);
        return TRUE;
    }
    else
    {
        return _delete_data(&(*pNode)->next, value);
    }
}
STATUS delete_data(LINK_NODE** pNode, int value)
{
    LINK_NODE* pLinkNode;
    if(NULL == pNode || NULL == *pNode)
        return FALSE;

    if(value == (*pNode)->data)
    {
        pLinkNode = *pNode;
        *pNode = pLinkNode->next;
        free(pLinkNode);
        return TRUE;
    }

    return _delete_data(pNode, value);
}

LINK_NODE* find_data(const LINK_NODE* pLinkNode, int value)
{
    if(NULL == pLinkNode)
        return NULL;
    if(value == pLinkNode->data)
    {
        return (LINK_NODE*)pLinkNode;
    }

    return find_data(pLinkNode->next ,value);   
}

void print_node(const LINK_NODE* pLinkNode)
{
    if(pLinkNode)
    {
        cout<<pLinkNode->data<<"  ";
        print_node(pLinkNode->next);
    }
    cout<<endl;
}

int count_node(const LINK_NODE* pLinkNode)
{
    if(NULL == pLinkNode)
        return 0;

    return 1 + count_node(pLinkNode->next); 
}

int main()
{
    LINK_NODE* pLinkNode = alloca_node(10);
    LINK_NODE** pNode = &pLinkNode;
    add_data(pNode,15);
    add_data(pNode,20);
    cout<<pLinkNode<<endl;
    cout<<pLinkNode->next<<endl;
    cout<<pLinkNode->next->next<<endl;
    cout<<*pNode<<endl;        //pNode里存放的是链表的首地址
    // print_node(pLinkNode);

    // delete_data(pNode,15);
    // print_node(pLinkNode);

    // print_node(pLinkNode);
    // cout<<pLinkNode<<endl;
    // cout<<&(*pNode)->next<<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值