91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用模板函數實現雙鏈表

發布時間:2021-10-21 11:15:14 來源:億速云 閱讀:111 作者:小新 欄目:開發技術

這篇文章給大家分享的是有關如何使用模板函數實現雙鏈表的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

#include <iostream>

#include <string>

using namespace std;

template<typename T>

struct Node

{

T _data;

Node<T> * _next;

Node<T> * _prev;

Node(const T&d)

:_next(NULL)

, _prev(NULL)

, _data(d)

{}

};

template<typename T>

class Dlist

{

public:

Node<T> *_head;

Node<T> *_tail;

public:

Dlist();

~Dlist();

Dlist(const Dlist<T>& list);

Dlist<T> & operator=(const Dlist<T>& list);

void PushBack(const T&d);

void Insert(int pos, const T&d);

void PopBack();

void PushFront(const T&d);

void PopFront();

void Remove(const T&d);

void RemoveAll(const T&d);

void Reverse();

void Sort();

void BubbleSort();

void print();

void DelKNode(int k);

Dlist& Merge(Dlist& l1, Dlist& l2);

void EraseNotTail(Node<T>* pos);

void InsertFrontNode(Node<T>* pos, const T& d);

Node <T>*Find(const T&d);

ostream &operator<<(ostream &os)

{

Node<T> *cur = _head;

while (cur)

{

os << cur->_data << " ";

cur = cur->_next;

}

return os;

}

};

template <typename T>

Dlist<T>::~Dlist()

{

Node<T>* cur = _head;

while (cur)

{

Node<T> *del = cur;

cur = cur->_next;

delete cur;

cur = NULL;

}

}

template <typename T>

void Dlist<T>::DelKNode(int k)

{

Node <T>*fast = _head;

Node <T>*slow = _head;

while (--k)

fast = fast->_next;

while(fast->_next)

{

fast = fast->_next;

slow = slow->_next;

}

slow->_prev->_next = slow->_next;

slow->_next->_prev = slow->_prev;

delete slow;

}

template <typename T>

void Dlist<T>::Sort()

{

Node <T> *cur = _head;

Node <T> *end = NULL;

T tmp = 0;

if (_head == NULL || _head == _tail)

return;

else

{

while (cur != end)

{

cur = _head;

while (cur->_next && cur->_next != end)

{

if (cur->_data > cur->_next->_data)

{

tmp = cur->_data;

cur->_data = cur->_next->_data;

cur->_next->_data = tmp;

}

cur = cur->_next;

}

end = cur;

}

}

}

template <typename T>

void Dlist<T>::BubbleSort()

{

Node <T>*cur = _head;

Node <T>*end = NULL;

while (cur != end)

{

cur = _head;

while (cur->_next && cur->_next != end)

{

if (cur->_data > cur->_next->_data)

{

T tem = cur->_data;

cur->_data = cur->_next->_data;

cur->_next->_data = tem;

}

cur = cur->_next;

}

end = cur;

}

}

template <typename T>

void Dlist<T>::EraseNotTail(Node<T>* pos)

{

pos->_prev->_next = pos->_next;

pos->_next->_prev = pos->_prev;

delete pos;

pos = NULL;

}

template <typename T>

void Dlist<T>::Remove(const T&d)

{

Node <T>*cur = _head;

while (cur)

{

if (cur->_data == d)

{

if (cur == _head)

{

PopFront();

return;

}

else

if (cur == _tail)

{

PopBack();

return;

}

else

{

cur->_next->_prev = cur->_prev;

cur->_prev->_next = cur->_next;

delete cur;

return;

}

}

cur = cur->_next;

}

}

template <typename T>

Node <T>*Dlist<T>::Find(const T&d)

{

Node <T>*cur = _head;

while (cur)

{

if (cur->_data == d)

return cur;

cur = cur->_next;

}

return NULL;

}

template <typename T>

void Dlist<T>::RemoveAll(const T&d)

{

Node <T>*cur = _head;

Node <T>*del = NULL;

while (cur)

{

if (cur->_data == d)

{

del = cur;

if (cur == _head)

{

PopFront();

cur = _head;

}

else if (cur == _tail)

{

PopBack();

cur = _head;

}

else

{

cur->_prev->_next = cur->_next;

cur->_next->_prev = cur->_prev;

cur = cur->_prev->_next;

}

delete del;

}

else

{

cur = cur->_next;

}

}

}

template <typename T>

void Dlist<T>::print()

{

Node<T>* cur = _head;

while (cur)

{

cout << cur->_data << "->";

cur = cur->_next;

}

cout << "over" << endl;

}

template <typename T>

Dlist<T>&Dlist<T>::Merge(Dlist& l1, Dlist& l2)

