日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

双向链表逆置c语言,【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)...

發布時間:2024/7/23 c/c++ 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双向链表逆置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)

{

//先刪除節點,再插入節點

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節點???1、多節點兩種情況

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;

}

}

//逆置方法一:從兩頭走,交換數據

/*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;

}

}*/

//逆置方法二:交換節點的前驅和后繼

/*void?Reverse()

{

ListNode*?cur?=?_head;

while?(cur)

{

swap(cur->_prev,?cur->_next);

cur?=?cur->_prev;

}

swap(_head,?_tail);

}*/

//逆置方法三:摘節點,頭插該節點

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__

總結

以上是生活随笔為你收集整理的双向链表逆置c语言,【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。