一、程序
#include<iostream>
#include<stack>
using namespace std;
typedef struct Node
{
int data;;
Node* next;
Node(int x)
:data(x)
,next(NULL)
{}
}Node;
typedef struct ComplexNode
{
int data;
ComplexNode* next;
ComplexNode* rodom; //随机域
ComplexNode(int x)
:data(x)
,next(NULL)
,rodom(NULL)
{}
}ComplexNode;
//1.从尾到头打印单链表;借助stack,时间复杂度O(1),空间复杂度O(N)
void PrintfStratEnd(Node* head)
{
if (head==NULL)//1.链表为空,不用打印
return ;
//2.链表不为空,利用栈的先进后出原则打印
Node* cur=head;
stack<Node*> s;
while(cur)
{
s.push(cur);
cur=cur->next;
}
while(!s.empty())
{
cout<<(s.top()->data)<<"->";
s.pop();
}
cout<<endl;
}
//1.1从尾到头打印单链表--递归
void PrintfEnd1(Node* head)
{
if (head==NULL)
return ;
PrintfEnd1(head->next);
cout<<head->data<<endl;
}
//2.删除单链表某个非尾位置的结点
void DeletepPos(Node* pos)
{
if (pos==NULL||pos->next==NULL)//1.如果pos为空或者为最后一个结点,不删除,直接返回
return ;
//2.pos不为空且不是尾位置,是一个正确的位置
Node* NextNext=pos->next->next;//记录该位置的下下位置
pos->data=pos->next->data;//将该位置和下一位置的值进行交换
delete pos->next;//释放下一个位置的内存
pos->next=NextNext;//将该位置连接到下下位置中
}
//3.复杂链表的复制
ComplexNode* CopyComplexList(ComplexNode* head)
{
if (head==NULL)
return NULL;
ComplexNode* cur=head;
ComplexNode* NewNode=NULL;
//1.复制链表结点并连接到原链表
while(cur)
{
NewNode=new ComplexNode(cur->data);
NewNode->next=cur->next;
cur->next=NewNode;
cur=NewNode->next;
}
//2.给新链表结点复制随机值
cur=head;
while (cur)
{
ComplexNode* Node=cur->next;
if (cur->rodom)
{
Node->rodom=cur->rodom->next;
}
cur=Node->next;
}
//3.将原链表和新链表分离
cur=head;
ComplexNode* NewHead=head->next;
while(cur->next->next)
{
ComplexNode* NewNode=cur->next;
cur->next=NewNode->next;
cur=NewNode->next;
NewNode->next=cur->next;
}
cur->next=NULL;
return NewHead;
}
void PrintfList(ComplexNode* head)
{
if (head==NULL)
return ;
ComplexNode* cur=head;
while(cur)
{
if (cur->rodom)
{
cout<<cur->data<<"->"<<cur->rodom->data<<endl;
}
else
cout<<cur->data<<endl;
cur=cur->next;
}
cout<<endl;
}
void Test()
{
Node* Node1=new Node(1);
Node* Node2=new Node(2);
Node* Node3=new Node(3);
Node* Node4=new Node(4);
Node* Node5=new Node(5);
Node* Node6=new Node(6);
Node1->next=Node2;
Node2->next=Node3;
Node3->next=Node4;
Node4->next=Node5;
Node5->next=Node6;
//1.从尾到头打印单链表
cout<<"从尾到头打印单链表"<<endl;
PrintfStratEnd(Node1);
//2.删除单链表某个非尾位置的结点
cout<<"删除单链表结点4"<<endl;
DeletepPos(Node4);
PrintfStratEnd(Node1);
cout<<"删除单链表结点6"<<endl;
DeletepPos(Node6);
PrintfStratEnd(Node1);
cout<<"删除单链表结点1"<<endl;
DeletepPos(Node1);
PrintfStratEnd(Node1);
ComplexNode* Node11=new ComplexNode(1);
ComplexNode* Node22=new ComplexNode(2);
ComplexNode* Node33=new ComplexNode(3);
ComplexNode* Node44=new ComplexNode(4);
ComplexNode* Node55=new ComplexNode(5);
ComplexNode* Node66=new ComplexNode(6);
Node11->next=Node22;
Node22->next=Node33;
Node33->next=Node44;
Node44->next=Node55;
Node55->next=Node66;
Node11->rodom=Node33;
Node22->rodom=Node55;
Node44->rodom=Node66;
Node66->rodom=Node66;
ComplexNode* NewHead=CopyComplexList(Node11);
cout<<"拷贝之后新的链表"<<endl;
PrintfList(NewHead);
cout<<"拷贝之后原来的链表"<<endl;
PrintfList(Node11);
}
int main()
{
Test();
return 0;
}
结果:
END!!!