c语言链表的去重,【C++】模版实现双向链表的各种操作(如:逆置、去重、分类、合并)...

在cplusplus.com里,我们可以搜索list来查看库是如何实现双向链表的。当然,我们也可以在平时使用时包上头文件list来调用C++里的list库。在这里,我今天就不再赘述用C语言或者C++未引入模版这两种场景来向大家分享双向链表了,而是注重多类型都可以使用双向链表。也就是我们今天的主题:模版实现双向链表。

我主要实现了尾插、尾删、打印、去重、逆置等等一系列操作,而关于像Insert()、Erase()之类的操作我并没有在这里全部实现,一方面是因为之前我们反复实现过,如果你感兴趣的话,可以查看我之前的博客。另一方面,我出于较为低效不常见的原因。希望大家理解。

另外,需要说明的一点是,今天我使用的均是类内声明,类外定义的方式。

代码如下:#include

using namespace std;

template

struct ListNode

{

ListNode(const T& x)

:_next(NULL)

, _prev(NULL)

, _data(x)

{}

ListNode* _next;

ListNode* _prev;

T _data;

};

template

class List

{

public:

List()

:_head(NULL)

, _tail(NULL)

{}

List(const List& l)

{

ListNode* cur = l._head;

while (cur)

{

this->PushBack(cur->_data);

cur = cur->_next;

}

}

List& operator=(const List& l)

{

//先删除节点,再插入节点

if (&s != this)

{

ListNode* pcur = _head;

while (pcur)

{

ListNode* del = pcur;

pcur = pcur->_next;

delete del;

del = NULL;

}

ListNode* cur = _head;

while (cur)

{

this->PushBack(cur->_data);

cur = cur->_next;

}

}

return *this;

}

~List()//一个节点一个节点的删除

{

ListNode* cur = _head;

while (cur)

{

ListNode* del = cur;

cur = cur->_next;

delete del;

del = NULL;

}

}

void PushBack(const T& x);

void PopBack();

void Unique();

void PrintList();

void Reverse();

int Length();

void Sort();

protected:

ListNode* _head;

ListNode* _tail;

};

//尾插

template

void List::PushBack(const T& x)

{

//分析:分为两种情况:无节点、有节点

if (_head == NULL)

{

_head = _tail = new ListNode(x);

}

else

{

ListNode* cur = new ListNode(x);

_tail->_next = cur;

cur->_prev = _tail;

_tail = cur;

_tail->_next = NULL;

}

}

//尾删

template

void List::PopBack()

{

//分析:分为三种情况:无节点、一个节点、多个节点

if (_head == _tail)

{

if (_head == NULL)

{

return;

}

else

{

delete _head;

_head = _tail = NULL;

}

}

else

{

ListNode* prev = _tail->_prev;

delete _tail;

_tail = NULL;

_tail = prev;

_tail->_next = NULL;

}

}

//去重:前提是针对已排序的有重复数据的链表

//template

//void List::Unique()

//{

//    //分析:分为三种情况:无节点一个节点(无需删除节点)、两个节点、两个以上节点

//    if (_head == _tail)

//    {

//        return;

//    }

//    else

//    {

//        ListNode* pcur = _head;

//        ListNode* pnext = _head->_next;

//        if (pnext->_next == NULL)    //两个节点

//        {

//            if (pcur->_data == pnext->_data)

//            {

//                delete pnext;

//                pnext = NULL;

//                _tail = _head = pcur;

//                return;

//            }

//            else

//            {

//                return;

//            }

//        }

//        else

//        {

//            //两个以上节点

//            ListNode* cur = _head;

//            while (cur->_next)

//            {

//                ListNode* next = cur->_next;

//                ListNode* nextnext = next->_next;

//                if (cur->_data == next->_data)

//                {

//                    cur->_next = nextnext;

//                     nextnext->_prev = cur;

//                    delete next;

//                    next = NULL;

//                }

//                cur = cur->_next;

//            }

//        }

//    }

//}

//逆置

template

void List::Reverse()

{

//分析:从两头开始走,交换数据(分奇数个数据和偶数个数据)

ListNode* begin = _head;

ListNode* end = _tail;

while (!((begin == end) || end->_next == begin))

{

swap(begin->_data, end->_data);

begin = begin->_next;

end = end->_prev;

}

}

//长度

template

int List::Length()

{

ListNode* cur = _head;

int count = 0;

while (cur)

{

count++;

cur = cur->_next;

}

return count;

}

//分类

//template

//void List::Sort()

//{

//    //使用冒泡排序,实现升序或者降序

//    ListNode* i = _head;

//    ListNode* j = _head;

//    for (i = _head; i != _tail; i++)

//    {

//        for (j = _head->_next; j 

//        {

//            if (j->_data >(j + 1)->_data)

//            {

//                swap(j->_data, (j + 1)->_data);

//            }

//        }

//    }

//}

//打印

template

void List::PrintList()

{

ListNode* cur = _head;

while (cur)

{

cout <_data <";

cur = cur->_next;

}

cout <

}

void Test()

{

List l1;

l1.PushBack(1);

l1.PushBack(2);

l1.PushBack(2);

l1.PushBack(3);

l1.PushBack(4);

l1.PushBack(4);

l1.PushBack(5);

l1.PrintList();

l1.PopBack();

l1.PrintList();

/*l1.Unique();

l1.PrintList();*/

l1.Reverse();

l1.PrintList();

/*l1.Sort();

l1.PrintList();*/

}

int main()

{

Test();

system("pause");

return 0;

}

原文:http://10740184.blog.51cto.com/10730184/1750695

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值