c++单向链表-数据结构

单向链表看着简单,实现起来还是需要很仔细,指向的元素需要想清楚,内存需要能正确释放,代码要讲究可读性,试了一下午,以下是自定义的实现:

//list.h

#ifndef LIST_H_H_
#define LIST_H_H_
#include<cstdio>
#include<cstdlib>
#include<cassert>

/** 节点中的具体的数据类型 */
typedef int DataType;

/** 链表中的节点 */
struct Node
{
    DataType data_;
    Node* next;
};

/** 表头 */
struct List
{
    Node* head; ///<指向链表的第一个节点
};

/** 初始化链表 */
void list_init(List* list);;

/** 插入数据(在链表头部) */
int list_insert(List* list, DataType data);

/** 在指定的元素后面插入数据 */
int list_insert(List* list, DataType element, DataType data);

/** 删除节点 */
int list_remove(List* list, DataType data);

/** 寻找节点,返回找到的节点或NULL */
Node* list_find(List* list, DataType data);

/** 链表大小 */
int list_size(List* list);

/** 删除链表(不含表头) */
void list_destroy(List* list);;


#endif // !LIST_H_H_


//list.cpp

#include "list.h"

/** 初始化链表 */
void list_init(List * list)
{
    list->head = NULL;
}

/** 插入数据(在链表头部) */
int list_insert(List * list, DataType data)
{
    Node* newNode = (Node*)malloc(sizeof(Node));

    if (newNode == NULL)
    {
        fprintf(stderr, "out of memory");
        return -1;
    }

    newNode->data_ = data;
    newNode->next = list->head;
    list->head = newNode;

    return 0;
}

/** 在指定的元素后面插入数据 */
int list_insert(List * list, DataType element, DataType data)
{
    Node* p = list_find(list, element);
    if (p == NULL)
    {
        fprintf(stderr, "can't find the element");
        return -1;
    }

    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL)
    {
        fprintf(stderr, "out of memory");
        return -1;
    }

    newNode->data_ = data;
    newNode->next = p->next;
    p->next = newNode;

    return 0;
}

/** 删除节点 */
int list_remove(List * list, DataType data)
{
    Node* tempNode;
    Node* p = list->head;

    //是否是空链表
    if (p == NULL)
    {
        return -1;
    }

    //如果是第一个节点
    if (p->data_ == data)
    {
        tempNode = p->next;

#ifdef _DEBUG 
        //test code
        printf("node remove:%d\n", p->data_);
#endif // _DEBUG

        free(p);
        list->head = tempNode;

        return 0;
    }
    else
    {   //遍历下一个节点
        while ((p->next != NULL) && (p->next->data_ != data))
        {
            p = p->next;
        }

        if (p->next==NULL)
        {
            fprintf(stderr, "not found element at %s\n",__FUNCTION__);
            return -1;
        }
        else
        {
            tempNode = p->next->next;

#ifdef _DEBUG 
            //test code
            printf("node remove:%d\n", p->next->data_);
#endif // _DEBUG

            free(p->next);
            p->next = tempNode;
            return 0;
        }
    }
}

/** 寻找节点,返回找到的节点或NULL */
Node * list_find(List * list, DataType data)
{
    Node* p = list->head;
    while ((p != NULL) && (p->data_ != data))
    {
        p = p->next;
    }

    return p;
}

/** 链表大小 */
int list_size(List * list)
{
    Node* p = list->head;
    int count = 0;

    while (p != NULL)
    {
        count++;
        p = p->next;
    }

    return count;
}

/** 删除链表(不含表头) */
void list_destroy(List * list)
{
    Node* tempNode;
    Node* p = list->head;
    while (p != NULL)
    {
        tempNode = p->next;

#ifdef _DEBUG 
        //test code
        printf("node destroy:%d\n", p->data_);
#endif // _DEBUG

        free(p);
        p = tempNode;

    }
    list->head = NULL;
}

//测试一下:

#include<iostream>
#include"list.h"

int main()
{
    List list;
    list_init(&list);

    list_insert(&list, 3);
    list_insert(&list, 2);
    list_insert(&list, 1);
    list_insert(&list, 5);
    list_insert(&list, 1, 7);

    list_remove(&list, 1);

    Node* first = list.head;
    while (first!=NULL)
    {
        std::cout << first->data_ << std::endl;
        first = first->next;
    }

    list_remove(&list, 9);

    Node* f1=list_find(&list,2);
    if (f1)
    {
        std::cout << "find:" << f1->data_ << std::endl;
    }
    else
    {
        std::cout << "not find" << std::endl;
    }

    Node* f2 = list_find(&list, 22);
    if (f2)
    {
        std::cout << "find:" << f2->data_ << std::endl;
    }
    else
    {
        std::cout <<"not find"<< std::endl;
    }

    std::cout << "list size--------------:" << list_size(&list) << std::endl;
    list_destroy(&list);
    std::cout << "list size after destroy:" << list_size(&list) << std::endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值