本篇文章会介绍如何用C++实现一个单链表类的方法,并且类中包含了对单链表的增删查改的一些基本操作,适合新手学习。话不多说,先上代码。
先看一下头文件:
Single_NodeList.h
class Single_NodeList
{
private:
// 节点结构
struct Node
{
int value;
Node *next;
Node():value(0), next(NULL){}
Node(int num):value(num), next(NULL){}
Node(int num, Node *node):value(num), next(node){}
};
// 头结点
Node *head;
// 清理链表
void clear()
{
Node *p = head;
while( p )
{
Node *q = p->next;
delete p;
p = q;
}
}
// 查找数据data的上一个节点位置的函数 为了方便插入和删除操作
Node* find_pre( int loc )
{
int n = 1;int flag = loc - 1;
if( flag == 0 ) return head;
Node *q = head->next;
while( q )
{
if( n++ == flag ) return q;
q = q->next;
}
return NULL;
}
// 查看当前链表的长度
int Size_List()
{
if( !head->next )return 0;
Node *p = head->next;
int n = 0;
while( p )
{
n++;
p = p->next;
}
return n;
}
public:
Single_NodeList(){create_list();}
~Single_NodeList(){clear();}
// 创建链表
void create_list();
// 插入节点
void insert( int data );
// 按索引 插入节点
void insert_node( int loc,int data );
// 按索引 删除节点
void delete_node( int loc );
// 按value 修改节点值
void change_node( int value, int data );
// 反转链表
void reverse();
// 打印链表
void print_list();
};
如下是cpp文件:
void Single_NodeList::create_list()
{
head = new Node(0);
}
// 头节点之后插入
void Single_NodeList::insert( int data )
{
Node *p = new Node(data);
if( head->next )
{
p->next = head->next;
head->next = p;
}
else
head->next = p;
return;
}
// 指定位置插入
void Single_NodeList::insert_node( int loc, int data )
{
// 安全检查
if( loc < 0 ) return;
int size = this->Size_List();
// 空链表则直接插入
if( size == 0 )
{
head->next = new Node(data);
}
// 如果位置大于当前链表长度 则在末端插入
else if( loc >= size )
{
Node *p = find_pre(size);
p->next->next = new Node(data);
}
// 正常情况则寻找当前位置前一节点 然后进行插入
else
{
Node *p = find_pre(loc);
Node *q = new Node(data);
q->next = p->next;
p->next = q;
}
return;
}
// 按照给定值 查找节点替换
void Single_NodeList::change_node( int value, int data )
{
Node *p = head;
while( p )
{
if( p->value == value ) p->value = data;
p = p->next;
}
return;
}
// 按照给定位置删除节点
void Single_NodeList::delete_node( int loc )
{
// 安全检查
if( loc < 0 ) return;
int size = this->Size_List();
// 空链表则直接返回
if( size == 0 )
{
return;
}
// 位置大于当前链表长度则删除末尾节点
else if( loc > size )
{
Node *p = find_pre(size);
Node *q = p->next;
p->next = NULL;
delete q;
}
// 寻找该位置前一节点 进行插入
else
{
Node *p = find_pre(loc);
Node *q = p->next;
p->next = q->next;
delete q;
}
return;
}
// 链表反转 思路是新建一个链表,遍历旧链表,依次在新链表的头结点之后插入遍历的值
// 完成上述步骤后清空旧链表,旧链表头结点替换为新链表头结点 时间复杂度O(n),空间复杂度也为O(n)
void Single_NodeList::reverse()
{
if( Size_List() == 0 ) return;
Node *p = new Node(0);
Node *p_head = head->next;
while( p_head )
{
if( p->next )
{
Node *q = new Node(p_head->value);
q->next = p->next;
p->next = q;
}
else
{
p->next = new Node( p_head->value );
}
p_head = p_head->next;
}
this->clear();
head = p;
return;
}
// 打印链表
void Single_NodeList::print_list()
{
Node *p = head->next;
if( !p ) std::cout << "There is no node here!" << std::endl;
while( p )
{
std::cout << p->value << "\t";
p = p->next;
}
std::cout << std::endl;
return;
}
测试代码:
int _tmain(int argc, _TCHAR* argv[])
{
Single_NodeList p_list;
p_list.insert(10);
p_list.insert(20);
p_list.insert(30);
p_list.insert(40);
p_list.insert(50);
p_list.print_list();
cout << "---------------------------------" << endl;
p_list.insert_node( 2, 15 );
p_list.print_list();
cout << "---------------------------------" << endl;
p_list.delete_node(3);
p_list.print_list();
cout << "---------------------------------" << endl;
p_list.change_node(30,60);
p_list.print_list();
cout << "---------------------------------" << endl;
p_list.reverse();
p_list.print_list();
cout << "---------------------------------" << endl;
return 0;
}
测试效果:
以上即为单链表的代码实现以及对各个功能的测试结果,希望能够帮助到有需要的同学,如有错误也欢迎大家批评指正。