Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213082
  • 博文数量: 35
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 14:27
文章分类

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-03-16 13:20:20

/**设计一个算法,将两个带表头结点的双向循环链表接成一个表,其中一个链在另一个之后**/
#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);
}
阅读(1371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~