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

c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)

2016-05-29 22:50 471 查看
c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)
本文主要内容
1)介绍c++中双向链表的写法。
2)实现双向链表中数据的增、删、查、改、链表逆置、链表输出
3)介绍类中构造函数、析构函数、运算符重载函数的写法

接下来介绍双向链表的成员函数:这写函数放在头文件中,方便编写
List::List()
:_head(NULL),
_tail(NULL)
{}
2)构造函数中的拷贝构造
List::List(List&s)
:_head(NULL),
_tail(NULL)
{
ListNode*cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
3)赋值运算符重载
List& List::operator=(const List&s)
{
_head = NULL;
_tail = NULL;
ListNode*cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
return *this;
}
4)析构函数
List::~List()
{
Clear();
}

void List::Clear()
{
cout << ]5)增加节点的函数(三种,前增、后增、给定节点后插入)
void List::PushFront(DataType x)
{
if (_head == NULL)
{
_head = new ListNode(x);
_tail = _head;
}
else
{
ListNode*temp = new ListNode(x);
_head->_prev = temp;
temp->_next = _head;
_head = temp;
}
}

void List::PushBack(DataType x)//后增
{
if (_head == NULL)
{
_head = new ListNode(x);
_tail = _head;
}
else
{
ListNode*temp = new ListNode(x);
_tail->_next = temp;
temp->_prev = _tail;
_tail = temp;
}
}

void List::Insert(ListNode *pos, DataType x)
{
assert(pos);
if (pos == _tail)
{
PushBack(x);
}
else
{
ListNode*cur = pos->_next;
ListNode*temp = new ListNode(x);
temp->_next = cur;
temp->_prev = pos;
pos->_next = temp;
cur->_prev = temp;
}
}
6)删除节点(三种,前删、后删、删除给定节点)
7)寻找节点
ListNode* List::Find(DataType x)
{
if (_head == NULL)
{
cout << "空链表!" << endl;
return  NULL;
}
ListNode*cur = _head;
while (cur)
{
if (cur->_data == x)
return cur;
cur = cur->_next;
}
}
8)双向链表逆置(两种方法)
方法一:交换每个节点的前驱和后继。
方法二:创建新的链表,从远链表上摘节点然后补到新链表上
9)打印链表(两种,顺序打印、逆序打印)
void List::PrintList()//顺序打印
{
ListNode*cur = _head;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << endl;
}

void List::PrintReverseList()//逆序打印
{
cout << "从后向前:";
ListNode*cur = _tail;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_prev;
}
cout << "NULL" << endl;
}


当然还有主函数(测试用例)
#include"List.h"
void Test3()
{
cout << endl;
List l1;
l1.PushFront(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.PushBack(5);
l1.PushBack(6);

//l1.PushFront(4);
//l1.PopFront();//前删
//l1.PopBack();//后删

//ListNode *pos = l1.Find(3);
//l1.Insert(pos, 5);

//l1.Erase(pos);
cout << "l1 :";
l1.PrintList();
l1.PrintReverseList();
cout << "逆置l1 :";
l1.Reverse();
l1.PrintList();
l1.PrintReverseList();
cout << "拷贝构造-》 l2 :";
List l2(l1);
l2.PrintList();

cout << "赋值运算符重载 -》 l3 :";
List l3;
l3 = l2;
l3.PrintList();
}

int main()
{
Test3();
system("pause");
return 0;
}[/code] 以上函数均是在学习的过程中总结写出的,肯定会有一些疏漏或者错误之处,所以敬请各位大神的批评指正,谢谢
本文出自 “分享中进步” 博客,请务必保留此出处http://xmwen1.blog.51cto.com/10730069/1748216
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: