DList àì夳堔傛蜴生んèń 2022-09-26 13:57 57阅读 0赞 #include<iostream> using namespace std; typedef int DataType; class LinkNode { public: LinkNode(const DataType &x) :_data(x) , _prev(NULL) , _next(NULL) {} friend class DList; private: DataType _data; LinkNode *_prev; LinkNode *_next; }; class DList { public: DList() :_head(NULL) , _tail(NULL) {} public: void Print() { if (_head == NULL) { return; } LinkNode *begin = _head; cout << "DList->"; while (begin) { cout << begin->_data << "->"; begin = begin->_next; } cout << endl; } void PushBack(const DataType &x) { if (_head == NULL) { _head = new LinkNode(x); _tail = _head; return; } _tail->_next = new LinkNode(x); _tail->_next->_prev = _tail; _tail = _tail->_next; } void PushFront(const DataType &x) { if (_head == NULL) { _head = new LinkNode(x); _tail = _head; return; } _head->_prev = new LinkNode(x); _head->_prev->_next = _head; _head = _head->_prev; } void PopBack() { if (_head == NULL) { return; } if (_head == _tail) { delete _head; _head = NULL; _tail = NULL; return; } _tail->_prev->_next = NULL; LinkNode *del = _tail; _tail = _tail->_prev; delete del; } void PopFront() { if (_head == NULL) { return; } if (_head == _tail) { delete _head; _head = NULL; _tail = NULL; return; } LinkNode *del = _head; _head = _head->_next; delete del; } LinkNode *Find(const DataType &x) { LinkNode *begin = _head; if (_head == NULL) { return NULL; } while (begin) { if (begin->_data == x) { return begin; } begin = begin->_next; } return NULL; } void Insert(LinkNode *n, const DataType &x) { //1.空链表 if (_head == NULL) { return; } //2.插到尾结点之后 if (n == _tail) { _tail = new LinkNode(x); n->_next = _tail; _tail->_prev = n; return; } //3.插到中间某个结点之后 LinkNode *tmp = new LinkNode(x); tmp->_next = n->_next; n->_next->_prev = tmp; n->_next = tmp; tmp->_prev = n; } void Erase(LinkNode *del) { //1.空表 if (_head == NULL) { return; } //2.只有一个结点 if (_head == _tail) { delete _head; _head = NULL; _tail = NULL; return; } //3.删除头结点 if (_head == del) { _head = _head->_next; delete del; _head->_prev = NULL; return; } //4.删除尾结点 if (del == _tail) { _tail = _tail->_prev; delete del; _tail->_next = NULL; return; } //5.删除中间结点 del->_prev->_next = del->_next; del->_next->_prev = del->_prev; delete del; } private: LinkNode *_head; LinkNode *_tail; };
相关 dlist(随机选择手机品牌列表brandlist) 数据结构问题:循环双链表有一个循环双链表,每个节点由两个指针(r 我来解答吧: void delnode(dlist \p) dlist \q,\r; q=p;-- 谁践踏了优雅/ 2023年09月26日 11:57/ 0 赞/ 71 阅读
相关 DList include<iostream> using namespace std; typedef int DataType; class LinkN àì夳堔傛蜴生んèń/ 2022年09月26日 13:57/ 0 赞/ 58 阅读
还没有评论,来说两句吧...