双链表类中的逆置成员函数

来源:互联网 发布:java解压tar.gz的命令 编辑:程序博客网 时间:2024/06/09 22:28

  思路:主要有三种方法

     1 逆置 data值
     2 逆置节点1(将链表结点取下头插法)
     3 逆置节点2(交换头尾结点,交换每个节点的next,prev指针)



头插法图示:

wKioL1baglLxzCx4AAAjjJg98KY040.png

//***逆置双向链表*********** //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
原创粉丝点击