{

Node <T>*cur = NULL;

Node <T>*p1 = l1._head;

Node <T>*NewNode = p1;

Node <T>*p2 = l2._head;

if (p1 == p2)

return l1;

if (p1 == NULL && p2 != NULL)

return l2;

if(p1 != NULL && p2 == NULL)

return l1;

else

if (p1->_data < p2->_data)

{

p1 = p1->_next;

}

else

{

NewNode->_data = p2->_data;

p2 = p2->_next;

}

cur = NewNode;

while (p1&&p2)

{

if (p1->_data < p2->_data)

{

cur->_next = p1;

p1->_prev = cur;

p1 = p1->_next;

cur = cur->_next;

}

else

{

cur->_next = p2;

p2->_prev = cur;

cur = cur->_next;

p2 = p2->_next;

}

}

if (p1)

{

cur->_next = p1;

p1->_prev = cur;

}

else

{

cur->_next= p2;

p2 = p2->_next;

}b

return l1;

}

template <typename T>

Dlist<T> & Dlist<T>::operator =(const Dlist<T>&list)

{

Node <T> *del = _head;

Node <T>* tem = _tail;

if (this != &list)

{

delete _head;

_head = NULL;

delete _tail;

_tail = NULL;

Node<T> *cur = list._head;

while (cur)

{

Dlist<T>::PushBack(cur->_data);

cur = cur->_next;

}

}

return *this;

}

template <typename T>

void Dlist<T>::Insert(int pos, const T&d)

{

Node <T>*NewNode = new Node<T>(d);

Node <T>*cur = _head;

while (cur && (--pos))

{

cur = cur->_next;

}

NewNode->_next = cur->_next;

NewNode->_prev = cur;

cur->_next = NewNode;

}

template <typename T>

Dlist<T>::Dlist()

{

_head = NULL;

_tail = NULL;

}

template <typename T>

void Dlist<T>::PushFront(const T&d)

{

Node<T>* NewNode = new Node<T>(d);

if (_head == NULL)

{

_head = NewNode;

_tail = _head;

}

else

{

_head->_prev = NewNode;

NewNode->_next = _head;

_head = NewNode;

}

}

template <typename T>

void Dlist<T>::PushBack(const T&d)

{

Node<T>* NewNode = new Node<T>(d);

if (_head == NULL)

{

_head = NewNode;

_tail = _head;

}

else

{

_tail->_next = NewNode;

NewNode->_prev = _tail;

_tail = NewNode;

}

}

template <typename T>

void Dlist<T>::PopBack()

{

if (_head == NULL)

return;

else if (_head == _tail)

{

delete _head;

_head = NULL;

_tail = _head;

}

else

{

_tail = _tail->_prev;

_tail->_next = NULL;

}

}

template <typename T>

void Dlist<T>::Reverse()

{

Node<T>* NewNode = NULL;

Node <T> *cur = _head;

Node<T> *prev = NULL;

while (cur->_next)

{

prev = cur;

cur = cur->_next;

prev->_next = NewNode;

prev->_prev = cur;

NewNode = prev;

}

_tail = _head;

_tail->_next = NULL;

_head = cur;

_head->_prev = NULL;

_head->_next = NewNode;

}

template <typename T>

void Dlist<T>::PopFront()

{

if (_head == NULL)

return;

else if (_head == _tail)

{

delete _head;

_head = NULL;

_tail = _head;

}

else

{

_head = _head->_next;

_head->_prev = NULL;

}

}

template <typename T>

Dlist<T>::Dlist(const Dlist<T>& list)

{

Node<T> *cur = list._head;

while (cur)

{

Dlist<T>::PushBack(cur->_data);

cur = cur->_next;

}

}

int main()

{

Dlist<int> list1;

Dlist <int>list2;

Node <int>*tem = NULL;

Node <int>*cur = NULL;

list1.PushBack(1);

list1.PushBack(2);

list1.PushBack(3);

list1.PushBack(2);

/*list2.PushBack(5);

list2.PushBack(6);

list2.PushBack(7);

list2.PushBack(8);

list2.Merge(list1, list2)<<cout;*/

list1.Insert(2, 5);

///*list1.Reverse();

//list1.PushFront(5);

//list1.PopBack();*/

///*Dlist <int> list2;

//list2 = list1;*/

list1.RemoveAll(2);

/*list1.BubbleSort();*/

list1.print();

getchar();

return 0;

}

感謝各位的閱讀!關于“如何使用模板函數實現雙鏈表”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

淳化县| 迁西县| 揭西县| 东光县| 行唐县| 安康市| 榆中县| 灌云县| 高碑店市| 安龙县| 天长市| 饶阳县| 盐亭县| 镇江市| 沾益县| 南皮县| 义马市| 大庆市| 栾城县| 方山县| 桐乡市| 平潭县| 涪陵区| 合阳县| 凤冈县| 綦江县| 驻马店市| 三原县| 荔浦县| 万宁市| 闽侯县| 牡丹江市| 句容市| 松原市| 洱源县| 察雅县| 枣庄市| 鄂尔多斯市| 临海市| 阿图什市| 湛江市|