双向链表逆置c语言,【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)...
建立源文件List.cppinclude?"List.h"
int?main()
{
Test();
system("pause");
return?0;
}
建立頭文件List.h#ifndef?__LISH_H__
#define?__LISH_H__
#include
using?namespace?std;
typedef?int?DataType;
struct?ListNode
{
ListNode(DataType?x)
:_next(NULL)
,?_prev(NULL)
,?_data(x)
{}
ListNode*?_next;
ListNode*?_prev;
DataType?_data;
};
class?List
{
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const?List&?s)
:_head(NULL)
,?_tail(NULL)
{
ListNode*?cur?=?s._head;
while?(cur)
{
this->PushBack(cur->_data);
cur?=?cur->_next;
}
}
List&?operator=?(const?List&?s)
{
//先刪除節(jié)點(diǎn),再插入節(jié)點(diǎn)
if?(&s?!=?this)
{
ListNode*?pcur?=?_head;
while?(pcur)
{
ListNode*?del?=?pcur;
pcur?=?pcur->_next;
delete?del;
del?=?NULL;
}
ListNode*?cur?=?s._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(DataType?x)
{
//分:0節(jié)點(diǎn)???1、多節(jié)點(diǎn)兩種情況
if?(_head?==?NULL)
{
_head?=?new?ListNode(x);
_tail?=?_head;
_tail->_next?=?NULL;
}
else
{
ListNode*?cur?=?new?ListNode(x);
_tail->_next?=?cur;
cur->_prev?=?_tail;
_tail?=?cur;
_tail->_next?=?NULL;
}
}
//尾刪
void?PopBack()
{
if?(_head?==?_tail)
{
if?(_head?!=?NULL)
{
delete?_head;
_head?=?NULL;
_tail?=?NULL;
}
else
{
return;
}
}
else
{
ListNode*?prev?=?_tail->_prev;
delete?_tail;
_tail?=?NULL;
_tail?=?prev;
_tail->_next?=?NULL;
}
}
//頭插
void?PushFront(DataType?x)
{
if?(_head?==?NULL)
{
PushBack(x);
}
else
{
ListNode*?index?=?new?ListNode(x);
index->_next?=?_head;
_head->_prev?=?index;
_head?=?index;
_head->_prev?=?NULL;
}
}
//頭刪
void?PopFront()
{
if?(_head?==?_tail)
{
PopBack();
}
else
{
ListNode*?del?=?_head;
ListNode*?next?=?_head->_next;
_head?=?next;
_head->_prev?=?NULL;
delete?del;
del?=?NULL;
}
}
//插入元素
void?Insert(size_t?pos,?DataType?x)
{
//分:是尾插???不是尾插????兩種情況
ListNode*?cur?=?_head;
while?(--pos)
{
cur?=?cur->_next;
}
if?(cur?==?_tail)
{
PushBack(x);
}
else
{
ListNode*?index?=?new?ListNode(x);
ListNode*?prev?=?cur->_prev;
index->_next?=?cur;
cur->_prev?=?index;
prev->_next?=?index;
index->_prev?=?prev;
}
}
//查找元素
ListNode*?Find(DataType?x)
{
ListNode*?cur?=?_head;
while?(cur)
{
if?(cur->_data?==?x)
{
return?cur;
}
cur?=?cur->_next;
}
return?NULL;
}
//刪除元素
void?Erase(ListNode*?pos)
{
if?(pos?==?_head)
{
PopFront();
}
else?if?(pos?==?_tail)
{
PopBack();
}
else
{
ListNode*?prev?=?pos->_prev;
ListNode*?next?=?pos->_next;
prev->_next?=?next;
next->_prev?=?prev;
delete?pos;
pos?=?NULL;
}
}
//逆置方法一:從兩頭走,交換數(shù)據(jù)
/*void?Reverse()
{
ListNode*?begin?=?_head;
ListNode*?end?=?_tail;
while?(!((begin?==?end)?||?(end->_next?==?begin)))
{
swap(begin->_data,?end->_data);
begin?=?begin->_next;
end?=?end->_prev;
}
}*/
//逆置方法二:交換節(jié)點(diǎn)的前驅(qū)和后繼
/*void?Reverse()
{
ListNode*?cur?=?_head;
while?(cur)
{
swap(cur->_prev,?cur->_next);
cur?=?cur->_prev;
}
swap(_head,?_tail);
}*/
//逆置方法三:摘節(jié)點(diǎn),頭插該節(jié)點(diǎn)
void?Reverse()
{
ListNode*?cur?=?_head;
ListNode*?newhead?=?NULL;
while?(cur)
{
ListNode*?tmp?=?cur;
cur?=?cur->_next;
if?(newhead?==?NULL)
{
newhead?=?tmp;
newhead->_next?=?NULL;
newhead->_prev?=?NULL;
_head?=?_tail?=?newhead;
}
else
{
newhead->_prev?=?tmp;
tmp->_next?=?newhead;
newhead?=?tmp;
_head?=?newhead;
_head->_prev?=?NULL;
}
}
}
//打印
void?PrintList()
{
ListNode*?cur?=?_head;
while?(cur)
{
cout?<_data?<";
cur?=?cur->_next;
}
cout?<
}
private:
ListNode*?_head;
ListNode*?_tail;
};
void?Test()
{
List?s;
s.PushBack(1);
s.PushBack(2);
s.PushBack(3);
s.PushBack(4);
s.PushBack(5);
s.PrintList();
s.PopBack();
s.PrintList();
s.PushFront(0);
s.PrintList();
s.PopFront();
s.PrintList();
s.Insert(2,?10);
s.PrintList();
s.Reverse();
s.PrintList();
}
#endif?//__LIST_H__
總結(jié)
以上是生活随笔為你收集整理的双向链表逆置c语言,【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java中po代码示例_java操作or
- 下一篇: 两个oracle数据库外网同步,利用DB