★C++实现双向链表的基础操作(类的实现)
#include<iostream> #include<cassert> using namespace std; typedef int DataType; class double_link_list { //定义双向链表类,包括了双向的前驱和后继指针,以及对象的初始化 public: friend class ListNode; double_link_list(DataType x = 0) :_data(x) ,_next(NULL) ,_prev(NULL) {} private: double_link_list* _prev; double_link_list* _next; DataType _data; }; class ListNode { public: //注意上一类中的friend关键字,友元函数,否则该类无法访问其类的private成员 ListNode() //构造函数 :_head(NULL) ,_tail(NULL) { } ~ListNode()//析构函数 { Clear();//清除函数 } ListNode(const ListNode& list) //拷贝构造函数 { double_link_list *cur = list._head; while (cur) { PushBack(cur->_data); } } ListNode operator = (ListNode list) //赋值运算符重载函数 { double_link_list *cur = list._head; while (cur) { PushBack(cur->_data); } } public: //尾插 void PushBack(const DataType& x); //尾删 void PopBack(); //头插 void PushFront(const DataType& x); //头删 void PopFront(); //指定位置插入节点 void Insert(double_link_list* pos, const DataType& x); //查找节点函数 double_link_list* Find(const DataType& x); //删除节点函数 void Erase(const DataType& x); //逆置链表函数 void Reverse(); //输出函数 void Print(); private: void Clear() { while (_head) { double_link_list* cur = _head; _head = _head->_next; delete cur; } } double_link_list *_head; double_link_list *_tail; }; //尾插 void ListNode::PushBack(const DataType&x) //注意域作用符的添加,否则只得在类体中定义 { if (_head == NULL) { _tail = _head = new double_link_list(x); _head->_next = NULL; _head->_prev = NULL; } else { _tail->_next = new double_link_list(x); _tail->_next->_prev = _tail; _tail = _tail->_next; } } //头插 void ListNode::PushFront(const DataType&x) { double_link_list* cur = new double_link_list(x); if (_head == NULL) { _tail = _head = cur; } else { cur->_next = _head; cur->_next->_prev = cur; _head = cur; } } //尾删 void ListNode::PopBack() { if (_tail) { double_link_list *cur = _tail; _tail = _tail->_prev; _tail->_next = NULL; delete cur; } } //头删 void ListNode::PopFront() { if (_head) { double_link_list *cur = _head; _head = _head->_next; _head->_prev = NULL; delete cur; } } //指定位置插入节点 void ListNode::Insert(double_link_list* pos, const DataType& x) { assert(_head); double_link_list *cur = _head; while (cur) { if (cur == pos) { if (cur == _tail) { PushBack(x); } else { double_link_list *tmp = new double_link_list(x); tmp->_next = cur->_next; tmp->_prev = cur; tmp->_next->_prev = tmp; cur->_next = tmp; } return; } cur = cur->_next; } } //查找指定节点 double_link_list* ListNode::Find(const DataType& x) { double_link_list* cur = _head; while (cur) { if (cur->_data == x) return cur; cur = cur->_next; } return NULL; } //删除某一节点 void ListNode::Erase(const DataType &x) { double_link_list *cur = _head; while (cur) { if (cur->_data == x) { double_link_list* tmp = cur; cur->_prev->_next = cur->_next; cur->_next->_prev = cur->_prev; cur = cur->_next; delete tmp; } } } //逆置双向链表 void ListNode::Reverse() { std::swap(_head, _tail); double_link_list* cur = _head; while (cur) { std::swap(cur->_next, cur->_prev); cur = cur->_next; } } //输出双向链表 void ListNode::Print() { double_link_list* cur = _head; while (cur) { cout << cur->_data << "->"; cur = cur->_next; } cout << "NULL" << endl; }
本文出自 “温暖的微笑” 博客,谢绝转载!
原文地址:http://10738469.blog.51cto.com/10728469/1747889