/**设计一个算法,将两个带表头结点的双向循环链表接成一个表,其中一个链在另一个之后**/
#include "iostream"
using namespace std;
template
struct Node
{
Node(const T& e=T(),Node* next=NULL,Node* before=NULL):element(e),left(before),right(next){}
T element;
Node* left;
Node* right;
};
Node* createList()
{
Node* head=new Node(0,NULL,NULL);
int e;
Node* p;
Node* r;
char c;
cout<<"input? y/n"<while((c=getchar())=='\n');
while(tolower(c)!='n')
{
cin>>e;
p=new Node(e,NULL,NULL);
if(head->right==NULL)
{
head->right=p;
p->left=head;
}
else
{
r->right=p;
p->left=r;
}
r=p;
cout<<"input? y/n"<while((c=getchar())=='\n');
}
r->right=head->right;
return head;
}
template
void printList(Node* head)
{
Node* temp=head->right;
cout<element<<",";
temp=temp->right;
for(;temp->right!=head->right;temp=temp->right)
cout<element<<",";
cout<element<<",";
}
template
Node* links(Node* head1,Node* head2)
{
Node* real1=head1->right;
Node* real2=head2->right;
Node* temp1=real1->right;
Node* temp2=real2->right;
for(;temp1->right!=real1;temp1=temp1->right);
temp1->right=head2->right;
head2->right->left=temp1;
for(;temp2->right!=real2;temp2=temp2->right);
temp2->right=head1->right;
head1->right->left=temp2;
return head1;
}
void main()
{
Node* head1=createList();
Node* head2=createList();
Node* head=links(head1,head2);
printList(head);
}
阅读(1398) | 评论(0) | 转发(0) |