您的位置:首页 > 理论基础 > 数据结构算法

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

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

//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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 单向链表