复杂链表的复制:一个链表的每个节点,有一个指向next指针指向下一个节点,还有一个random指针指向这个链表中一个随机节点或者NULL,现在要求实现复制这个链表,返回复制后的新链表。
思路:先复制每一个原始结点并将其放在每一个原始结点的后面,在确定每一个随机节点,最后将原始结点和复制节点形成的链表分开成原始链表和复制链表。
#define_CRT_SECURE_NO_WARNINGS 1
#include<iostream>
usingnamespace std;
typedefintDataType;
structNode
{
Node(constDataType& d)
:_data(d)
, _next(NULL)
, _random(NULL)
{}
DataType _data; //数据
structNode* _next; //指向下一个节点的指针
structNode* _random;//指向随机节点(可以是链表中的任意节点or空)
};
classSList
{
friendostream& operator<<(ostream& os, constSList& s);
public:
SList()
:_head(NULL)
, _tail(NULL)
{}
~SList()
{
if (_head == NULL)
return;
Node *cur = _head;
while (cur->_next != NULL)
{
Node *del = cur;
cur = cur->_next;
delete del;
}
delete cur;
_head = NULL;
_tail = NULL;
}
SList(constSList& s)
:_head(NULL)
, _tail(NULL)
{
Node *cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
SList& operator=(SLists)
{
swap(_head, s._head);
swap(_tail, s._tail);
return *this;
}
public:
void PushBack(constDataType& d);
void PopBack();
void PushFront(constDataType& d);
void PopFront();
Node *Find(constDataType& d);
void Insert(Node *pos, constDataType& d);
void Clone(constDataType& d);
void Random(constDataType& d);
Node* Sort(constDataType& d);
private:
Node *_head;
Node *_tail;
};
voidSList::PushBack(constDataType& d) //尾插
{
Node *newNode = newNode(d);
if (_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
_tail->_next = newNode;
_tail = newNode;
}
}
voidSList::PopBack() //尾删
{
if (_head == NULL)
return;
if (_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
return;
}
Node *cur = _head;
while (cur->_next != _tail)
{
cur = cur->_next;
}
delete _tail;
_tail = cur;
_tail->_next = NULL;
}
voidSList::PushFront(constDataType& d) //头插
{
Node *newNode = newNode(d);
if (_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
newNode->_next = _head;
_head = newNode;
}
}
voidSList::PopFront() //头删
{
if (_head == NULL)
return;
Node *del = _head;
_head = _head->_next;
delete del;
}
Node *SList::Find(constDataType& d) //查找
{
Node *cur = _head;
while (cur)
{
if (cur->_data == d)
{
return cur;
}
cur = cur->_next;
}
returnNULL;
}
voidSList::Insert(Node *pos, constDataType& d) //插入
{
Node *newNode = newNode(d);
if (pos == _tail)
{
_tail->_next = newNode;
_tail = newNode;
}
else
{
newNode->_next = pos->_next;
pos->_next = newNode;
}
}
voidSList::Clone(constDataType& d) //复制每一个节点
{
Node* cur = _head;
while (cur)
{
Node* tmp = newNode(d);
tmp->_data = cur->_data;
tmp->_next = cur->_next;
tmp->_random = NULL;
cur->_next = tmp;
cur = tmp->_next;
}
}
voidSList::Random(constDataType& d) //确定_random随机节点
{
Node* cur = _head; //原始节点
Node* clone=newNode(d); //复制节点
while (cur)
{
clone = cur->_next;
if (cur->_random)
{
clone->_random = cur->_random->_next;
cur = clone->_next;
}
}
}
Node *SList::Sort(constDataType& d) //将链表拆分成原始链表和拷贝链表
{
Node* cur = _head; //原始节点
Node* clone = newNode(d); //复制节点
Node* CloneHead = newNode(d); //复制链表的头结点
if (cur)
{
CloneHead = cur->_next;
cur->_next = CloneHead->_next;
cur = CloneHead->_next;
clone = CloneHead;
}
while (cur)
{
Node* temp = cur->_next;
cur->_next = temp->_next;
cur = cur->_next;
clone->_next = temp;
clone = temp;
}
return CloneHead;
}
ostream& operator<<(ostream& os, constSList& s)
{
if (s._head == NULL)
returnos;
Node *cur = s._head;
while (cur != NULL)
{
os << cur->_data << "->";
cur = cur->_next;
}
cout << "over" << endl;
returnos;
}
void Test1()
{
SList s1;
s1.PushBack(5);
s1.PushBack(4);
s1.PushBack(3);
s1.PushBack(2);
s1.PushBack(1);
cout << s1;
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.PopBack();
cout << s1;
s1.PushFront(4);
s1.PushFront(3);
s1.PushFront(2);
s1.PushFront(1);
cout << s1;
s1.PopFront();
s1.PopFront();
cout << s1;
s1.Insert(s1.Find(5), 6);
cout << s1;
SList s2(s1);
cout << s2;
SList s3;
s3 = s2;
cout << s3;
s1.Clone(1);
cout << s1;
s1.Sort(1);
cout << s1;
}
int main()
{
Test1();
getchar();
return 0;
}
本文出自 “追” 博客,转载请与作者联系!
原文地址:http://10802712.blog.51cto.com/10792712/1764163