In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use template functions to implement double-linked lists. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
# include
# include
Using namespace std
Template
Struct Node
{
T _ data
Node * _ next
Node * _ prev
Node (const titled)
: _ next (NULL)
, _ prev (NULL)
, _ data (d)
{}
}
Template
Class Dlist
{
Public:
Node * _ head
Node * _ tail
Public:
Dlist ()
~ Dlist ()
Dlist (const Dlist& list)
Dlist& operator= (const Dlist& list)
Void PushBack (const titled)
Void Insert (int pos, const titled)
Void PopBack ()
Void PushFront (const titled)
Void PopFront ()
Void Remove (const titled)
Void RemoveAll (const titled)
Void Reverse ()
Void Sort ()
Void BubbleSort ()
Void print ()
Void DelKNode (int k)
Dlist& Merge (Dlist& L1, Dlist& L2)
Void EraseNotTail (Node* pos)
Void InsertFrontNode (Node* pos, const T & d)
Node * Find (const titled)
Ostream & operator_next
While (fast- > _ next)
{
Fast = fast- > _ next
Slow = slow- > _ next
}
Slow- > _ prev- > _ next = slow- > _ next
Slow- > _ next- > _ prev = slow- > _ prev
Delete slow
}
Template
Void Dlist::Sort ()
{
Node * cur = _ head
Node * 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
Void Dlist::BubbleSort ()
{
Node * cur = _ head
Node * 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
Void Dlist::EraseNotTail (Node* pos)
{
Pos- > _ prev- > _ next = pos- > _ next
Pos- > _ next- > _ prev = pos- > _ prev
Delete pos
Pos = NULL
}
Template
Void Dlist::Remove (const titled)
{
Node * 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
Node * Dlist::Find (const titled)
{
Node * cur = _ head
While (cur)
{
If (cur- > _ data = = d)
Return cur
Cur = cur- > _ next
}
Return NULL
}
Template
Void Dlist::RemoveAll (const titled)
{
Node * cur = _ head
Node * 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
Void Dlist::print ()
{
Node* cur = _ head
While (cur)
{
Cout _ data _ next
}
Cout _ 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
Dlist& Dlist::operator = (const Dlist&list)
{
Node * del = _ head
Node * tem = _ tail
If (this! = & list)
{
Delete _ head
_ head = NULL
Delete _ tail
_ tail = NULL
Node * cur = list._head
While (cur)
{
Dlist::PushBack (cur- > _ data)
Cur = cur- > _ next
}
}
Return * this
}
Template
Void Dlist::Insert (int pos, const titled)
{
Node * NewNode = new Node (d)
Node * cur = _ head
While (cur & & (--pos))
{
Cur = cur- > _ next
}
NewNode- > _ next = cur- > _ next
NewNode- > _ prev = cur
Cur- > _ next = NewNode
}
Template
Dlist::Dlist ()
{
_ head = NULL
_ tail = NULL
}
Template
Void Dlist::PushFront (const titled)
{
Node* NewNode = new Node (d)
If (_ head = = NULL)
{
_ head = NewNode
_ tail = _ head
}
Else
{
_ head- > _ prev = NewNode
NewNode- > _ next = _ head
_ head = NewNode
}
}
Template
Void Dlist::PushBack (const titled)
{
Node* NewNode = new Node (d)
If (_ head = = NULL)
{
_ head = NewNode
_ tail = _ head
}
Else
{
_ tail- > _ next = NewNode
NewNode- > _ prev = _ tail
_ tail = NewNode
}
}
Template
Void Dlist::PopBack ()
{
If (_ head = = NULL)
Return
Else if (_ head = = _ tail)
{
Delete _ head
_ head = NULL
_ tail = _ head
}
Else
{
_ tail = _ tail- > _ prev
_ tail- > _ next = NULL
}
}
Template
Void Dlist::Reverse ()
{
Node* NewNode = NULL
Node * cur = _ head
Node * 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
Void Dlist::PopFront ()
{
If (_ head = = NULL)
Return
Else if (_ head = = _ tail)
{
Delete _ head
_ head = NULL
_ tail = _ head
}
Else
{
_ head = _ head- > _ next
_ head- > _ prev = NULL
}
}
Template
Dlist::Dlist (const Dlist& list)
{
Node * cur = list._head
While (cur)
{
Dlist::PushBack (cur- > _ data)
Cur = cur- > _ next
}
}
Int main ()
{
Dlist list1
Dlist list2
Node * tem = NULL
Node * 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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.