双链表类中的逆置成员函数
来源:互联网 发布:java解压tar.gz的命令 编辑:程序博客网 时间:2024/06/09 22:28
思路:主要有三种方法
1 逆置 data值
2 逆置节点1(将链表结点取下头插法)
3 逆置节点2(交换头尾结点,交换每个节点的next,prev指针)
头插法图示:
//***逆置双向链表*********** //1 逆置 值 //2 逆置节点1(将链表结点取下头插法) //3 逆置节点2(交换头尾结点,交换每个节点的next,prev指针) void Reverse() { DListNode*ToHead = _head; DListNode*ToTail = _tail; if (ToHead != ToTail) { while (ToHead != ToTail&&ToHead->prev != ToTail) { swap(ToHead->_data, ToTail->_data); ToHead = ToHead->next; ToTail = ToTail->prev; } } } void Reverse() { if (_head != _tail) { DListNode* tmp = _head; DListNode* cur = tmp->next; tmp->next = NULL; _tail = tmp; DListNode* NewHead = tmp; while (cur) { tmp = cur; cur = cur->next; tmp->next = NewHead; NewHead->prev = tmp; NewHead = NewHead->prev; } _head = NewHead; } } void Reverse() { if (_head != _tail) { swap(_head, _tail); DListNode* tmp = _head; while (tmp) { swap(tmp->next, tmp->prev); tmp = tmp->next; } } }
test
void test2(){ DulList A; A.Reverse(); A.PrintDulList(); A.PushBack(1); A.Reverse(); A.PrintDulList(); A.PushBack(2); A.Reverse(); A.PrintDulList(); A.PushBack(3); A.Reverse(); A.PrintDulList(); A.PushBack(4); A.Reverse(); A.PrintDulList();}
附:双向链表其他成员函数
//*****************双向链表************************typedef int DataType;struct DListNode{ DataType _data; struct DListNode* next; struct DListNode* prev;}ListNode;class DulList{private: DListNode* _head; DListNode* _tail;public: DulList() :_head(NULL) , _tail(NULL) {} //DulList(DataType x) // :_head(new DListNode) // , _tail(_head) //{ // _head->_data = x; // _head->next = NULL; // _head->prev = NULL; //} DulList(DataType x) { PushBack(x); } ~DulList() { Clear(); } void Clear() { if (_head) { DListNode* del = _head; DListNode* tmp = _head; while (tmp) { del = tmp; tmp = tmp->next; delete del; del = NULL; } _head = NULL; _tail = NULL; } } void PrintDulList() { DListNode* tmp = _head; while (tmp) { cout << tmp->_data << "->"; tmp = tmp->next; } cout << "NULL" << endl; } void PushBack(DataType x) { DListNode* tmp = new DListNode; //DulList* tmp = new DulList(x); tmp->_data = x; tmp->next = NULL; if (_head == NULL) { tmp->prev = NULL; _head = tmp; _tail = tmp; } else { tmp->prev = _tail; _tail->next = tmp; _tail = tmp; } } void PopBack() { //没有结点、一个、多个 if (_head == _tail) { if (_head) { delete _head; _head = _tail = NULL; } } else { DListNode* tmp = _head; DListNode* del = _tail; while (tmp->next != _tail) { tmp = tmp->next; } _tail = tmp; delete del; del = NULL; _tail->next = NULL; } } DListNode* Find(const DataType x) { DListNode* tmp = _head; while (tmp) { if (tmp->_data == x) break; tmp = tmp->next; } return tmp; } void Insert(DListNode* pos, const DataType x) { assert(pos); if (pos == _tail) { PushBack(x); } else { DListNode* tmp = new DListNode; tmp->_data = x; tmp->next = pos->next; pos->next->prev = tmp; pos->next = tmp; tmp->prev = pos; } } void PrintInvert() { DListNode* tmp = _tail; while (tmp) { cout << tmp->_data << "->"; tmp = tmp->prev; } cout << "NULL" << endl; }};void PrintDListNode(DListNode* dul){ DListNode* tmp = dul; while (tmp) { cout << tmp->_data << "->"; tmp = tmp->next; } cout << "NULL" << endl;}
0 0
- 双链表类中的逆置成员函数
- vector中的成员函数
- C++中的成员函数
- C++中的成员函数
- C++中的成员函数
- 成员函数中的静态变量
- C++中的const成员函数
- c++中的const 成员函数
- c++ 中的const 成员函数
- 类中的静态成员函数
- C++中的const成员函数
- 类中的const成员函数
- list类中的成员函数
- C++中的类成员函数
- c++中的静态成员函数
- C++中的const成员函数
- 类中的虚函数成员
- 继承中的静态成员函数
- 一小球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。
- ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
- 回文数与回文字符串的判断
- 不创建第三方变量对整型数组逆置
- 求斐波那契数列的第n个数(递归、非递归)
- 双链表类中的逆置成员函数
- 单链表类成员函数现代写法、传统写法
- 一个类如何只创建一个对象?
- 如何修改一个类的私有成员?
- 菱形继承的内部实现方式
- 大数运算实现加减乘除四则运算
- 求迷宫通路问题
- 广义表
- 注释转换